let i18nextBrowserLanguageDetector detect the lang

This commit is contained in:
s2
2019-04-15 17:55:56 +02:00
parent 84ba6c30f3
commit 0be7831276
147 changed files with 927 additions and 139 deletions

View File

@@ -11,6 +11,27 @@
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
MyApp.Utils.removeURLParameter = function(url, parameter) {
//prefer to use l.search if you have a location/link object
var urlparts = url.split('?');
if (urlparts.length >= 2) {
var prefix = encodeURIComponent(parameter) + '=';
var pars = urlparts[1].split(/[&;]/g);
//reverse iteration as may be destructive
for (var i = pars.length; i-- > 0;) {
//idiom for string.startsWith
if (pars[i].lastIndexOf(prefix, 0) !== -1) {
pars.splice(i, 1);
}
}
return urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : '');
}
return url;
};
// app functions
MyApp.renderShell = function() {
@@ -61,8 +82,17 @@
// app startup
// set language
i18next.init({
i18next
.use(i18nextBrowserLanguageDetector)
.init({
lng: MyApp.config.lang,
detection: {
order: ['querystring', 'cookie', 'navigator'],
lookupQuerystring: 'lang',
lookupCookie: 'current-language',
caches: ['cookie'],
cookieMinutes: 5256000
},
resources: {
en: {
translation: {
@@ -86,6 +116,13 @@
})
.then(function() {
// language initialized
// if there is a lang query string parameter, remove it and reload: we save the language in a cookie so the url stays nice
if (MyApp.Utils.getUrlParameter('lang')) {
var newUrl = MyApp.Utils.removeURLParameter(window.location.toString(), 'lang');
//removeURLParameter does not return the hash. add it back if there is one.
newUrl = newUrl + window.location.hash;
window.location = newUrl;
}
// render main shell
MyApp.renderShell()