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.
This commit is contained in:
Robert Lord 2013-10-29 10:40:07 -07:00
parent 5acbe01c3d
commit c404bc02df
2 changed files with 14 additions and 3 deletions

View file

@ -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;

View file

@ -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 %>