Commit c404bc02 authored by Robert Lord's avatar Robert Lord

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.
parent 5acbe01c
...@@ -21,18 +21,29 @@ function activateLanguage(language) { ...@@ -21,18 +21,29 @@ function activateLanguage(language) {
$(".highlight." + languages[i]).hide(); $(".highlight." + languages[i]).hide();
} }
$(".highlight." + language).show(); $(".highlight." + language).show();
} }
function setupLanguages(l) { function setupLanguages(l) {
languages = l; languages = l;
currentLanguage = languages[0]; 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)); 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 { } else {
// no language selected, so use the default
activateLanguage(languages[0]); 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() { $("#lang-selector a").bind("click", function() {
window.location.replace("?" + $(this).data("language-name") + window.location.hash); window.location.replace("?" + $(this).data("language-name") + window.location.hash);
return false; return false;
......
...@@ -51,9 +51,9 @@ under the License. ...@@ -51,9 +51,9 @@ under the License.
<% if current_page.data.language_tabs %> <% if current_page.data.language_tabs %>
<% current_page.data.language_tabs.each do |lang| %> <% current_page.data.language_tabs.each do |lang| %>
<% if lang.is_a? Hash %> <% if lang.is_a? Hash %>
['<%= lang.keys[0] %>'], '<%= lang.keys[0] %>',
<% else %> <% else %>
['<%= lang %>'], '<%= lang %>',
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment