{"ast":null,"code":"import _classCallCheck from \"@babel/runtime/helpers/classCallCheck\";\nimport _createClass from \"@babel/runtime/helpers/createClass\";\nimport _intlMessageformat from \"intl-messageformat\";\nimport _babelRuntimeHelpersEsmExtends from \"@babel/runtime/helpers/esm/extends\";\nexport var MessageDictionary = function () {\n  function MessageDictionary(messages, defaultLocale) {\n    _classCallCheck(this, MessageDictionary);\n\n    if (defaultLocale === void 0) {\n      defaultLocale = 'en-US';\n    }\n\n    this.messages = void 0;\n    this.defaultLocale = void 0;\n    this.messages = _babelRuntimeHelpersEsmExtends({}, messages);\n    this.defaultLocale = defaultLocale;\n  }\n\n  _createClass(MessageDictionary, [{\n    key: \"getStringForLocale\",\n    value: function getStringForLocale(key, locale) {\n      var strings = this.messages[locale];\n\n      if (!strings) {\n        strings = $e73469445899f72bd9d935bbafe76d4$var$getStringsForLocale(locale, this.messages, this.defaultLocale);\n        this.messages[locale] = strings;\n      }\n\n      var string = strings[key];\n\n      if (!string) {\n        throw new Error(\"Could not find intl message \" + key + \" in \" + locale + \" locale\");\n      }\n\n      return string;\n    }\n  }]);\n\n  return MessageDictionary;\n}();\n\nfunction $e73469445899f72bd9d935bbafe76d4$var$getStringsForLocale(locale, strings, defaultLocale) {\n  if (defaultLocale === void 0) {\n    defaultLocale = 'en-US';\n  }\n\n  if (strings[locale]) {\n    return strings[locale];\n  }\n\n  var language = $e73469445899f72bd9d935bbafe76d4$var$getLanguage(locale);\n\n  for (var key in strings) {\n    if (key.startsWith(language + '-')) {\n      return strings[key];\n    }\n  }\n\n  return strings[defaultLocale];\n}\n\nfunction $e73469445899f72bd9d935bbafe76d4$var$getLanguage(locale) {\n  if (Intl.Locale) {\n    return new Intl.Locale(locale).language;\n  }\n\n  return locale.split('-')[0];\n}\n\nexport var MessageFormatter = function () {\n  function MessageFormatter(locale, messages) {\n    _classCallCheck(this, MessageFormatter);\n\n    this.locale = void 0;\n    this.messages = void 0;\n    this.cache = void 0;\n    this.locale = locale;\n    this.messages = messages;\n    this.cache = {};\n  }\n\n  _createClass(MessageFormatter, [{\n    key: \"format\",\n    value: function format(key, variables) {\n      var message = this.cache[key];\n\n      if (!message) {\n        var msg = this.messages.getStringForLocale(key, this.locale);\n\n        if (!msg) {\n          throw new Error(\"Could not find intl message \" + key + \" in \" + this.locale + \" locale\");\n        }\n\n        message = new _intlMessageformat(msg, this.locale);\n        this.cache[key] = message;\n      }\n\n      return message.format(variables);\n    }\n  }]);\n\n  return MessageFormatter;\n}();","map":{"version":3,"sources":["packages/@internationalized/message/src/MessageDictionary.ts","packages/@internationalized/message/src/MessageFormatter.ts"],"names":["messages","defaultLocale","constructor","strings","getStringsForLocale","string","language","getLanguage","key","Intl","locale","cache","message","msg"],"mappings":";;;;AAsBO,WAAA,iBAAA;AAILE,6BAAW,QAAXA,EAAW,aAAXA,EAAyE;AAAA;;AAAA,QAAjCD,aAAiC,KAAA,KAAA,CAAA,EAAA;AAAjCA,MAAAA,aAAiC,GAAT,OAAxBA;AAAiC;;AAAA,SAHjED,QAGiE,GAAA,KAAA,CAAA;AAAA,SAFjEC,aAEiE,GAAA,KAAA,CAAA;AAEvE,SAAA,QAAA,GAAA,8BAAA,CAAA,EAAA,EAAA,QAAA,CAAA;AACA,SAAA,aAAA,GAAA,aAAA;AACD;;AARI;AAAA;AAAA,uCAUa,GAVb,EAUa,MAVb,EAU2C;AAC9C,UAAIE,OAAO,GAAG,KAAA,QAAA,CAAd,MAAc,CAAd;;AACA,UAAI,CAAJ,OAAA,EAAc;AACZA,QAAAA,OAAO,GAAGC,wDAAmB,CAAA,MAAA,EAAS,KAAT,QAAA,EAAwB,KAArDD,aAA6B,CAA7BA;AACA,aAAA,QAAA,CAAA,MAAA,IAAA,OAAA;AACD;;AAED,UAAIE,MAAM,GAAGF,OAAO,CAApB,GAAoB,CAApB;;AACA,UAAI,CAAJ,MAAA,EAAa;AACX,cAAM,IAAA,KAAA,CAAA,iCAAA,GAAA,GAAA,MAAA,GAAA,MAAA,GAAN,SAAM,CAAN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,KAAA,QAAA,CAAA,kBAAA,CAAA,GAAA,EAAsC,KAAhD,MAAU,CAAV;;AACA,YAAI,CAAJ,GAAA,EAAU;AACR,gBAAM,IAAA,KAAA,CAAA,iCAAA,GAAA,GAAA,MAAA,GAAmD,KAAnD,MAAA,GAAN,SAAM,CAAN;AACD;;AAEDD,QAAAA,OAAO,GAAG,IAAA,kBAAA,CAAA,GAAA,EAA2B,KAArCA,MAAU,CAAVA;AACA,aAAA,KAAA,CAAA,GAAA,IAAA,OAAA;AACD;;AAED,aAAOA,OAAO,CAAPA,MAAAA,CAAP,SAAOA,CAAP;AACD;AAxBI;;AAAA;AAAA","sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport type LocalizedStrings = {\n  [lang: string]: {\n    [key: string]: string\n  }\n};\n\n/**\n * Stores a mapping of localized strings. Can be used to find the\n * closest available string for a given locale.\n */\nexport class MessageDictionary {\n  private messages: LocalizedStrings;\n  private defaultLocale: string;\n\n  constructor(messages: LocalizedStrings, defaultLocale: string = 'en-US') {\n    // Clone messages so we don't modify the original object.\n    this.messages = {...messages};\n    this.defaultLocale = defaultLocale;\n  }\n\n  getStringForLocale(key: string, locale: string) {\n    let strings = this.messages[locale];\n    if (!strings) {\n      strings = getStringsForLocale(locale, this.messages, this.defaultLocale);\n      this.messages[locale] = strings;\n    }\n\n    let string = strings[key];\n    if (!string) {\n      throw new Error(`Could not find intl message ${key} in ${locale} locale`);\n    }\n\n    return string;\n  }\n}\n\nfunction getStringsForLocale(locale: string, strings: LocalizedStrings, defaultLocale = 'en-US') {\n  // If there is an exact match, use it.\n  if (strings[locale]) {\n    return strings[locale];\n  }\n\n  // Attempt to find the closest match by language.\n  // For example, if the locale is fr-CA (French Canadian), but there is only\n  // an fr-FR (France) set of strings, use that.\n  // This could be replaced with Intl.LocaleMatcher once it is supported.\n  // https://github.com/tc39/proposal-intl-localematcher\n  let language = getLanguage(locale);\n  for (let key in strings) {\n    if (key.startsWith(language + '-')) {\n      return strings[key];\n    }\n  }\n\n  // Nothing close, use english.\n  return strings[defaultLocale];\n}\n\nfunction getLanguage(locale: string) {\n  // @ts-ignore\n  if (Intl.Locale) {\n    // @ts-ignore\n    return new Intl.Locale(locale).language;\n  }\n\n  return locale.split('-')[0];\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {FormatXMLElementFn, PrimitiveType} from 'intl-messageformat/src/formatters';\nimport IntlMessageFormat from 'intl-messageformat';\nimport {MessageDictionary} from './MessageDictionary';\n\n/**\n * Formats ICU Message strings to create localized strings from a MessageDictionary.\n */\nexport class MessageFormatter {\n  private locale: string;\n  private messages: MessageDictionary;\n  private cache: {[key: string]: IntlMessageFormat};\n\n  constructor(locale: string, messages: MessageDictionary) {\n    this.locale = locale;\n    this.messages = messages;\n    this.cache = {};\n  }\n\n  format<T = void>(key: string, variables: Record<string, PrimitiveType | T | FormatXMLElementFn<T, string | T | (string | T)[]>> | undefined) {\n    let message = this.cache[key];\n    if (!message) {\n      let msg = this.messages.getStringForLocale(key, this.locale);\n      if (!msg) {\n        throw new Error(`Could not find intl message ${key} in ${this.locale} locale`);\n      }\n\n      message = new IntlMessageFormat(msg, this.locale);\n      this.cache[key] = message;\n    }\n\n    return message.format(variables);\n  }\n}\n"]},"metadata":{},"sourceType":"module"}