mirror of
https://github.com/danbulant/api_docs
synced 2026-05-19 12:19:08 +00:00
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:
parent
5acbe01c3d
commit
c404bc02df
2 changed files with 14 additions and 3 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 %>
|
||||
|
|
|
|||
Loading…
Reference in a new issue