2020-01-26 16:21:20 +01:00
|
|
|
/**
|
|
|
|
* @module i18n
|
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
|
|
|
* @description This is the internationalization module
|
|
|
|
*/
|
2020-09-10 07:33:00 +02:00
|
|
|
import Jed from 'jed';
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import log from "@converse/headless/log";
|
|
|
|
import { _converse, api, i18n } from '@converse/headless/converse-core';
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2018-11-15 11:51:43 +01:00
|
|
|
|
|
|
|
function detectLocale (library_check) {
|
|
|
|
/* Determine which locale is supported by the user's system as well
|
2019-05-06 11:16:56 +02:00
|
|
|
* as by the relevant library (e.g. converse.js or dayjs).
|
2019-03-29 23:47:56 +01:00
|
|
|
* @param { Function } library_check - Returns a boolean indicating whether
|
|
|
|
* the locale is supported.
|
2018-11-15 11:51:43 +01:00
|
|
|
*/
|
|
|
|
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);
|
2017-10-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 11:51:43 +01:00
|
|
|
if (window.navigator.browserLanguage && !locale) {
|
|
|
|
locale = isLocaleAvailable(window.navigator.browserLanguage, library_check);
|
2017-10-24 12:56:35 +02:00
|
|
|
}
|
2018-11-15 11:51:43 +01:00
|
|
|
if (window.navigator.language && !locale) {
|
|
|
|
locale = isLocaleAvailable(window.navigator.language, library_check);
|
2017-10-24 12:56:35 +02:00
|
|
|
}
|
2018-11-15 11:51:43 +01:00
|
|
|
if (window.navigator.systemLanguage && !locale) {
|
|
|
|
locale = isLocaleAvailable(window.navigator.systemLanguage, library_check);
|
|
|
|
}
|
|
|
|
return locale || 'en';
|
|
|
|
}
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2018-11-15 11:51:43 +01:00
|
|
|
function isConverseLocale (locale, supported_locales) {
|
2019-08-23 14:03:52 +02:00
|
|
|
return typeof locale === 'string' && supported_locales.includes(locale);
|
2018-11-15 11:51:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLocale (preferred_locale, isSupportedByLibrary) {
|
2019-08-23 14:03:52 +02:00
|
|
|
if (typeof preferred_locale === 'string') {
|
2018-11-15 11:51:43 +01:00
|
|
|
if (preferred_locale === 'en' || isSupportedByLibrary(preferred_locale)) {
|
|
|
|
return preferred_locale;
|
2017-10-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 11:51:43 +01:00
|
|
|
return detectLocale(isSupportedByLibrary) || 'en';
|
|
|
|
}
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
/* Check whether the locale or sub locale (e.g. en-US, en) is supported.
|
|
|
|
* @param { String } locale - The locale to check for
|
|
|
|
* @param { Function } available - Returns a boolean indicating whether the locale is supported
|
|
|
|
*/
|
2018-11-15 11:51:43 +01:00
|
|
|
function isLocaleAvailable (locale, available) {
|
|
|
|
if (available(locale)) {
|
|
|
|
return locale;
|
|
|
|
} else {
|
|
|
|
var sublocale = locale.split("-")[0];
|
|
|
|
if (sublocale !== locale && available(sublocale)) {
|
|
|
|
return sublocale;
|
2017-10-24 12:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 11:51:43 +01:00
|
|
|
}
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2020-09-10 07:33:00 +02:00
|
|
|
|
|
|
|
/* Fetch the translations for the given local at the given URL.
|
|
|
|
* @private
|
|
|
|
* @method i18n#fetchTranslations
|
|
|
|
* @param { _converse }
|
|
|
|
*/
|
|
|
|
async function fetchTranslations (_converse) {
|
|
|
|
const { api, locale } = _converse;
|
2020-10-15 11:13:43 +02:00
|
|
|
const dayjs_locale = locale.toLowerCase().replace('_', '-');
|
|
|
|
|
2020-09-10 07:33:00 +02:00
|
|
|
if (!isConverseLocale(locale, api.settings.get("locales")) || locale === 'en') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { default: data } = await import(/*webpackChunkName: "locales/[request]" */ `../i18n/${locale}/LC_MESSAGES/converse.po`);
|
2020-10-15 11:13:43 +02:00
|
|
|
await import(/*webpackChunkName: "locales/dayjs/[request]" */ `dayjs/locale/${dayjs_locale}`);
|
|
|
|
dayjs.locale(getLocale(dayjs_locale, l => dayjs.locale(l)));
|
2020-09-10 07:33:00 +02:00
|
|
|
jed_instance = new Jed(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-15 11:51:43 +01:00
|
|
|
let jed_instance;
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
/**
|
|
|
|
* @namespace i18n
|
|
|
|
*/
|
2020-09-10 07:33:00 +02:00
|
|
|
Object.assign(i18n, {
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2019-08-23 14:03:52 +02:00
|
|
|
getLocale (preferred_locale, available_locales) {
|
|
|
|
return getLocale(preferred_locale, preferred => isConverseLocale(preferred, available_locales));
|
2018-11-15 11:51:43 +01:00
|
|
|
},
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2018-11-15 11:51:43 +01:00
|
|
|
translate (str) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (!jed_instance) {
|
2018-11-15 11:51:43 +01:00
|
|
|
return Jed.sprintf.apply(Jed, arguments);
|
|
|
|
}
|
2019-03-29 23:47:56 +01:00
|
|
|
const t = jed_instance.translate(str);
|
|
|
|
if (arguments.length > 1) {
|
2018-11-15 11:51:43 +01:00
|
|
|
return t.fetch.apply(t, [].slice.call(arguments, 1));
|
|
|
|
} else {
|
|
|
|
return t.fetch();
|
|
|
|
}
|
|
|
|
},
|
2017-10-24 12:56:35 +02:00
|
|
|
|
2020-09-10 07:33:00 +02:00
|
|
|
async initialize () {
|
|
|
|
if (_converse.isTestEnv()) {
|
|
|
|
_converse.locale = 'en';
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
_converse.locale = i18n.getLocale(api.settings.get('i18n'), api.settings.get("locales"));
|
|
|
|
await fetchTranslations(_converse);
|
|
|
|
} catch (e) {
|
|
|
|
log.fatal(e.message);
|
|
|
|
_converse.locale = 'en';
|
|
|
|
}
|
2019-08-23 10:41:28 +02:00
|
|
|
}
|
2020-09-10 07:33:00 +02:00
|
|
|
},
|
2020-01-23 10:18:41 +01:00
|
|
|
|
2020-09-10 07:33:00 +02:00
|
|
|
__ (...args) {
|
|
|
|
return i18n.translate(...args);
|
|
|
|
}
|
|
|
|
});
|
2020-01-23 10:18:41 +01:00
|
|
|
|
2020-09-10 07:33:00 +02:00
|
|
|
export const __ = i18n.__;
|