qwc2-lts
Version:
QGIS Web Client
112 lines (111 loc) • 4.26 kB
JavaScript
/**
* Copyright 2015 GeoSolutions Sas
* Copyright 2016-2024 Sourcepole AG
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import ReducerIndex from '../reducers/index';
import localeReducer from '../reducers/locale';
ReducerIndex.register("locale", localeReducer);
import { getLanguageCountries } from '@ladjs/country-language';
import axios from 'axios';
import deepmerge from 'deepmerge';
import ConfigUtils from '../utils/ConfigUtils';
import { UrlParams } from '../utils/PermaLinkUtils';
export var CHANGE_LOCALE = 'CHANGE_LOCALE';
export var ADD_TRANSLATIONS = 'ADD_TRANSLATIONS';
export function loadLocale(defaultLangData) {
var defaultLang = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
return function (dispatch) {
var lang = defaultLang || UrlParams.getParam("lang") || (navigator ? navigator.language || navigator.browserLanguage : "en-US");
var config = {
headers: {
'Content-Type': 'application/json'
},
data: {}
};
var translationsPath = ConfigUtils.getTranslationsPath();
axios.get(translationsPath + '/' + lang + '.json', config).then(function (response) {
var messages = response.data.messages;
if (ConfigUtils.getConfigProp("loadTranslationOverrides")) {
axios.get(translationsPath + '/' + lang + '_overrides.json', config).then(function (response2) {
var overrideMessages = response2.data.messages;
dispatch({
type: CHANGE_LOCALE,
locale: lang,
messages: deepmerge(messages, overrideMessages),
fallbackMessages: defaultLangData.messages
});
})["catch"](function () {
dispatch({
type: CHANGE_LOCALE,
locale: lang,
messages: messages,
fallbackMessages: defaultLangData.messages
});
});
} else {
dispatch({
type: CHANGE_LOCALE,
locale: lang,
messages: messages,
fallbackMessages: defaultLangData.messages
});
}
})["catch"](function (e) {
var langCode = lang.slice(0, 2).toLowerCase();
var countries = getLanguageCountries(langCode);
var country = countries.find(function (entry) {
return entry.code_2 === langCode.toUpperCase();
}) ? langCode.toUpperCase() : (countries[0] || {}).code_2 || "";
// eslint-disable-next-line
console.warn("Failed to load locale for " + lang + " (" + e + "), trying " + langCode + "-" + country);
lang = langCode + "-" + country;
axios.get(translationsPath + '/' + lang + '.json', config).then(function (response) {
var messages = response.data.messages;
if (ConfigUtils.getConfigProp("loadTranslationOverrides")) {
axios.get(translationsPath + '/' + lang + '_overrides.json', config).then(function (response2) {
var overrideMessages = response2.data.messages;
dispatch({
type: CHANGE_LOCALE,
locale: lang,
messages: deepmerge(messages, overrideMessages),
fallbackMessages: defaultLangData.messages
});
})["catch"](function () {
dispatch({
type: CHANGE_LOCALE,
locale: lang,
messages: messages,
fallbackMessages: defaultLangData.messages
});
});
} else {
dispatch({
type: CHANGE_LOCALE,
locale: lang,
messages: messages,
fallbackMessages: defaultLangData.messages
});
}
})["catch"](function (e2) {
// eslint-disable-next-line
console.warn("Failed to load locale for " + lang + " (" + e2 + "), defaulting to " + defaultLangData.locale);
dispatch({
type: CHANGE_LOCALE,
locale: defaultLangData.locale,
messages: defaultLangData.messages,
fallbackMessages: defaultLangData.messages
});
});
});
};
}
export function addTranslations(translations) {
return {
type: ADD_TRANSLATIONS,
translations: translations
};
}