{"version":3,"names":["LanguageKeys","languageLocalization","element","parent","parentNode","host","langAttributeAvailable","hasAttribute","setCurrentLocalizationDelegate","component","closestElementWithLangAttr","hostElement","closest","locale","lang","indexOf","split","Object","values","includes","currentLocalization","localization"],"sources":["src/common/localizable-component.ts"],"sourcesContent":["/**\n * Forces all LocalizableComponents to provide ComponentLocalizations for the specified languages.\n * If you ever need to add support for a new language, do it here!\n */\nexport interface Localization {\n /**\n * English\n */\n en: ComponentLocalization;\n\n /**\n * German\n */\n de: ComponentLocalization;\n\n /**\n * Swedish\n */\n sv: ComponentLocalization;\n\n /**\n * Dutch locale for the Netherlands\n */\n nl: ComponentLocalization;\n\n /**\n * Italian\n */\n it: ComponentLocalization;\n\n /**\n * Hungarian\n */\n hu: ComponentLocalization;\n\n /**\n * Danish\n */\n da: ComponentLocalization;\n\n /**\n * French\n */\n fr: ComponentLocalization;\n\n /**\n * Spanish\n */\n es: ComponentLocalization;\n\n /**\n * Polish\n */\n pl: ComponentLocalization;\n\n /**\n * Romanian\n */\n ro: ComponentLocalization;\n\n /**\n * Croatian\n */\n hr: ComponentLocalization;\n\n /**\n * Czech\n */\n cs: ComponentLocalization;\n\n /**\n * Slovenian\n */\n sl: ComponentLocalization;\n}\n\n/**\n * Keep in sync with the above defined interface. These keys will be checked on runtime by setCurrentLocalizationDelegate() function.\n */\nexport enum LanguageKeys {\n en,\n de,\n sv,\n nl,\n it,\n hu,\n da,\n fr,\n es,\n pl,\n ro,\n hr,\n cs,\n sl\n}\n\n/**\n * Implement this interface if your component has hard-coded localizable strings (i18n).\n * @example See the Link component\n */\nexport interface LocalizableComponent {\n /**\n * The host element of the custom component. Should be annotated with the Stencil decorator @Element().\n * @example @Element() hostElement: HTMLEonUiMyComponentElement;\n */\n hostElement;\n\n /**\n * The localization of the component for all languages.\n */\n localization: Localization;\n\n /**\n * The current localization based on the closest lang attribute.\n * Use this to set translated strings in your component.\n */\n currentLocalization: ComponentLocalization;\n\n /**\n * Determines the current language context of the component and updates the currentLocalization property.\n *\n * @default\n * setCurrentLocalization() {\n * setCurrentLocalizationDelegate(this);\n * }\n */\n setCurrentLocalization(): void;\n}\n\nconst languageLocalization = (element: HTMLElement) => {\n const parent = element && ((element.parentNode as HTMLElement) || ((element as any).host as HTMLElement));\n let langAttributeAvailable = parent && typeof parent.hasAttribute === \"function\" && parent.hasAttribute(\"lang\");\n\n switch (langAttributeAvailable) {\n case true:\n return parent;\n case false:\n return languageLocalization(parent);\n default:\n break;\n }\n};\n\n/**\n * Default implementation for setCurrentLocalization().\n * Determines the current language context of the component based on its own or the nearest enclosing [lang] attribute set on any DOM element.\n * If there is none, it uses \"en\" (English) as fallback language.\n * Finally updates the currentLocalization property with the appropriate values taken from the localization of the component.\n * @param component A component implementing LocalizableComponent.\n */\nexport function setCurrentLocalizationDelegate(\n component: LocalizableComponent\n): void {\n let closestElementWithLangAttr = component.hostElement.closest(\"[lang]\") as HTMLElement;\n if (closestElementWithLangAttr === null) {\n closestElementWithLangAttr = languageLocalization(component.hostElement);\n }\n\n let locale = \"en\"; // fallback language\n\n if (closestElementWithLangAttr && closestElementWithLangAttr.lang) {\n const lang = closestElementWithLangAttr.lang.indexOf(\"-\")\n ? closestElementWithLangAttr.lang.split(\"-\")[0]\n : closestElementWithLangAttr.lang;\n\n if (Object.values(LanguageKeys).includes(lang)) locale = lang;\n }\n\n component.currentLocalization = component.localization[locale];\n}\n\nexport interface DefaultLocalization {\n required: string;\n}\n"],"mappings":"AA+EA,IAAYA,GAAZ,SAAYA,GACVA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,cACAA,IAAA,eACAA,IAAA,eACAA,IAAA,eACAA,IAAA,cACD,EAfD,CAAYA,MAAY,KAkDxB,MAAMC,EAAwBC,IAC5B,MAAMC,EAASD,IAAaA,EAAQE,YAAgCF,EAAgBG,MACpF,IAAIC,EAAyBH,UAAiBA,EAAOI,eAAiB,YAAcJ,EAAOI,aAAa,QAExG,OAAQD,GACN,KAAK,KACH,OAAOH,EACT,KAAK,MACH,OAAOF,EAAqBE,G,WAalBK,EACdC,GAEA,IAAIC,EAA6BD,EAAUE,YAAYC,QAAQ,UAC/D,GAAIF,IAA+B,KAAM,CACvCA,EAA6BT,EAAqBQ,EAAUE,Y,CAG9D,IAAIE,EAAS,KAEb,GAAIH,GAA8BA,EAA2BI,KAAM,CACjE,MAAMA,EAAOJ,EAA2BI,KAAKC,QAAQ,KACjDL,EAA2BI,KAAKE,MAAM,KAAK,GAC3CN,EAA2BI,KAE/B,GAAIG,OAAOC,OAAOlB,GAAcmB,SAASL,GAAOD,EAASC,C,CAG3DL,EAAUW,oBAAsBX,EAAUY,aAAaR,EACzD,Q"}