浏览代码

Fix i18n type errors

JC Brand 1 年之前
父节点
当前提交
c07ca9b106
共有 2 个文件被更改,包括 24 次插入31 次删除
  1. 23 30
      src/i18n/index.js
  2. 1 1
      src/plugins/omemo/utils.js

+ 23 - 30
src/i18n/index.js

@@ -5,32 +5,31 @@
  * @description This is the internationalization module
  * @description This is the internationalization module
  */
  */
 import Jed from 'jed';
 import Jed from 'jed';
-import { _converse, api, converse, log, i18n } from '@converse/headless';
+import { api, converse, log, i18n } from '@converse/headless';
 import { isTestEnv } from '@converse/headless/utils/session';
 import { isTestEnv } from '@converse/headless/utils/session';
 
 
 const { dayjs } = converse.env;
 const { dayjs } = converse.env;
 
 
 let jed_instance;
 let jed_instance;
+let locale = 'en';
 
 
 /**
 /**
- * @private
- * @param { string } locale
- * @param { string[] } supported_locales
+ * @param {string} preferred_locale
+ * @param {string[]} supported_locales
  */
  */
-function isConverseLocale (locale, supported_locales) {
-    return typeof locale === 'string' && supported_locales.includes(locale);
+function isConverseLocale (preferred_locale, supported_locales) {
+    return supported_locales.includes(preferred_locale);
 }
 }
 
 
 /**
 /**
  * Determines which locale is supported by the user's system as well
  * Determines which locale is supported by the user's system as well
  * as by the relevant library (e.g. converse.js or dayjs).
  * as by the relevant library (e.g. converse.js or dayjs).
- * @private
- * @param { string } preferred_locale
- * @param { Function } isSupportedByLibrary - Returns a boolean indicating whether
+ * @param {string} preferred_locale
+ * @param {Function} isSupportedByLibrary - Returns a boolean indicating whether
  *   the locale is supported.
  *   the locale is supported.
- * @returns { string }
+ * @returns {string}
  */
  */
-function getLocale (preferred_locale, isSupportedByLibrary) {
+function determineLocale (preferred_locale, isSupportedByLibrary) {
     if (preferred_locale === 'en' || isSupportedByLibrary(preferred_locale)) {
     if (preferred_locale === 'en' || isSupportedByLibrary(preferred_locale)) {
         return preferred_locale;
         return preferred_locale;
     }
     }
@@ -46,9 +45,8 @@ function getLocale (preferred_locale, isSupportedByLibrary) {
 
 
 /**
 /**
  * Check whether the locale or sub locale (e.g. en-US, en) is supported.
  * 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
+ * @param {string} locale - The locale to check for
+ * @param {Function} available - Returns a boolean indicating whether the locale is supported
  */
  */
 function isLocaleAvailable (locale, available) {
 function isLocaleAvailable (locale, available) {
     if (available(locale)) {
     if (available(locale)) {
@@ -63,8 +61,7 @@ function isLocaleAvailable (locale, available) {
 
 
 /**
 /**
  * Given a locale, return the closest locale returned by dayJS
  * Given a locale, return the closest locale returned by dayJS
- * @private
- * @param { string } locale
+ * @param {string} locale
  */
  */
 function getDayJSLocale (locale) {
 function getDayJSLocale (locale) {
     const dayjs_locale = locale.toLowerCase().replace('_', '-');
     const dayjs_locale = locale.toLowerCase().replace('_', '-');
@@ -73,11 +70,9 @@ function getDayJSLocale (locale) {
 
 
 /**
 /**
  * Fetch the translations for the given local at the given URL.
  * Fetch the translations for the given local at the given URL.
- * @private
- * @returns { Jed }
+ * @returns {Jed}
  */
  */
 async function fetchTranslations () {
 async function fetchTranslations () {
-    const { api, locale } = _converse;
     const dayjs_locale = getDayJSLocale(locale);
     const dayjs_locale = getDayJSLocale(locale);
 
 
     if (!isConverseLocale(locale, api.settings.get('locales')) || locale === 'en') {
     if (!isConverseLocale(locale, api.settings.get('locales')) || locale === 'en') {
@@ -87,7 +82,7 @@ async function fetchTranslations () {
         /*webpackChunkName: "locales/[request]" */ `../i18n/${locale}/LC_MESSAGES/converse.po`
         /*webpackChunkName: "locales/[request]" */ `../i18n/${locale}/LC_MESSAGES/converse.po`
     );
     );
     await import(/*webpackChunkName: "locales/dayjs/[request]" */ `dayjs/locale/${dayjs_locale}.js`);
     await import(/*webpackChunkName: "locales/dayjs/[request]" */ `dayjs/locale/${dayjs_locale}.js`);
-    dayjs.locale(getLocale(dayjs_locale, (l) => dayjs.locale(l)));
+    dayjs.locale(determineLocale(dayjs_locale, (l) => dayjs.locale(l)));
     return new Jed(data);
     return new Jed(data);
 }
 }
 
 
@@ -97,16 +92,12 @@ async function fetchTranslations () {
  */
  */
 Object.assign(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));
+    getLocale () {
+        return locale;
     },
     },
 
 
     /**
     /**
-     * @param { string } str - The string to be translated
+     * @param {string} str - The string to be translated
      */
      */
     translate (str) {
     translate (str) {
         if (!jed_instance) {
         if (!jed_instance) {
@@ -122,15 +113,17 @@ Object.assign(i18n, {
 
 
     async initialize () {
     async initialize () {
         if (isTestEnv()) {
         if (isTestEnv()) {
-            _converse.locale = 'en';
+            locale = 'en';
         } else {
         } else {
             try {
             try {
                 const preferred_locale = api.settings.get('i18n');
                 const preferred_locale = api.settings.get('i18n');
-                _converse.locale = i18n.getLocale(preferred_locale, api.settings.get('locales'));
+                const available_locales = api.settings.get('locales');
+                const isSupportedByLibrary = /** @param {string} pref */(pref) => isConverseLocale(pref, available_locales);
+                locale = determineLocale(preferred_locale, isSupportedByLibrary);
                 jed_instance = await fetchTranslations();
                 jed_instance = await fetchTranslations();
             } catch (e) {
             } catch (e) {
                 log.fatal(e.message);
                 log.fatal(e.message);
-                _converse.locale = 'en';
+                locale = 'en';
             }
             }
         }
         }
     },
     },

+ 1 - 1
src/plugins/omemo/utils.js

@@ -838,7 +838,7 @@ async function getBundlesAndBuildSessions (chatbox) {
             throw new UserFacingError(no_devices_err);
             throw new UserFacingError(no_devices_err);
         }
         }
         const bare_jid = _converse.session.get('bare_jid');
         const bare_jid = _converse.session.get('bare_jid');
-        const own_list = await api.omemo.devicelists.get(bare_jid)
+        const own_list = await api.omemo.devicelists.get(bare_jid);
         const own_devices = own_list.devices;
         const own_devices = own_list.devices;
         devices = [...own_devices.models, ...their_devices.models];
         devices = [...own_devices.models, ...their_devices.models];
     }
     }