{"version":3,"file":"index.cjs","names":["LocaleManager","RawBundleSource","enUS","zhCN","nlNL","frFR","deDE","itIT","jaJP","ptBR","ruRU","esLA"],"sources":["../../localization/index.js"],"sourcesContent":["import { LocaleManager, RawBundleSource } from '@dialpad/i18n';\n\nimport enUS from './en-US.ftl?raw';\nimport zhCN from './zh-CN.ftl?raw';\nimport nlNL from './nl-NL.ftl?raw';\nimport frFR from './fr-FR.ftl?raw';\nimport deDE from './de-DE.ftl?raw';\nimport itIT from './it-IT.ftl?raw';\nimport jaJP from './ja-JP.ftl?raw';\nimport ptBR from './pt-BR.ftl?raw';\nimport ruRU from './ru-RU.ftl?raw';\nimport esLA from './es-LA.ftl?raw';\n\nconst dialtoneNamespace = 'dialtone';\nconst allowedLocales = {\n  ENGLISH: 'en-US',\n  CHINESE: 'zh-CN',\n  DUTCH: 'nl-NL',\n  FRENCH: 'fr-FR',\n  GERMAN: 'de-DE',\n  ITALIAN: 'it-IT',\n  JAPANESE: 'ja-JP',\n  PORTUGUESE: 'pt-BR',\n  RUSSIAN: 'ru-RU',\n  SPANISH: 'es-LA',\n};\nconst fallbackLocale = 'en-US';\n/**\n * Default key name used by the LocaleManager to store the user's preferred locale in localStorage\n */\nconst localeManagerStorageKey = 'user-locale';\n\n/**\n * Dialtone localization class, follows the singleton pattern to make sure only one instance of the class is created.\n * Initializes the localeManager and looks for changes on the browser storage to update the current locale.\n * https://github.com/dialpad/goblin-client-tools/tree/main/packages/i18n#i18n-vue-3-compatible\n */\nexport class DialtoneLocalization {\n  /**\n   * @prop {import('@dialpad/i18n').UseI18N} i18n\n   * @private\n   */\n  constructor (locale = null) {\n    if (typeof DialtoneLocalization.instance === 'object') {\n      return DialtoneLocalization.instance;\n    }\n\n    this._locale = locale || DialtoneLocalization.getPreferredLocale();\n\n    const bundleSource = new RawBundleSource({\n      resources: RawBundleSource.builtResources([\n        ['en-US', dialtoneNamespace, enUS],\n        ['zh-CN', dialtoneNamespace, zhCN],\n        ['nl-NL', dialtoneNamespace, nlNL],\n        ['fr-FR', dialtoneNamespace, frFR],\n        ['de-DE', dialtoneNamespace, deDE],\n        ['it-IT', dialtoneNamespace, itIT],\n        ['ja-JP', dialtoneNamespace, jaJP],\n        ['pt-BR', dialtoneNamespace, ptBR],\n        ['ru-RU', dialtoneNamespace, ruRU],\n        ['es-LA', dialtoneNamespace, esLA],\n      ]),\n    });\n\n    const localeManager = new LocaleManager({\n      bundleSource,\n      allowedLocales: Object.values(allowedLocales),\n      fallbackLocale,\n      preferredLocale: this._locale,\n      namespaces: [dialtoneNamespace],\n    });\n\n    DialtoneLocalization.instance = this;\n    this.i18n = localeManager.useI18N(dialtoneNamespace);\n\n    if (typeof window !== 'undefined') {\n      /**\n       * @description\n       * When the browser storage changes, update the current locale\n       * @param event\n       */\n      window.onstorage = (event) => {\n        if (event.key === localeManagerStorageKey) {\n          this.currentLocale = event.newValue;\n        }\n      };\n    }\n\n    return this;\n  }\n\n  /**\n   * Gets the preferred locale from user's locale stored in localStorage or the browser language\n   * @returns { string }\n   */\n  static getPreferredLocale () {\n    /**\n     * Early return if we're not in the browser or if localStorage is not available\n     */\n    if (typeof window === 'undefined' || !window.localStorage) {\n      return fallbackLocale;\n    }\n\n    const localStorageLanguage = window.localStorage.getItem(localeManagerStorageKey);\n\n    // Get the first two letters of the navigator language and check if it's in the allowed locales\n    const navigatorLanguage = Object.values(allowedLocales)\n      .find(locale => locale.startsWith(navigator.language.slice(0, 2)));\n\n    return localStorageLanguage || navigatorLanguage || fallbackLocale;\n  }\n\n  static getAllowedLocales () {\n    return allowedLocales;\n  }\n\n  /**\n   * Passthrough function to the i18n $t function including the dialtone namespace\n   * Returns a translated string based on a key and optional variables.\n   * It’s used for simple text translations.\n   * https://github.com/dialpad/goblin-client-tools/tree/main/packages/i18n#t\n   */\n  $t (...args) {\n    return this.i18n.$t(...args);\n  }\n\n  /**\n   * Passthrough function to the i18n $ta function including the dialtone namespace\n   * Returns an object containing translated attributes to pass directly as props to components, it can contain\n   * aria-label, title, etc, rather than just a plain text. It’s useful for handling element attributes in the UI.\n   * https://github.com/dialpad/goblin-client-tools/tree/main/packages/i18n#ta\n   */\n  $ta (...args) {\n    return this.i18n.$ta(...args);\n  }\n\n  get currentLocale () {\n    return this._locale;\n  }\n\n  set currentLocale (newLocale) {\n    if (newLocale === this._locale) return;\n    if (!Object.values(allowedLocales).includes(newLocale)) {\n      throw new Error(`Locale ${newLocale} is not allowed, please use one of the following: ${Object.values(allowedLocales).join(', ')}`);\n    }\n\n    this._locale = newLocale;\n    this.i18n.setI18N({ preferredLocale: newLocale }, dialtoneNamespace);\n  }\n}\n"],"mappings":"mYAaA,IAAM,EAAoB,WACpB,EAAiB,CACrB,QAAS,QACT,QAAS,QACT,MAAO,QACP,OAAQ,QACR,OAAQ,QACR,QAAS,QACT,SAAU,QACV,WAAY,QACZ,QAAS,QACT,QAAS,QACV,CACK,EAAiB,QAIjB,EAA0B,cAOnB,EAAb,MAAa,CAAqB,CAKhC,YAAa,EAAS,KAAM,CAC1B,GAAI,OAAO,EAAqB,UAAa,SAC3C,OAAO,EAAqB,SAG9B,KAAK,QAAU,GAAU,EAAqB,oBAAoB,CAiBlE,IAAM,EAAgB,IAAIA,EAAAA,cAAc,CACtC,aAhBmB,IAAIC,EAAAA,gBAAgB,CACvC,UAAWA,EAAAA,gBAAgB,eAAe,CACxC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CAClC,CAAC,QAAS,EAAmBC,EAAAA,QAAK,CACnC,C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