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EAAE,CAAC;IAEnD,EAAE,CAAC,CAAC,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC;QAC7B,EAAE,CAAC,CAAC,QAAQ,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC;YAC/C,SAAS,CAAC,IAAI,CAAC;gBACd,IAAI,EAAE;oBACL,CAAC,UAAU,CAAC,EAAE,EAAE;iBAChB;aACD,CAAC,CAAC;YACH,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QAC9B,CAAC;QAED,cAAc,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IAC7D,CAAC;AACF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAW,CAAC;IACpC,IAAI,YAAY,GAAG,IAAI,CAAC;IACxB,EAAE,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;QACzB,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QACnC,YAAY,GAAG,SAAS,CAAC,QAAQ,IAAI,SAAS,CAAC,YAAY,CAAC;IAC7D,CAAC;IAAC,IAAI,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;QAC7B,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,YAAY,CAAC;IACjD,CAAC;IACD,MAAM,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;AACtC,CAAC,CAAC,EAAE,CAAC","sourcesContent":["/* tslint:disable:interface-name */\nimport global from '@dojo/shim/global';\nimport Map from '@dojo/shim/Map';\nimport Evented from '@dojo/core/Evented';\nimport has from '@dojo/core/has';\nimport uuid from '@dojo/core/uuid';\nimport { Handle } from '@dojo/core/interfaces';\nimport { useDefault } from '@dojo/core/load/util';\nimport * as Globalize from 'globalize/dist/globalize/message';\nimport { isLoaded } from './cldr/load';\nimport { generateLocales, normalizeLocale } from './util/main';\n\n/**\n * A default bundle used as basis for loading locale-specific bundles.\n */\nexport interface Bundle<T extends Messages> {\n\t/**\n\t * A unique identifier for the bundle that will be generated automatically when it is registered.\n\t */\n\treadonly id?: string;\n\n\t/**\n\t * A list of supported locales. Any included locale MUST have an associated bundle.\n\t */\n\treadonly locales?: LocaleLoaders<T>;\n\n\t/**\n\t * The map of default messages that will be used when locale-specific messages are unavailable.\n\t * Note that any message key used in the i18n system MUST have a default specified here.\n\t */\n\treadonly messages: T;\n}\n\n/**\n * Options object passed to message formatters and used for token replacement.\n */\nexport interface FormatOptions {\n\t[key: string]: any;\n}\n\nexport interface I18n<T extends Messages> {\n\t(bundle: Bundle<T>, locale?: string): Promise<T>;\n\n\t/**\n\t * The current namespace as set via `switchLocale`. Defaults to `systemLocale`.\n\t */\n\treadonly locale: string;\n}\n\n/**\n * A map of locales to functions responsible for loading their respective translations.\n */\nexport interface LocaleLoaders<T extends Messages> {\n\t[locale: string]: () => LocaleTranslations<T> | Promise<LocaleTranslations<T>>;\n}\n\n/**\n * An object of locale-specific translations.\n */\nexport type LocaleTranslations<T extends Messages> = Partial<T> | { default?: Partial<T> };\n\n/**\n * Describes a compiled ICU message formatter function.\n */\nexport interface MessageFormatter {\n\t(options?: FormatOptions): string;\n}\n\n/**\n * An object of keys to locale messages.\n */\nexport interface Messages {\n\t[key: string]: string;\n}\n\nconst TOKEN_PATTERN = /\\{([a-z0-9_]+)\\}/gi;\nconst bundleMap = new Map<string, Map<string, Messages>>();\nconst formatterMap = new Map<string, MessageFormatter>();\nconst localeProducer = new Evented();\nlet rootLocale: string;\n\n/**\n * Return the bundle's unique identifier, creating one if it does not already exist.\n *\n * @param bundle A message bundle\n * @return The bundle's unique identifier\n */\nfunction getBundleId<T extends Messages>(bundle: Bundle<T>): string {\n\tif (bundle.id) {\n\t\treturn bundle.id;\n\t}\n\n\tconst id = uuid();\n\tObject.defineProperty(bundle, 'id', {\n\t\tvalue: id\n\t});\n\treturn id;\n}\n\n/**\n * @private\n * Return a function that formats an ICU-style message, and takes an optional value for token replacement.\n *\n * Usage:\n * const formatter = getMessageFormatter(bundle, 'guestInfo', 'fr');\n * const message = formatter({\n *   host: 'Miles',\n *   gender: 'male',\n *   guest: 'Oscar',\n *   guestCount: '15'\n * });\n *\n * @param id\n * The message's bundle id.\n *\n * @param key\n * The message's key.\n *\n * @param locale\n * An optional locale for the formatter. If no locale is supplied, or if the locale is not supported, the\n * default locale is used.\n *\n * @return\n * The message formatter.\n */\nfunction getIcuMessageFormatter(id: string, key: string, locale?: string): MessageFormatter {\n\tlocale = normalizeLocale(locale || getRootLocale());\n\tconst formatterKey = `${locale}:${id}:${key}`;\n\tlet formatter = formatterMap.get(formatterKey);\n\n\tif (formatter) {\n\t\treturn formatter;\n\t}\n\n\tconst globalize = locale !== getRootLocale() ? new Globalize(normalizeLocale(locale)) : Globalize;\n\tformatter = globalize.messageFormatter(`${id}/${key}`);\n\n\tconst cached = bundleMap.get(id);\n\tif (cached && cached.get(locale)) {\n\t\tformatterMap.set(formatterKey, formatter);\n\t}\n\n\treturn formatter;\n}\n\n/**\n * @private\n * Load the specified locale-specific bundles, mapping the default exports to simple `Messages` objects.\n */\nfunction loadLocaleBundles<T extends Messages>(locales: LocaleLoaders<T>, supported: string[]): Promise<T[]> {\n\treturn Promise.all(supported.map((locale) => locales[locale]())).then((bundles) => {\n\t\treturn bundles.map((bundle) => useDefault(bundle));\n\t});\n}\n\n/**\n * @private\n * Return the root locale. Defaults to the system locale.\n */\nfunction getRootLocale(): string {\n\treturn rootLocale || systemLocale;\n}\n\n/**\n * @private\n * Retrieve a list of supported locales that can provide messages for the specified locale.\n *\n * @param locale\n * The target locale.\n *\n * @param supported\n * The locales that are supported by the bundle.\n *\n * @return\n * A list of supported locales that match the target locale.\n */\nfunction getSupportedLocales(locale: string, supported: string[] = []): string[] {\n\treturn generateLocales(locale).filter((locale: string) => supported.indexOf(locale) > -1);\n}\n\n/**\n * @private\n * Inject messages for the specified locale into the i18n system.\n *\n * @param id\n * The bundle's unique identifier\n *\n * @param messages\n * The messages to inject\n *\n * @param locale\n * An optional locale. If not specified, then it is assumed that the messages are the defaults for the given\n * bundle path.\n */\nfunction loadMessages<T extends Messages>(id: string, messages: T, locale: string = 'root') {\n\tlet cached = bundleMap.get(id);\n\n\tif (!cached) {\n\t\tcached = new Map<string, Messages>();\n\t\tbundleMap.set(id, cached);\n\t}\n\n\tcached.set(locale, messages);\n\tGlobalize.loadMessages({\n\t\t[locale]: {\n\t\t\t[id]: messages\n\t\t}\n\t});\n}\n\n/**\n * Return a formatted message.\n *\n * If both the \"supplemental/likelySubtags\" and \"supplemental/plurals-type-cardinal\" CLDR data have been loaded, then\n * the ICU message format is supported. Otherwise, a simple token-replacement mechanism is used.\n *\n * Usage:\n * formatMessage(bundle, 'guestInfo', {\n *   host: 'Bill',\n *   guest: 'John'\n * }, 'fr');\n *\n * @param bundle\n * The bundle containing the target message.\n *\n * @param key\n * The message's key.\n *\n * @param options\n * An optional value used by the formatter to replace tokens with values.\n *\n * @param locale\n * An optional locale for the formatter. If no locale is supplied, or if the locale is not supported, the\n * default locale is used.\n *\n * @return\n * The formatted message.\n */\nexport function formatMessage<T extends Messages>(\n\tbundle: Bundle<T>,\n\tkey: string,\n\toptions?: FormatOptions,\n\tlocale?: string\n): string {\n\treturn getMessageFormatter(bundle, key, locale)(options);\n}\n\n/**\n * Return the cached messages for the specified bundle and locale. If messages have not been previously loaded for the\n * specified locale, no value will be returned.\n *\n * @param bundle\n * The default bundle that is used to determine where the locale-specific bundles are located.\n *\n * @param locale\n * The locale of the desired messages.\n *\n * @return The cached messages object, if it exists.\n */\nexport function getCachedMessages<T extends Messages>(bundle: Bundle<T>, locale: string): T | void {\n\tconst { id = getBundleId(bundle), locales, messages } = bundle;\n\tconst cached = bundleMap.get(id);\n\n\tif (!cached) {\n\t\tloadMessages(id, messages);\n\t} else {\n\t\tconst localeMessages = cached.get(locale);\n\t\tif (localeMessages) {\n\t\t\treturn localeMessages as T;\n\t\t}\n\t}\n\n\tconst supportedLocales = getSupportedLocales(locale, locales && Object.keys(locales));\n\tif (!supportedLocales.length) {\n\t\treturn messages;\n\t}\n\n\tif (cached) {\n\t\treturn cached.get(supportedLocales[supportedLocales.length - 1]) as T;\n\t}\n}\n\n/**\n * Return a function that formats a specific message, and takes an optional value for token replacement.\n *\n * If both the \"supplemental/likelySubtags\" and \"supplemental/plurals-type-cardinal\" CLDR data have been loaded, then\n * the returned function will have ICU message format support. Otherwise, the returned function will perform a simple\n * token replacement on the message string.\n *\n * Usage:\n * const formatter = getMessageFormatter(bundle, 'guestInfo', 'fr');\n * const message = formatter({\n *   host: 'Miles',\n *   gender: 'male',\n *   guest: 'Oscar',\n *   guestCount: '15'\n * });\n *\n * @param bundle\n * The bundle containing the target message.\n *\n * @param key\n * The message's key.\n *\n * @param locale\n * An optional locale for the formatter. If no locale is supplied, or if the locale is not supported, the\n * default locale is used.\n *\n * @return\n * The message formatter.\n */\nexport function getMessageFormatter<T extends Messages>(\n\tbundle: Bundle<T>,\n\tkey: string,\n\tlocale?: string\n): MessageFormatter {\n\tconst { id = getBundleId(bundle) } = bundle;\n\n\tif (isLoaded('supplemental', 'likelySubtags') && isLoaded('supplemental', 'plurals-type-cardinal')) {\n\t\treturn getIcuMessageFormatter(id, key, locale);\n\t}\n\n\tconst cached = bundleMap.get(id);\n\tconst messages = cached ? cached.get(locale || getRootLocale()) || cached.get('root') : null;\n\n\tif (!messages) {\n\t\tthrow new Error(`The bundle has not been registered.`);\n\t}\n\n\treturn function(options: FormatOptions = Object.create(null)) {\n\t\treturn messages[key].replace(TOKEN_PATTERN, (token: string, property: string) => {\n\t\t\tconst value = options[property];\n\n\t\t\tif (typeof value === 'undefined') {\n\t\t\t\tthrow new Error(`Missing property ${property}`);\n\t\t\t}\n\n\t\t\treturn value;\n\t\t});\n\t};\n}\n\n/**\n * Load locale-specific messages for the specified bundle and locale.\n *\n * @param bundle\n * The default bundle that is used to determine where the locale-specific bundles are located.\n *\n * @param locale\n * An optional locale. If no locale is provided, then the current locale is assumed.\n *\n * @return A promise to the locale-specific messages.\n */\nasync function i18n<T extends Messages>(bundle: Bundle<T>, locale?: string): Promise<T> {\n\tconst currentLocale = locale ? normalizeLocale(locale) : getRootLocale();\n\tconst cachedMessages = getCachedMessages(bundle, currentLocale);\n\n\tif (cachedMessages) {\n\t\treturn cachedMessages;\n\t}\n\n\tconst locales = bundle.locales as LocaleLoaders<T>;\n\tconst supportedLocales = getSupportedLocales(currentLocale, Object.keys(locales));\n\tconst bundles = await loadLocaleBundles<T>(locales, supportedLocales);\n\treturn bundles.reduce((previous: any, partial: any): T => {\n\t\tconst localeMessages: T = { ...previous, ...partial };\n\t\tloadMessages(getBundleId(bundle), <T>Object.freeze(localeMessages), currentLocale);\n\t\treturn localeMessages;\n\t}, bundle.messages);\n}\n\nObject.defineProperty(i18n, 'locale', {\n\tget: getRootLocale\n});\n\nexport default i18n as I18n<Messages>;\n\n/**\n * Invalidate the cache for a particular bundle, or invalidate the entire cache. Note that cached messages for all\n * locales for a given bundle will be cleared.\n *\n * @param bundle\n * An optional bundle to invalidate. If no bundle is provided, then the cache is cleared for all bundles.\n */\nexport function invalidate<T extends Messages>(bundle?: Bundle<T>) {\n\tif (bundle) {\n\t\tbundle.id && bundleMap.delete(bundle.id);\n\t} else {\n\t\tbundleMap.clear();\n\t}\n}\n\n/**\n * Register an observer to be notified when the root locale changes.\n *\n * @param callback\n * A callback function which will receive the updated locale string on updates.\n *\n * @return\n * A handle object that can be used to unsubscribe from updates.\n */\nexport const observeLocale = function(callback: (locale: string) => {}): Handle {\n\treturn localeProducer.on('change', (event: any) => {\n\t\tcallback(event.target);\n\t});\n};\n\n/**\n * Pre-load locale-specific messages into the i18n system.\n *\n * @param bundle\n * The default bundle that is used to merge locale-specific messages with the default messages.\n *\n * @param messages\n * The messages to cache.\n *\n * @param locale\n * The locale for the messages\n */\nexport function setLocaleMessages<T extends Messages>(\n\tbundle: Bundle<T>,\n\tlocaleMessages: Partial<T>,\n\tlocale: string\n): void {\n\tconst messages: T = { ...(bundle.messages as any), ...(localeMessages as any) };\n\tloadMessages(getBundleId(bundle), <T>Object.freeze(messages), locale);\n}\n\n/**\n * Change the root locale, and notify any registered observers.\n *\n * @param locale\n * The new locale.\n */\nexport function switchLocale(locale: string): void {\n\tconst previous = rootLocale;\n\trootLocale = locale ? normalizeLocale(locale) : '';\n\n\tif (previous !== rootLocale) {\n\t\tif (isLoaded('supplemental', 'likelySubtags')) {\n\t\t\tGlobalize.load({\n\t\t\t\tmain: {\n\t\t\t\t\t[rootLocale]: {}\n\t\t\t\t}\n\t\t\t});\n\t\t\tGlobalize.locale(rootLocale);\n\t\t}\n\n\t\tlocaleProducer.emit({ type: 'change', target: rootLocale });\n\t}\n}\n\n/**\n * The default environment locale.\n *\n * It should be noted that while the system locale will be normalized to a single\n * format when loading message bundles, this value represents the unaltered\n * locale returned directly by the environment.\n */\nexport const systemLocale: string = (function() {\n\tlet systemLocale = 'en';\n\tif (has('host-browser')) {\n\t\tconst navigator = global.navigator;\n\t\tsystemLocale = navigator.language || navigator.userLanguage;\n\t} else if (has('host-node')) {\n\t\tsystemLocale = process.env.LANG || systemLocale;\n\t}\n\treturn normalizeLocale(systemLocale);\n})();\n"]}