@uifabric/utilities
Version:
Fluent UI React utilities for building components.
1 lines • 3.95 kB
Source Map (JSON)
{"version":3,"file":"language.js","sourceRoot":"../src/","sources":["language.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,YAAY,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,cAAc,MAAM,kBAAkB,CAAC;AAEnD,4DAA4D;AAC5D,IAAI,SAAwB,CAAC;AAE7B,IAAM,WAAW,GAAG,UAAU,CAAC;AAE/B;;;;GAIG;AACH,MAAM,UAAU,WAAW,CACzB,eAA4E;IAA5E,gCAAA,EAAA,gCAA4E;IAE5E,IAAI,SAAS,KAAK,SAAS,EAAE;QAC3B,IAAI,GAAG,GAAG,WAAW,EAAE,CAAC;QACxB,IAAM,aAAa,GACjB,eAAe,KAAK,cAAc;YAChC,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC,WAAW,CAAC;YACnC,CAAC,CAAC,eAAe,KAAK,gBAAgB;gBACtC,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,WAAW,CAAC;gBACrC,CAAC,CAAC,SAAS,CAAC;QAEhB,IAAI,aAAa,EAAE;YACjB,SAAS,GAAG,aAAa,CAAC;SAC3B;QAED,IAAI,SAAS,KAAK,SAAS,IAAI,GAAG,EAAE;YAClC,SAAS,GAAG,GAAG,CAAC,eAAe,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;SACtD;QAED,IAAI,SAAS,KAAK,SAAS,EAAE;YAC3B,SAAS,GAAG,IAAI,CAAC;SAClB;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAgBD,MAAM,UAAU,WAAW,CACzB,QAAgB,EAChB,gBAAuE;IAEvE,IAAI,GAAG,GAAG,WAAW,EAAE,CAAC;IAExB,IAAI,GAAG,EAAE;QACP,GAAG,CAAC,eAAe,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;KACpD;IAED,IAAM,eAAe,GAAG,gBAAgB,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,gBAAgB,CAAC;IACnH,IAAI,eAAe,KAAK,cAAc,EAAE;QACtC,YAAY,CAAC,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;KAC7C;SAAM,IAAI,eAAe,KAAK,gBAAgB,EAAE;QAC/C,cAAc,CAAC,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;KAC/C;IAED,SAAS,GAAG,QAAQ,CAAC;AACvB,CAAC","sourcesContent":["import { getDocument } from './dom/getDocument';\nimport * as localStorage from './localStorage';\nimport * as sessionStorage from './sessionStorage';\n\n// Default to undefined so that we initialize on first read.\nlet _language: string | null;\n\nconst STORAGE_KEY = 'language';\n\n/**\n * Gets the language set for the page.\n * @param persistenceType - Where to persist the value. Default is `localStorage` if available.\n * (In version 8, the default will be `sessionStorage`.)\n */\nexport function getLanguage(\n persistenceType: 'localStorage' | 'sessionStorage' | 'none' = 'localStorage',\n): string | null {\n if (_language === undefined) {\n let doc = getDocument();\n const savedLanguage =\n persistenceType === 'localStorage'\n ? localStorage.getItem(STORAGE_KEY)\n : persistenceType === 'sessionStorage'\n ? sessionStorage.getItem(STORAGE_KEY)\n : undefined;\n\n if (savedLanguage) {\n _language = savedLanguage;\n }\n\n if (_language === undefined && doc) {\n _language = doc.documentElement.getAttribute('lang');\n }\n\n if (_language === undefined) {\n _language = 'en';\n }\n }\n\n return _language;\n}\n\n/**\n * Sets the language for the page (by adjusting the lang attribute of the html element).\n * @param language - Language to set.\n * @param persistenceType - Where to persist the value. Default is `localStorage` if available.\n * (In version 8, the default will be `sessionStorage`.)\n */\nexport function setLanguage(language: string, persistenceType?: 'localStorage' | 'sessionStorage' | 'none'): void;\n/**\n * Sets the language for the page (by adjusting the lang attribute of the html element).\n * @deprecated Use string parameter version.\n * @param language - Language to set.\n * @param avoidPersisting - If true, don't store the value.\n */\nexport function setLanguage(language: string, avoidPersisting?: boolean): void;\nexport function setLanguage(\n language: string,\n persistenceParam?: 'localStorage' | 'sessionStorage' | 'none' | boolean,\n): void {\n let doc = getDocument();\n\n if (doc) {\n doc.documentElement.setAttribute('lang', language);\n }\n\n const persistenceType = persistenceParam === true ? 'none' : !persistenceParam ? 'localStorage' : persistenceParam;\n if (persistenceType === 'localStorage') {\n localStorage.setItem(STORAGE_KEY, language);\n } else if (persistenceType === 'sessionStorage') {\n sessionStorage.setItem(STORAGE_KEY, language);\n }\n\n _language = language;\n}\n"]}