Skip to content
Snippets Groups Projects
Select Git revision
  • 0fa4ce98c32000088b946399ee8a26e2f76b4fd9
  • main default protected
  • renovate/lock-file-maintenance
  • demo protected
  • person-select-custom
  • dbp-translation-component
  • icon-set-mapping
  • port-i18next-parser
  • remove-sentry
  • favorites-and-recent-files
  • revert-6c632dc6
  • lit2
  • advertisement
  • wc-part
  • automagic
  • publish
  • wip-cleanup
  • demo-file-handling
18 results

i18next.js

Blame
  • i18next.js 6.06 KiB
    import i18next from 'i18next';
    
    /**
     * Like Intl.DateTimeFormat().format() but uses the current language as locale.
     *
     * A i18next instance can be created with createInstance()
     *
     * @param {i18next.i18n} i18n - The i18next instance
     * @param {Date} date - The date to format
     * @param {object} options - Options passed to Intl.DateTimeFormat
     * @returns {string} The formatted datetime
     */
    export function dateTimeFormat(i18n, date, options = {}) {
        return new Intl.DateTimeFormat(i18n.languages, options).format(date);
    }
    
    /**
     * Like Intl.NumberFormat().format() but uses the current language as locale.
     *
     * A i18next instance can be created with createInstance()
     *
     * @param {i18next.i18n} i18n - The i18next instance
     * @param {number} number - The number to format
     * @param {object} options - Options passed to Intl.NumberFormat
     * @returns {string} The formatted number
     */
    export function numberFormat(i18n, number, options = {}) {
        return new Intl.NumberFormat(i18n.languages, options).format(number);
    }
    
    export function humanFileSize(bytes, si = false) {
        const thresh = si ? 1000 : 1024;
        if (Math.abs(bytes) < thresh) {
            return bytes + ' B';
        }
        const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        let u = -1;
        do {
            bytes /= thresh;
            ++u;
        } while (Math.abs(bytes) >= thresh && u < units.length - 1);
        return bytes.toFixed(1) + ' ' + units[u];
    }
    
    /**
     * @param {string} namespace The namespace to override
     * @returns {string} The new namespace name
     */
    function getOverrideNamespace(namespace) {
        // This just needs to be different to the namespace, make it special
        // so it's clear what it is used for in case it ends up in some error
        // message or something
        return '--' + namespace + '-override';
    }
    
    /**
     * Creates a new i18next instance that is fully initialized.
     *
     * Call changeLanguage() on the returned object to change the language.
     *
     * @param {object} languages - Mapping from languages to translation objects
     * @param {string} lng - The default language
     * @param {string} fallback - The fallback language to use for unknown languages or untranslated keys
     * @param {string} [namespace] - The i18next namespace to load, defaults to 'translation'
     * @returns {i18next.i18n} A new independent i18next instance
     */
    export function createInstance(languages, lng, fallback, namespace) {
        if (namespace === undefined) {
            namespace = 'translation';
        }