You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/customize.dist/languageSelector.js

50 lines
1.3 KiB
JavaScript

define(['/bower_components/jquery/dist/jquery.min.js'], function() {
var $ = window.jQuery;
var out = {};
var LS_LANG = "CRYPTPAD_LANG";
var getStoredLanguage = function () {
return localStorage.getItem(LS_LANG);
};
var storeLanguage = function (l) {
localStorage.setItem(LS_LANG, l);
};
var getBrowserLanguage = function () {
return navigator.language || navigator.userLanguage;
};
var getLanguage = out.getLanguage = function () {
return getStoredLanguage() || getBrowserLanguage();
};
var main = out.main = function ($select) {
var selector = $select || $('#language-selector');
if (!selector.length) { return; }
// Select the current language in the list
var language = getLanguage();
var option = $(selector).find('option[value="' + language + '"]');
if ($(option).length) {
$(selector).val(language);
}
else {
$(selector).val('en');
}
// Listen for language change
$(selector).on('change', function () {
var newLanguage = $(selector).val();
storeLanguage(newLanguage);
if (newLanguage !== language) {
window.location.reload();
}
});
};
return out;
});