All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.dist.prod.asset-registries.i18n.js.map Maven / Gradle / Ivy

{
  "version": 3,
  "sources": ["../../../src/asset-registries/i18n.ts"],
  "sourcesContent": ["import getLocale from \"../locale/getLocale.js\";\nimport { attachLanguageChange } from \"../locale/languageChange.js\";\nimport normalizeLocale from \"../locale/normalizeLocale.js\";\nimport nextFallbackLocale from \"../locale/nextFallbackLocale.js\";\nimport { DEFAULT_LANGUAGE } from \"../generated/AssetParameters.js\";\nimport { getFetchDefaultLanguage } from \"../config/Language.js\";\n\ntype I18nLoader = (localeId: string) => Promise;\ntype I18nData = Record;\n\n// contains package names for which the warning has been shown\nconst warningShown = new Set();\nconst reportedErrors = new Set();\n\nconst bundleData = new Map();\nconst bundlePromises = new Map>();\nconst loaders = new Map();\n\n/**\n * Registers i18n loader function for given package and locale.\n *\n * @public\n * @param {string} packageName for which package this loader can fetch data\n * @param {string} localeId locale that this loader can handle\n * @param {function} loader async function that will be passed a localeId and should return a JSON object\n */\nconst registerI18nLoader = (packageName: string, localeId: string, loader: I18nLoader) => {\n\t// register loader by key\n\tconst bundleKey = `${packageName}/${localeId}`;\n\tloaders.set(bundleKey, loader);\n};\n\nconst _setI18nBundleData = (packageName: string, data: I18nData | null) => {\n\tbundleData.set(packageName, data);\n};\n\nconst getI18nBundleData = (packageName: string) => {\n\treturn bundleData.get(packageName);\n};\n\nconst _hasLoader = (packageName: string, localeId: string) => {\n\tconst bundleKey = `${packageName}/${localeId}`;\n\treturn loaders.has(bundleKey);\n};\n\n// load bundle over the network once\nconst _loadMessageBundleOnce = (packageName: string, localeId: string) => {\n\tconst bundleKey = `${packageName}/${localeId}`;\n\tconst loadMessageBundle = loaders.get(bundleKey);\n\n\tif (loadMessageBundle && !bundlePromises.get(bundleKey)) {\n\t\tbundlePromises.set(bundleKey, loadMessageBundle(localeId));\n\t}\n\n\treturn bundlePromises.get(bundleKey)!; // Investigate if i18n loader exists and this won't return undefined.\n};\n\nconst _showAssetsWarningOnce = (packageName: string) => {\n\tif (!warningShown.has(packageName)) {\n\t\tconsole.warn(`[${packageName}]: Message bundle assets are not configured. Falling back to English texts.`, /* eslint-disable-line */\n\t\t\t` Add \\`import \"${packageName}/dist/Assets.js\"\\` in your bundle and make sure your build tool supports dynamic imports and JSON imports. See section \"Assets\" in the documentation for more information.`); /* eslint-disable-line */\n\t\twarningShown.add(packageName);\n\t}\n};\n\nconst useFallbackBundle = (packageName: string, localeId: string) => {\n\treturn localeId !== DEFAULT_LANGUAGE && !_hasLoader(packageName, localeId);\n};\n\n/**\n * This method preforms the asynchronous task of fetching the actual text resources. It will fetch\n * each text resource over the network once (even for multiple calls to the same method).\n * It should be fully finished before the i18nBundle class is created in the webcomponents.\n * This method uses the bundle URLs that are populated by the `registerI18nBundle` method.\n * To simplify the usage, the synchronization of both methods happens internally for the same `bundleId`\n * @param {packageName} packageName the NPM package name\n * @public\n */\nconst fetchI18nBundle = async (packageName: string) => {\n\tconst language = getLocale().getLanguage();\n\tconst region = getLocale().getRegion();\n\tconst variant = getLocale().getVariant();\n\tlet localeId = language + (region ? `-${region}` : ``) + (variant ? `-${variant}` : ``);\n\n\tif (useFallbackBundle(packageName, localeId)) {\n\t\tlocaleId = normalizeLocale(localeId);\n\n\t\twhile (useFallbackBundle(packageName, localeId)) {\n\t\t\tlocaleId = nextFallbackLocale(localeId);\n\t\t}\n\t}\n\n\t// use default language unless configured to always fetch it from the network\n\tconst fetchDefaultLanguage = getFetchDefaultLanguage();\n\tif (localeId === DEFAULT_LANGUAGE && !fetchDefaultLanguage) {\n\t\t_setI18nBundleData(packageName, null); // reset for the default language (if data was set for a previous language)\n\t\treturn;\n\t}\n\n\tif (!_hasLoader(packageName, localeId)) {\n\t\t_showAssetsWarningOnce(packageName);\n\t\treturn;\n\t}\n\n\ttry {\n\t\tconst data = await _loadMessageBundleOnce(packageName, localeId);\n\t\t_setI18nBundleData(packageName, data);\n\t} catch (error: unknown) {\n\t\tconst e = error as Error;\n\t\tif (!reportedErrors.has(e.message)) {\n\t\t\treportedErrors.add(e.message);\n\t\t\tconsole.error(e.message); /* eslint-disable-line */\n\t\t}\n\t}\n};\n\n// When the language changes dynamically (the user calls setLanguage), re-fetch all previously fetched bundles\nattachLanguageChange((lang: string /* eslint-disable-line */) => {\n\tconst allPackages = [...bundleData.keys()];\n\treturn Promise.all(allPackages.map(fetchI18nBundle));\n});\n\nexport {\n\tregisterI18nLoader,\n\tfetchI18nBundle,\n\tgetI18nBundleData,\n};\n"],
  "mappings": "aAAA,OAAOA,MAAe,yBACtB,OAAS,wBAAAC,MAA4B,8BACrC,OAAOC,MAAqB,+BAC5B,OAAOC,MAAwB,kCAC/B,OAAS,oBAAAC,MAAwB,kCACjC,OAAS,2BAAAC,MAA+B,wBAMxC,MAAMC,EAAe,IAAI,IACnBC,EAAiB,IAAI,IAErBC,EAAa,IAAI,IACjBC,EAAiB,IAAI,IACrBC,EAAU,IAAI,IAUdC,EAAqB,CAACC,EAAqBC,EAAkBC,IAAuB,CAEzF,MAAMC,EAAY,GAAGH,CAAW,IAAIC,CAAQ,GAC5CH,EAAQ,IAAIK,EAAWD,CAAM,CAC9B,EAEME,EAAqB,CAACJ,EAAqBK,IAA0B,CAC1ET,EAAW,IAAII,EAAaK,CAAI,CACjC,EAEMC,EAAqBN,GACnBJ,EAAW,IAAII,CAAW,EAG5BO,EAAa,CAACP,EAAqBC,IAAqB,CAC7D,MAAME,EAAY,GAAGH,CAAW,IAAIC,CAAQ,GAC5C,OAAOH,EAAQ,IAAIK,CAAS,CAC7B,EAGMK,EAAyB,CAACR,EAAqBC,IAAqB,CACzE,MAAME,EAAY,GAAGH,CAAW,IAAIC,CAAQ,GACtCQ,EAAoBX,EAAQ,IAAIK,CAAS,EAE/C,OAAIM,GAAqB,CAACZ,EAAe,IAAIM,CAAS,GACrDN,EAAe,IAAIM,EAAWM,EAAkBR,CAAQ,CAAC,EAGnDJ,EAAe,IAAIM,CAAS,CACpC,EAEMO,EAA0BV,GAAw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ale", "nextFallbackLocale", "DEFAULT_LANGUAGE", "getFetchDefaultLanguage", "warningShown", "reportedErrors", "bundleData", "bundlePromises", "loaders", "registerI18nLoader", "packageName", "localeId", "loader", "bundleKey", "_setI18nBundleData", "data", "getI18nBundleData", "_hasLoader", "_loadMessageBundleOnce", "loadMessageBundle", "_showAssetsWarningOnce", "useFallbackBundle", "fetchI18nBundle", "language", "region", "variant", "fetchDefaultLanguage", "error", "e", "lang", "allPackages"]
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy