/** * @module i18n * @copyright 2020, the Converse.js contributors * @license Mozilla Public License (MPLv2) * @description This is the internationalization module */ import Jed from "jed"; import dayjs from "dayjs"; 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 dayjs). * @param { 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 isConverseLocale(preferred, available_locales)); }, translate (str) { if (!jed_instance) { return Jed.sprintf.apply(Jed, arguments); } const t = jed_instance.translate(str); if (arguments.length > 1) { return t.fetch.apply(t, [].slice.call(arguments, 1)); } else { return t.fetch(); } }, /** * Fetch the translations for the given local at the given URL. * @private * @method i18n#fetchTranslations * @param { _converse } */ async fetchTranslations (_converse) { const locale = _converse.locale; if (!isConverseLocale(locale, _converse.locales) || locale === 'en') { return; } const { default: data } = await import(/*webpackChunkName: "locales/[request]" */ `../../locale/${locale}/LC_MESSAGES/converse.po`); await import(/*webpackChunkName: "locales/dayjs/[request]" */ `dayjs/locale/${locale.toLowerCase().replace('_', '-')}`); dayjs.locale(getLocale(_converse.locale, l => dayjs.locale(l))); jed_instance = new Jed(data); } }; export const __ = function () { return i18n.translate.apply(i18n, arguments); }