From c404bc02df002da6756024b13d64453cc7425758 Mon Sep 17 00:00:00 2001 From: Robert Lord Date: Tue, 29 Oct 2013 10:40:07 -0700 Subject: [PATCH] Add default language selecting with localStorage Now, Slate will use localStorage to track the last selected language, so if the user visits again with no language in the URL, they will be redirected to the last language they used. This update also fixes a bug with how the layout inserts the language names into the javascript. Previously, they would be, (for some inane reason) arrays within arrays. It is now just an array. Whoever wrote that previous code was clearly a fool. --- source/javascripts/lang_selector.js | 13 ++++++++++++- source/layouts/layout.erb | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/source/javascripts/lang_selector.js b/source/javascripts/lang_selector.js index a9bf0b8..4e3dbc9 100644 --- a/source/javascripts/lang_selector.js +++ b/source/javascripts/lang_selector.js @@ -21,18 +21,29 @@ function activateLanguage(language) { $(".highlight." + languages[i]).hide(); } $(".highlight." + language).show(); + } function setupLanguages(l) { languages = l; currentLanguage = languages[0]; + defaultLanguage = localStorage.getItem("language"); - if (location.search.substr(1) != "") { + if ((location.search.substr(1) != "") && (jQuery.inArray(location.search.substr(1), languages)) != -1) { + // the language is in the URL, so use that language! activateLanguage(location.search.substr(1)); + + // set this language as the default for next time, if the URL has no language + localStorage.setItem("language", location.search.substr(1)); + } else if ((defaultLanguage !== null) && (jQuery.inArray(defaultLanguage, languages) != -1)) { + // the language was the last selected one saved in localstorage, so use that language! + activateLanguage(defaultLanguage); } else { + // no language selected, so use the default activateLanguage(languages[0]); } + // if we click on a language tab, reload the page with that language in the URL $("#lang-selector a").bind("click", function() { window.location.replace("?" + $(this).data("language-name") + window.location.hash); return false; diff --git a/source/layouts/layout.erb b/source/layouts/layout.erb index d1e1795..46d8b34 100644 --- a/source/layouts/layout.erb +++ b/source/layouts/layout.erb @@ -51,9 +51,9 @@ under the License. <% if current_page.data.language_tabs %> <% current_page.data.language_tabs.each do |lang| %> <% if lang.is_a? Hash %> - ['<%= lang.keys[0] %>'], + '<%= lang.keys[0] %>', <% else %> - ['<%= lang %>'], + '<%= lang %>', <% end %> <% end %> <% end %>