diff --git a/public/js/main.js b/public/js/main.js index 5d985acf..fbf5b3b9 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -47,3 +47,36 @@ for(var i in list) { var date = new Date(e.innerText); e.innerText = date.toDateString() + " " + date.toLocaleTimeString(); } + +function loadLanguages() { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4 && xhr.status == 200) { + var selector = document.getElementById("bottom_language_selector"); + selector.hidden = false + /* Response format is + * { "current": "(user current language)", + * "languages": { + * "(language_code)": "(language_name"), + * }} */ + var response = JSON.parse(xhr.responseText); + for (var language in response.languages) { + if (!response.languages.hasOwnProperty(language)) continue; + + var opt = document.createElement("option") + opt.value = language + opt.innerHTML = response.languages[language] + if (language == response.current) { + opt.selected = true + } + + selector.appendChild(opt) + } + } + } + xhr.open("GET", "/language?format=json", true) + xhr.send() +} + +loadLanguages(); + diff --git a/router/changeLanguageHandler.go b/router/changeLanguageHandler.go index 8de035a3..75f88314 100644 --- a/router/changeLanguageHandler.go +++ b/router/changeLanguageHandler.go @@ -1,6 +1,7 @@ package router; import ( + "encoding/json" "net/http" "github.com/ewhal/nyaa/util/languages" @@ -8,16 +9,31 @@ import ( "github.com/gorilla/mux" ) +type LanguagesJSONResponse struct { + Current string `json:"current"` + Languages map[string]string `json:"languages"` +} + func SeeLanguagesHandler(w http.ResponseWriter, r *http.Request) { _, Tlang := languages.GetTfuncAndLanguageFromRequest(r, "en-us") availableLanguages := languages.GetAvailableLanguages() - clv := ChangeLanguageVariables{NewSearchForm(), Navigation{}, Tlang.Tag, availableLanguages, GetUser(r), r.URL, mux.CurrentRoute(r)} - languages.SetTranslationFromRequest(changeLanguageTemplate, r, "en-us") - err := changeLanguageTemplate.ExecuteTemplate(w, "index.html", clv) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + format := r.URL.Query().Get("format") + if format == "json" { + w.Header().Set("Content-Type", "application/json") + err := json.NewEncoder(w).Encode(LanguagesJSONResponse{Tlang.Tag, availableLanguages}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + clv := ChangeLanguageVariables{NewSearchForm(), Navigation{}, Tlang.Tag, availableLanguages, GetUser(r), r.URL, mux.CurrentRoute(r)} + languages.SetTranslationFromRequest(changeLanguageTemplate, r, "en-us") + err := changeLanguageTemplate.ExecuteTemplate(w, "index.html", clv) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } } diff --git a/templates/index.html b/templates/index.html index 93810b2f..44eed24d 100755 --- a/templates/index.html +++ b/templates/index.html @@ -95,7 +95,13 @@ -
{{ T "change_language" }}
+ +
+ +
+