summaryrefslogtreecommitdiffstats
path: root/roles/reverseproxy/files/conversejs/src/i18n/index.js
blob: a82ce3ced96dc221497ac56d7cdf7e83ab58000a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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.__;