cryptpad/customize.dist/messages.js

141 lines
4.4 KiB
JavaScript
Raw Normal View History

2023-10-20 22:35:26 +08:00
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
(function () {
// add your module to this map so it gets used
var map = {
2019-06-03 17:31:12 +08:00
'ca': 'Català',
'cs': 'Čeština',
2019-03-13 22:48:36 +08:00
'de': 'Deutsch',
'el': 'Ελληνικά',
'es': 'Español',
2022-01-05 18:05:26 +08:00
'eu': 'Euskara',
'fi': 'Suomi',
2019-03-13 22:48:36 +08:00
'fr': 'Français',
2020-03-26 18:10:44 +08:00
//'hi': 'हिन्दी',
2024-07-02 16:51:38 +08:00
'id': 'Bahasa Indonesia',
2019-04-26 21:22:29 +08:00
'it': 'Italiano',
'ja': '日本語',
2019-04-17 17:14:18 +08:00
'nb': 'Norwegian Bokmål',
//'nl': 'Nederlands'
2022-04-13 20:28:39 +08:00
'pl': 'Polski',
2017-05-16 20:32:54 +08:00
'pt-br': 'Português do Brasil',
2022-09-01 16:22:41 +08:00
'pt-pt': 'Português do Portugal',
2017-05-16 20:32:54 +08:00
'ro': 'Română',
2019-03-13 22:48:36 +08:00
'ru': 'Русский',
2020-03-21 21:57:49 +08:00
//'sv': 'Svenska',
//'te': 'తెలుగు',
'uk': 'Українська',
'zh': '中文(簡體)',
};
var Messages = {};
var LS_LANG = "CRYPTPAD_LANG";
2017-12-01 21:49:21 +08:00
var getStoredLanguage = function () { return localStorage && localStorage.getItem(LS_LANG); };
var getBrowserLanguage = function () { return navigator.language || navigator.userLanguage || ''; };
var getLanguage = Messages._getLanguage = function () {
if (window.cryptpadLanguage) { return window.cryptpadLanguage; }
2020-11-04 18:22:31 +08:00
try {
if (getStoredLanguage()) { return getStoredLanguage(); }
} catch (e) { console.log(e); }
var l = getBrowserLanguage();
// Edge returns 'fr-FR' --> transform it to 'fr' and check again
return map[l] ? l :
2019-08-23 22:19:45 +08:00
(map[l.split('-')[0]] ? l.split('-')[0] :
(map[l.split('_')[0]] ? l.split('_')[0] : 'en'));
};
var language = getLanguage();
2024-05-07 22:52:01 +08:00
window.cryptpadLanguage = language;
2019-01-30 21:38:21 +08:00
// Translations files were migrated from requirejs modules to json.
// To avoid asking every administrator to update their customized translation files,
// we use a requirejs map to redirect the old path to the new one and to use the
// requirejs json plugin
var reqPaths = {
"/common/translations/messages.js":"json!/common/translations/messages.json"
};
Object.keys(map).forEach(function (k) {
reqPaths["/common/translations/messages."+k+".js"] = "json!/common/translations/messages."+k+".json";
});
require.config({
map: {
"*": reqPaths
}
});
var req = [
'/customize/application_config.js',
'/customize/translations/messages.js'
];
if (language && map[language]) { req.push('/customize/translations/messages.' + language + '.js'); }
2020-10-14 21:20:56 +08:00
define(req, function(AppConfig, Default, Language) {
map.en = 'English';
var defaultLanguage = 'en';
if (AppConfig.availableLanguages) {
if (AppConfig.availableLanguages.indexOf(language) === -1) {
language = defaultLanguage;
Language = Default;
2020-11-04 18:22:31 +08:00
try {
localStorage.setItem(LS_LANG, language);
} catch (e) { console.log(e); }
}
Object.keys(map).forEach(function (l) {
if (l === defaultLanguage) { return; }
if (AppConfig.availableLanguages.indexOf(l) === -1) {
delete map[l];
}
});
}
let html = typeof(document) !== "undefined" && document.documentElement;
if (html) { html.setAttribute('lang', language); }
var extend = function (a, b) {
for (var k in b) {
if (Array.isArray(b[k])) {
a[k] = b[k].slice();
continue;
}
2020-10-14 21:20:56 +08:00
if (b[k] && typeof(b[k]) === "object") {
a[k] = (a[k] && typeof(a[k]) === "object" && !Array.isArray(a[k])) ? a[k] : {};
extend(a[k], b[k]);
continue;
}
a[k] = b[k] || a[k];
}
};
extend(Messages, Default);
2017-12-01 21:49:21 +08:00
if (Language && language !== defaultLanguage) {
2016-09-16 00:35:09 +08:00
// Add the translated keys to the returned object
extend(Messages, Language);
2016-09-16 00:35:09 +08:00
}
2016-07-11 23:36:53 +08:00
Messages._languages = map;
Messages._languageUsed = language;
2016-09-16 00:35:09 +08:00
// Get keys with parameters
Messages._getKey = function (key, argArray) {
if (!Messages[key]) { return '?'; }
var text = Messages[key];
if (typeof(text) === 'string') {
return text.replace(/\{(\d+)\}/g, function (str, p1) {
2018-01-02 19:13:43 +08:00
if (typeof(argArray[p1]) === 'string' || typeof(argArray[p1]) === "number") {
return argArray[p1];
}
console.error("Only strings and numbers can be used in _getKey params!");
return '';
});
} else {
return text;
}
};
2016-07-11 23:36:53 +08:00
return Messages;
});
}());