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ACf,CAAC;KACD,CAAA;IAxFA;QADC,WAAW,EAAE;;;;+CAkBb;IArDa,IAAI;QAPlB,MAAM,CAAC;YACP,IAAI,EAAE,YAAY;YAClB,aAAa,EAAE,CAAC,UAAsB,EAAE,UAA0B,EAAE,EAAE;gBACrE,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,GAAG,GAAG,UAAU,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC;gBACxE,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;YACxB,CAAC;SACD,CAAC;OACa,IAAI,CA4HlB;IAED,MAAM,CAAC,IAAI,CAAC;AACb,CAAC;AAED,eAAe,SAAS,CAAC","sourcesContent":["/* tslint:disable:interface-name */\nimport i18n, { Bundle, formatMessage, getCachedMessages, Messages } from '@dojo/i18n/i18n';\nimport Map from '@dojo/shim/Map';\nimport { isVNode, decorate } from './../d';\nimport { afterRender } from './../decorators/afterRender';\nimport { inject } from './../decorators/inject';\nimport { Constructor, DNode, WidgetProperties, VNodeProperties } from './../interfaces';\nimport { Injector } from './../Injector';\nimport { Registry } from './../Registry';\nimport { WidgetBase } from './../WidgetBase';\n\nexport const INJECTOR_KEY = Symbol('i18n');\n\nexport interface LocaleData {\n\t/**\n\t * The locale for the widget. If not specified, then the root locale (as determined by `@dojo/i18n`) is assumed.\n\t * If specified, the widget's node will have a `lang` property set to the locale.\n\t */\n\tlocale?: string;\n\n\t/**\n\t * An optional flag indicating the widget's text direction. If `true`, then the underlying node's `dir`\n\t * property is set to \"rtl\". If it is `false`, then the `dir` property is set to \"ltr\". Otherwise, the property\n\t * is not set.\n\t */\n\trtl?: boolean;\n}\n\nexport interface I18nProperties extends LocaleData, WidgetProperties {\n\t/**\n\t * An optional override for the bundle passed to the `localizeBundle`. If the override contains a `messages` object,\n\t * then it will completely replace the underlying bundle. Otherwise, a new bundle will be created with the additional\n\t * locale loaders.\n\t */\n\ti18nBundle?: Bundle<Messages> | Map<Bundle<Messages>, Bundle<Messages>>;\n}\n\n/**\n * @private\n * An internal helper interface for defining locale and text direction attributes on widget nodes.\n */\ninterface I18nVNodeProperties extends VNodeProperties {\n\tdir: string;\n\tlang: string;\n}\n\nexport type LocalizedMessages<T extends Messages> = {\n\t/**\n\t * Indicates whether the messages are placeholders while waiting for the actual localized messages to load.\n\t * This is always `false` if the associated bundle does not list any supported locales.\n\t */\n\treadonly isPlaceholder: boolean;\n\n\t/**\n\t * Formats an ICU-formatted message template for the represented bundle.\n\t *\n\t * @param key\n\t * The message key.\n\t *\n\t * @param options\n\t * The values to pass to the formatter.\n\t *\n\t * @return\n\t * The formatted string.\n\t */\n\tformat(key: string, options?: any): string;\n\n\t/**\n\t * The localized messages if available, or either the default messages or a blank bundle depending on the\n\t * call signature for `localizeBundle`.\n\t */\n\treadonly messages: T;\n};\n\n/**\n * interface for I18n functionality\n */\nexport interface I18nMixin {\n\t/**\n\t * Return the cached messages for the specified bundle for the current locale, assuming they have already\n\t * been loaded. If the locale-specific messages have not been loaded, they are fetched and the widget state\n\t * is updated.\n\t *\n\t * @param bundle\n\t * The required bundle object for which available locale messages should be loaded.\n\t *\n\t * @return\n\t * An object containing the localized messages, along with a `format` method for formatting ICU-formatted\n\t * templates and an `isPlaceholder` property indicating whether the returned messages are the defaults.\n\t */\n\tlocalizeBundle<T extends Messages>(bundle: Bundle<T>): LocalizedMessages<T>;\n\n\tproperties: I18nProperties;\n}\n\nexport function registerI18nInjector(localeData: LocaleData, registry: Registry): Injector {\n\tconst injector = new Injector(localeData);\n\tregistry.defineInjector(INJECTOR_KEY, (invalidator) => {\n\t\tinjector.setInvalidator(invalidator);\n\t\treturn () => injector.get();\n\t});\n\treturn injector;\n}\n\nexport function I18nMixin<T extends Constructor<WidgetBase<any>>>(Base: T): T & Constructor<I18nMixin> {\n\t@inject({\n\t\tname: INJECTOR_KEY,\n\t\tgetProperties: (localeData: LocaleData, properties: I18nProperties) => {\n\t\t\tconst { locale = localeData.locale, rtl = localeData.rtl } = properties;\n\t\t\treturn { locale, rtl };\n\t\t}\n\t})\n\tabstract class I18n extends Base {\n\t\tpublic abstract properties: I18nProperties;\n\n\t\t/**\n\t\t * Return a localized messages object for the provided bundle, deferring to the `i18nBundle` property\n\t\t * when present. If the localized messages have not yet been loaded, return either a blank bundle or the\n\t\t * default messages.\n\t\t *\n\t\t * @param bundle\n\t\t * The bundle to localize\n\t\t *\n\t\t * @param useDefaults\n\t\t * If `true`, the default messages will be used when the localized messages have not yet been loaded. If `false`\n\t\t * (the default), then a blank bundle will be returned (i.e., each key's value will be an empty string).\n\t\t */\n\t\tpublic localizeBundle<T extends Messages>(\n\t\t\tbaseBundle: Bundle<T>,\n\t\t\tuseDefaults: boolean = false\n\t\t): LocalizedMessages<T> {\n\t\t\tconst bundle = this._resolveBundle(baseBundle);\n\t\t\tconst messages = this._getLocaleMessages(bundle);\n\t\t\tconst isPlaceholder = !messages;\n\t\t\tconst { locale } = this.properties;\n\t\t\tconst format =\n\t\t\t\tisPlaceholder && !useDefaults\n\t\t\t\t\t? (key: string, options?: any) => ''\n\t\t\t\t\t: (key: string, options?: any) => formatMessage(bundle, key, options, locale);\n\n\t\t\treturn Object.create({\n\t\t\t\tformat,\n\t\t\t\tisPlaceholder,\n\t\t\t\tmessages: messages || (useDefaults ? bundle.messages : this._getBlankMessages(bundle))\n\t\t\t});\n\t\t}\n\n\t\t@afterRender()\n\t\tprotected renderDecorator(result: DNode | DNode[]): DNode | DNode[] {\n\t\t\tdecorate(result, {\n\t\t\t\tmodifier: (node, breaker) => {\n\t\t\t\t\tconst { locale, rtl } = this.properties;\n\t\t\t\t\tconst properties: Partial<I18nVNodeProperties> = {};\n\t\t\t\t\tif (typeof rtl === 'boolean') {\n\t\t\t\t\t\tproperties['dir'] = rtl ? 'rtl' : 'ltr';\n\t\t\t\t\t}\n\t\t\t\t\tif (locale) {\n\t\t\t\t\t\tproperties['lang'] = locale;\n\t\t\t\t\t}\n\t\t\t\t\tnode.properties = { ...node.properties, ...properties };\n\t\t\t\t\tbreaker();\n\t\t\t\t},\n\t\t\t\tpredicate: isVNode\n\t\t\t});\n\t\t\treturn result;\n\t\t}\n\n\t\t/**\n\t\t * @private\n\t\t * Return a message bundle containing an empty string for each key in the provided bundle.\n\t\t *\n\t\t * @param bundle\n\t\t * The message bundle\n\t\t *\n\t\t * @return\n\t\t * The blank message bundle\n\t\t */\n\t\tprivate _getBlankMessages(bundle: Bundle<Messages>): Messages {\n\t\t\tconst blank = {} as Messages;\n\t\t\treturn Object.keys(bundle.messages).reduce((blank, key) => {\n\t\t\t\tblank[key] = '';\n\t\t\t\treturn blank;\n\t\t\t}, blank);\n\t\t}\n\n\t\t/**\n\t\t * @private\n\t\t * Return the cached dictionary for the specified bundle and locale, if it exists. If the requested dictionary does not\n\t\t * exist, then load it and update the instance's state with the appropriate messages.\n\t\t *\n\t\t * @param bundle\n\t\t * The bundle for which to load a locale-specific dictionary.\n\t\t *\n\t\t * @return\n\t\t * The locale-specific dictionary, if it has already been loaded and cached.\n\t\t */\n\t\tprivate _getLocaleMessages(bundle: Bundle<Messages>): Messages | void {\n\t\t\tconst { properties } = this;\n\t\t\tconst locale = properties.locale || i18n.locale;\n\t\t\tconst localeMessages = getCachedMessages(bundle, locale);\n\n\t\t\tif (localeMessages) {\n\t\t\t\treturn localeMessages;\n\t\t\t}\n\n\t\t\ti18n(bundle, locale).then(() => {\n\t\t\t\tthis.invalidate();\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * @private\n\t\t * Resolve the bundle to use for the widget's messages to either the provided bundle or to the\n\t\t * `i18nBundle` property.\n\t\t *\n\t\t * @param bundle\n\t\t * The base bundle\n\t\t *\n\t\t * @return\n\t\t * Either override bundle or the original bundle.\n\t\t */\n\t\tprivate _resolveBundle(bundle: Bundle<Messages>): Bundle<Messages> {\n\t\t\tlet { i18nBundle } = this.properties;\n\t\t\tif (i18nBundle) {\n\t\t\t\tif (i18nBundle instanceof Map) {\n\t\t\t\t\ti18nBundle = i18nBundle.get(bundle);\n\n\t\t\t\t\tif (!i18nBundle) {\n\t\t\t\t\t\treturn bundle;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn i18nBundle;\n\t\t\t}\n\t\t\treturn bundle;\n\t\t}\n\t}\n\n\treturn I18n;\n}\n\nexport default I18nMixin;\n"]}