summaryrefslogtreecommitdiffstats
path: root/roles/reverseproxy/files/conversejs/src/i18n/index.js
diff options
context:
space:
mode:
authorMatthieu Saulnier <fantom@fedoraproject.org>2023-09-10 11:02:37 +0200
committerMatthieu Saulnier <fantom@fedoraproject.org>2023-09-10 11:02:37 +0200
commitc3d25357d4a61a26609480bf84a67a56d1a17fc3 (patch)
tree4209832fa639354a1c905ca6d3ca899e889df90e /roles/reverseproxy/files/conversejs/src/i18n/index.js
parent78a34e844540a6f24a42e49bcc00ce00cd6ec955 (diff)
downloadplaybooks-ansible-c3d25357d4a61a26609480bf84a67a56d1a17fc3.tar.gz
playbooks-ansible-c3d25357d4a61a26609480bf84a67a56d1a17fc3.tar.xz
playbooks-ansible-c3d25357d4a61a26609480bf84a67a56d1a17fc3.zip
Add conversejs deployment
Diffstat (limited to 'roles/reverseproxy/files/conversejs/src/i18n/index.js')
-rw-r--r--roles/reverseproxy/files/conversejs/src/i18n/index.js143
1 files changed, 143 insertions, 0 deletions
diff --git a/roles/reverseproxy/files/conversejs/src/i18n/index.js b/roles/reverseproxy/files/conversejs/src/i18n/index.js
new file mode 100644
index 0000000..a82ce3c
--- /dev/null
+++ b/roles/reverseproxy/files/conversejs/src/i18n/index.js
@@ -0,0 +1,143 @@
+/**
+ * @module i18n
+ * @copyright 2022, the Converse.js contributors
+ * @license Mozilla Public License (MPLv2)
+ * @description This is the internationalization module
+ */
+import Jed from 'jed';
+import log from '@converse/headless/log.js';
+import { _converse, api, converse, i18n } from '@converse/headless/core.js';
+
+const { dayjs } = converse.env;
+
+let jed_instance;
+
+/**
+ * @private
+ * @param { string } locale
+ * @param { string[] } supported_locales
+ */
+function isConverseLocale (locale, supported_locales) {
+ return typeof locale === 'string' && supported_locales.includes(locale);
+}
+
+/**
+ * Determines which locale is supported by the user's system as well
+ * as by the relevant library (e.g. converse.js or dayjs).
+ * @private
+ * @param { string } preferred_locale
+ * @param { Function } isSupportedByLibrary - Returns a boolean indicating whether
+ * the locale is supported.
+ * @returns { string }
+ */
+function getLocale (preferred_locale, isSupportedByLibrary) {
+ if (preferred_locale === 'en' || isSupportedByLibrary(preferred_locale)) {
+ return preferred_locale;
+ }
+
+ const { languages } = window.navigator;
+
+ let locale;
+ for (let i = 0; i < languages.length && !locale; i++) {
+ locale = isLocaleAvailable(languages[i], isSupportedByLibrary);
+ }
+ return locale || 'en';
+}
+
+/**
+ * Check whether the locale or sub locale (e.g. en-US, en) is supported.
+ * @private
+ * @param { String } locale - The locale to check for
+ * @param { Function } available - Returns a boolean indicating whether the locale is supported
+ */
+function isLocaleAvailable (locale, available) {
+ if (available(locale)) {
+ return locale;
+ } else {
+ var sublocale = locale.split('-')[0];
+ if (sublocale !== locale && available(sublocale)) {
+ return sublocale;
+ }
+ }
+}
+
+/**
+ * Given a locale, return the closest locale returned by dayJS
+ * @private
+ * @param { string } locale
+ */
+function getDayJSLocale (locale) {
+ const dayjs_locale = locale.toLowerCase().replace('_', '-');
+ return dayjs_locale === 'ug' ? 'ug-cn' : dayjs_locale;
+}
+
+/**
+ * Fetch the translations for the given local at the given URL.
+ * @private
+ * @returns { Jed }
+ */
+async function fetchTranslations () {
+ const { api, locale } = _converse;
+ const dayjs_locale = getDayJSLocale(locale);
+
+ if (!isConverseLocale(locale, api.settings.get('locales')) || locale === 'en') {
+ return;
+ }
+ const { default: data } = await import(
+ /*webpackChunkName: "locales/[request]" */ `../i18n/${locale}/LC_MESSAGES/converse.po`
+ );
+ await import(/*webpackChunkName: "locales/dayjs/[request]" */ `dayjs/locale/${dayjs_locale}.js`);
+ dayjs.locale(getLocale(dayjs_locale, (l) => dayjs.locale(l)));
+ return new Jed(data);
+}
+
+
+/**
+ * @namespace i18n
+ */
+Object.assign(i18n, {
+
+ /**
+ * @param { string } preferred_locale
+ * @param { string[] } available_locales
+ */
+ getLocale (preferred_locale, available_locales) {
+ return getLocale(preferred_locale, (preferred) => isConverseLocale(preferred, available_locales));
+ },
+
+ /**
+ * @param { string } str - The string to be translated
+ */
+ 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();
+ }
+ },
+
+ async initialize () {
+ if (_converse.isTestEnv()) {
+ _converse.locale = 'en';
+ } else {
+ try {
+ const preferred_locale = api.settings.get('i18n');
+ _converse.locale = i18n.getLocale(preferred_locale, api.settings.get('locales'));
+ jed_instance = await fetchTranslations();
+ } catch (e) {
+ log.fatal(e.message);
+ _converse.locale = 'en';
+ }
+ }
+ },
+
+ __ (...args) {
+ return i18n.translate(...args);
+ },
+});
+
+export const __ = i18n.__;