blueocean-core-js/src/js/i18n/i18n.js
/**
 * Create an i18n Translator instance for accessing i18n resource bundles
 * in the named plugin namespace.
 * @param pluginName The name of the plugin.
 * @param namespace The resource bundle namespace. Optional, defaulting to
 * the plugin's default resource bundle e.g. "jenkins.plugins.blueocean.web.Messages"
 * for the "blueocean-web" plugin and "jenkins.plugins.blueocean.dashboard.Messages"
 * for the "blueocean-dashboard" plugin.
 * @return An i18n Translator instance.
 */
Create an i18n Translator instance for accessing i18n resource bundles
in the named plugin namespace.
@param pluginName The name of the plugin.
@param namespace The resource bundle namespace. Optional, defaulting to
the plugin's default resource bundle e.g.
Create an i18n Translator instance for accessing i18n resource bundles in the named plugin namespace . @param pluginName The name of the plugin . @param namespace The resource bundle namespace . Optional defaulting to the plugin ' s default resource bundle e . g .
function i18nTranslator(pluginName, namespace, onLoad) {
    assertPluginNameDefined(pluginName);
    const translatorCacheKey = buildCacheKey(pluginName, namespace);
    let translator = translatorCache[translatorCacheKey];
    if (translator) {
        if (typeof onLoad === 'function') {
            onLoad();
        }
        return translator;
    }
    return function translate(key, params) {
        if (useMockFallback) {
            return (params && params.defaultValue) || key;
        }
        if (!translator) {
            const I18n = pluginI18next(pluginName, namespace);
            if (typeof onLoad === 'function') {
                I18n.on('loaded', onLoad);
            }
            let detectedLang;
            try {
                detectedLang = defaultLngDetector.detect();
            } catch (e) {
                detectedLang = FALLBACK_LANG;
            }
            if (logger.isLogEnabled()) {
                logger.log('Translator instance created for "%s". Language detected as "%s".', translatorCacheKey, detectedLang);
            }
            const fixedT = I18n.getFixedT(detectedLang, namespace);
            translator = function _translate(i18nKey, i18nParams) {
                const normalizedKey = i18nKey.replace(/[\W]/g, '.');
                let passedParams = i18nParams;
                if (normalizedKey !== i18nKey) {
                    if (!passedParams) {
                        passedParams = {};
                    }
                    if (!passedParams.defaultValue) {
                        passedParams.defaultValue = i18nKey;
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug(`Normalized i18n key "${i18nKey}" to "${normalizedKey}".`);
                    }
                }
                return fixedT(normalizedKey, passedParams);
            };
            translatorCache[translatorCacheKey] = translator;
        }
        if (key) {
            return translator(key, params);
        }
        return undefined;
    };
}
function i18nTranslator ( pluginName , namespace , onLoad ) { assertPluginNameDefined ( pluginName ) ; const translatorCacheKey = buildCacheKey ( pluginName , namespace ) ; let translator = translatorCache [ translatorCacheKey ] ; if ( translator ) { if ( typeof onLoad === 'function' ) { onLoad ( ) ; } return translator ; } return function translate ( key , params ) { if ( useMockFallback ) { return ( params && params . defaultValue ) || key ; } if ( ! translator ) { const I18n = pluginI18next ( pluginName , namespace ) ; if ( typeof onLoad === 'function' ) { I18n . on ( 'loaded' , onLoad ) ; } let detectedLang ; try { detectedLang = defaultLngDetector . detect ( ) ; } catch ( e ) { detectedLang = FALLBACK_LANG ; } if ( logger . isLogEnabled ( ) ) { logger . log ( 'Translator instance created for "%s". Language detected as "%s".' , translatorCacheKey , detectedLang ) ; } const fixedT = I18n . getFixedT ( detectedLang , namespace ) ; translator = function _translate ( i18nKey , i18nParams ) { const normalizedKey = i18nKey . replace ( / [\W] / g , '.' ) ; let passedParams = i18nParams ; if ( normalizedKey !== i18nKey ) { if ( ! passedParams ) { passedParams = { } ; } if ( ! passedParams . defaultValue ) { passedParams . defaultValue = i18nKey ; } if ( logger . isDebugEnabled ( ) ) { logger . debug ( ` ${ i18nKey } ${ normalizedKey } ` ) ; } } return fixedT ( normalizedKey , passedParams ) ; } ; translatorCache [ translatorCacheKey ] = translator ; } if ( key ) { return translator ( key , params ) ; } return undefined ; } ; }
Create an i18n Translator instance for accessing i18n resource bundles
 in the named plugin namespace.
Create an i18n Translator instance for accessing i18n resource bundles in the named plugin namespace .
// Lazily construct what we need instead of on creation // Create and cache the translator instance.