xmpp.chapril.org-conversejs/src/headless/i18n.js

171 lines
5.5 KiB
JavaScript
Raw Normal View History

// Converse.js (A browser based XMPP chat client)
// https://conversejs.org
//
// This is the internationalization module.
//
2018-09-06 16:03:22 +02:00
// Copyright (c) 2013-2017, Jan-Carel Brand <jc@opkode.com>
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
2018-11-15 11:51:43 +01:00
import 'moment/locale/af';
import 'moment/locale/ar';
import 'moment/locale/bg';
import 'moment/locale/ca';
import 'moment/locale/cs';
import 'moment/locale/de';
2019-01-29 05:12:41 +01:00
import 'moment/locale/eo';
2018-11-15 11:51:43 +01:00
import 'moment/locale/es';
import 'moment/locale/eu';
import 'moment/locale/fr';
2018-11-15 12:23:41 +01:00
import 'moment/locale/gl';
2018-11-15 11:51:43 +01:00
import 'moment/locale/he';
import 'moment/locale/hi';
import 'moment/locale/hu';
import 'moment/locale/id';
import 'moment/locale/it';
import 'moment/locale/ja';
import 'moment/locale/nb';
import 'moment/locale/nl';
import 'moment/locale/pl';
import 'moment/locale/pt-br';
import 'moment/locale/ro';
import 'moment/locale/ru';
import 'moment/locale/tr';
import 'moment/locale/uk';
import 'moment/locale/zh-cn';
import 'moment/locale/zh-tw';
import Jed from "jed";
import Promise from "es6-promise/dist/es6-promise.auto";
import _ from "./lodash.noconflict";
import moment from "moment";
2018-11-15 11:51:43 +01:00
function detectLocale (library_check) {
/* Determine which locale is supported by the user's system as well
* as by the relevant library (e.g. converse.js or moment.js).
*
* Parameters:
* (Function) library_check - Returns a boolean indicating whether
* the locale is supported.
*/
var locale, i;
if (window.navigator.userLanguage) {
locale = isLocaleAvailable(window.navigator.userLanguage, library_check);
}
if (window.navigator.languages && !locale) {
for (i=0; i<window.navigator.languages.length && !locale; i++) {
locale = isLocaleAvailable(window.navigator.languages[i], library_check);
}
}
2018-11-15 11:51:43 +01:00
if (window.navigator.browserLanguage && !locale) {
locale = isLocaleAvailable(window.navigator.browserLanguage, library_check);
}
2018-11-15 11:51:43 +01:00
if (window.navigator.language && !locale) {
locale = isLocaleAvailable(window.navigator.language, library_check);
}
2018-11-15 11:51:43 +01:00
if (window.navigator.systemLanguage && !locale) {
locale = isLocaleAvailable(window.navigator.systemLanguage, library_check);
}
return locale || 'en';
}
2018-11-15 11:51:43 +01:00
function isMomentLocale (locale) {
return _.includes(moment.locales(), locale);
}
function isConverseLocale (locale, supported_locales) {
return _.isString(locale) && _.includes(supported_locales, locale);
}
function getLocale (preferred_locale, isSupportedByLibrary) {
if (_.isString(preferred_locale)) {
if (preferred_locale === 'en' || isSupportedByLibrary(preferred_locale)) {
return preferred_locale;
}
}
2018-11-15 11:51:43 +01:00
return detectLocale(isSupportedByLibrary) || 'en';
}
2018-11-15 11:51:43 +01:00
function isLocaleAvailable (locale, available) {
/* Check whether the locale or sub locale (e.g. en-US, en) is supported.
*
* Parameters:
* (String) locale - The locale to check for
* (Function) available - returns a boolean indicating whether the locale is supported
*/
if (available(locale)) {
return locale;
} else {
var sublocale = locale.split("-")[0];
if (sublocale !== locale && available(sublocale)) {
return sublocale;
}
}
2018-11-15 11:51:43 +01:00
}
2018-11-15 11:51:43 +01:00
let jed_instance;
2018-11-15 11:51:43 +01:00
export default {
2018-11-15 11:51:43 +01:00
setLocales (preferred_locale, _converse) {
_converse.locale = getLocale(
preferred_locale,
_.partial(isConverseLocale, _, _converse.locales)
);
moment.locale(getLocale(preferred_locale, isMomentLocale));
},
2018-11-15 11:51:43 +01:00
translate (str) {
if (_.isNil(jed_instance)) {
return Jed.sprintf.apply(Jed, arguments);
}
var t = jed_instance.translate(str);
if (arguments.length>1) {
return t.fetch.apply(t, [].slice.call(arguments, 1));
} else {
return t.fetch();
}
},
2018-11-15 11:51:43 +01:00
fetchTranslations (locale, supported_locales, locale_url) {
/* Fetch the translations for the given local at the given URL.
*
* Parameters:
* (String) locale: The given i18n locale
* (Array) supported_locales: List of locales supported
* (String) locale_url: The URL from which the translations
* should be fetched.
*/
return new Promise((resolve, reject) => {
if (!isConverseLocale(locale, supported_locales) || locale === 'en') {
return resolve();
}
const xhr = new XMLHttpRequest();
xhr.open('GET', locale_url, true);
xhr.setRequestHeader(
'Accept',
"application/json, text/javascript"
);
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 400) {
try {
const data = window.JSON.parse(xhr.responseText);
jed_instance = new Jed(data);
resolve();
} catch (e) {
xhr.onerror(e);
}
2018-11-15 11:51:43 +01:00
} else {
xhr.onerror();
}
2018-11-15 11:51:43 +01:00
};
xhr.onerror = (e) => {
const err_message = e ? ` Error: ${e.message}` : '';
reject(new Error(`Could not fetch translations. Status: ${xhr.statusText}. ${err_message}`));
}
xhr.send();
});
}
};