gt-react-native 10.19.19 → 10.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  require("./_virtual/_rolldown/runtime.js");
3
+ const require_utils_nativeStore = require("./utils/nativeStore.js");
3
4
  const require_provider_GTProvider = require("./provider/GTProvider.js");
4
5
  let _generaltranslation_react_core = require("@generaltranslation/react-core");
5
6
  exports.Branch = _generaltranslation_react_core.Branch;
@@ -16,6 +17,7 @@ exports.declareVar = _generaltranslation_react_core.declareVar;
16
17
  exports.decodeMsg = _generaltranslation_react_core.decodeMsg;
17
18
  exports.decodeOptions = _generaltranslation_react_core.decodeOptions;
18
19
  exports.decodeVars = _generaltranslation_react_core.decodeVars;
20
+ exports.getLocaleFromNativeStore = require_utils_nativeStore.getLocaleFromNativeStore;
19
21
  exports.gtFallback = _generaltranslation_react_core.gtFallback;
20
22
  exports.mFallback = _generaltranslation_react_core.mFallback;
21
23
  exports.msg = _generaltranslation_react_core.msg;
@@ -5,10 +5,11 @@ const require_utils_getNativeLocales = require("../../../utils/getNativeLocales.
5
5
  const require_utils_nativeStore = require("../../../utils/nativeStore.js");
6
6
  let react = require("react");
7
7
  let _generaltranslation_format = require("@generaltranslation/format");
8
+ let _generaltranslation_react_core_internal = require("@generaltranslation/react-core/internal");
8
9
  let generaltranslation_internal = require("generaltranslation/internal");
9
10
  let _generaltranslation_react_core_errors = require("@generaltranslation/react-core/errors");
10
11
  //#region src/provider/hooks/locale/useDetermineLocale.ts
11
- function useDetermineLocale({ locale: _locale = "", defaultLocale = generaltranslation_internal.libraryDefaultLocale, locales = [], localeCookieName = "generaltranslation.locale", ssr = false, customMapping, enableI18n }) {
12
+ function useDetermineLocale({ locale: _locale = "", defaultLocale = generaltranslation_internal.libraryDefaultLocale, locales = [], localeCookieName = _generaltranslation_react_core_internal.defaultLocaleCookieName, ssr = false, customMapping, enableI18n }) {
12
13
  _locale = (0, _generaltranslation_format.resolveAliasLocale)(_locale, customMapping);
13
14
  locales = locales.map((locale) => (0, _generaltranslation_format.resolveAliasLocale)(locale, customMapping));
14
15
  const initializeLocale = () => {
@@ -1 +1 @@
1
- {"version":3,"file":"useDetermineLocale.js","names":["libraryDefaultLocale","nativeStoreGet","getNativeLocales","PACKAGE_NAME"],"sources":["../../../../../src/provider/hooks/locale/useDetermineLocale.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport type { Dispatch, SetStateAction } from 'react';\nimport {\n determineLocale,\n resolveAliasLocale,\n} from '@generaltranslation/format';\nimport { libraryDefaultLocale } from 'generaltranslation/internal';\nimport type { CustomMapping } from '@generaltranslation/format/types';\nimport type {\n UseDetermineLocaleParams,\n UseDetermineLocaleReturn,\n} from '@generaltranslation/react-core/types';\nimport { createUnsupportedLocaleWarning } from '@generaltranslation/react-core/errors';\nimport { PACKAGE_NAME } from '../../../errors-dir/constants';\nimport { getNativeLocales } from '../../../utils/getNativeLocales';\nimport { nativeStoreGet, nativeStoreSet } from '../../../utils/nativeStore';\n\nexport function useDetermineLocale({\n locale: _locale = '',\n defaultLocale = libraryDefaultLocale,\n locales = [],\n localeCookieName = 'generaltranslation.locale',\n ssr = false, // not relevant\n customMapping,\n enableI18n,\n}: UseDetermineLocaleParams): UseDetermineLocaleReturn {\n // resolve alias locale\n _locale = resolveAliasLocale(_locale, customMapping);\n locales = locales.map((locale) => resolveAliasLocale(locale, customMapping));\n\n const initializeLocale = () => {\n if (!enableI18n) {\n return defaultLocale;\n }\n const result = resolveAliasLocale(\n ssr\n ? _locale\n ? determineLocale(_locale, locales, customMapping) || ''\n : ''\n : getNewLocale({\n _locale,\n locale: _locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n }),\n customMapping\n );\n return result;\n };\n\n // maintaining the state of locale\n const [locale, _setLocale] = useState<string>(initializeLocale());\n\n // Functions for setting internal locale state\n const [setLocale, setLocaleWithoutPersist] = createSetLocale({\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n _setLocale,\n customMapping,\n enableI18n,\n });\n\n // check browser for locales\n useEffect(() => {\n const newLocale = getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n });\n setLocaleWithoutPersist(newLocale);\n }, [\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n ]);\n\n return [locale, setLocale];\n}\n\n// ----- HELPER FUNCTIONS ---- //\n\n/**\n * Choose a locale to use\n * (1) use provided locale\n * (2) use cookie locale\n * (3) use preferred locale\n * (5) fallback to defaultLocale\n * Update the cookie locale to be correct\n */\nfunction getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n _locale: string;\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): string {\n if (!enableI18n) {\n return defaultLocale;\n }\n // No change, return\n if (\n _locale &&\n _locale === locale &&\n determineLocale(_locale, locales, customMapping) === locale\n ) {\n return resolveAliasLocale(_locale, customMapping);\n }\n\n // Check for locale in native store\n let storedLocale = nativeStoreGet(localeCookieName);\n if (storedLocale) {\n storedLocale = resolveAliasLocale(storedLocale, customMapping);\n }\n\n // check user's configured locales\n let preferredLocales = getNativeLocales();\n if (preferredLocales.length === 0) preferredLocales = [defaultLocale];\n preferredLocales = preferredLocales.map((l) =>\n resolveAliasLocale(l, customMapping)\n );\n\n // determine locale\n let newLocale =\n determineLocale(\n [\n ...(_locale ? [_locale] : []), // prefer user passed locale\n ...(storedLocale ? [storedLocale] : []), // then prefer stored locale\n ...preferredLocales, // then prefer browser locale\n ],\n locales,\n customMapping\n ) || defaultLocale;\n if (newLocale) {\n newLocale = resolveAliasLocale(newLocale, customMapping);\n }\n\n // if stored locale not valid, change it back to whatever we use for fallback\n if (storedLocale && storedLocale !== newLocale) {\n nativeStoreSet(localeCookieName, newLocale);\n }\n\n // return new locale\n return newLocale;\n}\n\nfunction createSetLocale({\n locale,\n locales,\n defaultLocale,\n _setLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n _setLocale: Dispatch<SetStateAction<string>>;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): [(newLocale: string) => void, (newLocale: string) => void] {\n locale = resolveAliasLocale(locale, customMapping);\n // Just update the internal state, don't persist it\n const setLocaleWithoutPersist = (newLocale: string): string => {\n if (!enableI18n) {\n return defaultLocale;\n }\n // validate locale\n const validatedLocale = resolveAliasLocale(\n determineLocale(newLocale, locales, customMapping) ||\n locale ||\n defaultLocale,\n customMapping\n );\n if (validatedLocale !== newLocale) {\n console.warn(\n createUnsupportedLocaleWarning(validatedLocale, newLocale, PACKAGE_NAME)\n );\n }\n // persist locale\n _setLocale(validatedLocale);\n\n return validatedLocale;\n };\n // update locale and persist it in native store\n const setLocale = (newLocale: string): void => {\n if (!enableI18n) {\n return;\n }\n newLocale = resolveAliasLocale(newLocale, customMapping);\n const validatedLocale = setLocaleWithoutPersist(newLocale);\n nativeStoreSet(localeCookieName, validatedLocale);\n };\n return [setLocale, setLocaleWithoutPersist];\n}\n"],"mappings":";;;;;;;;;;AAiBA,SAAgB,mBAAmB,EACjC,QAAQ,UAAU,IAClB,gBAAgBA,4BAAAA,sBAChB,UAAU,EAAE,EACZ,mBAAmB,6BACnB,MAAM,OACN,eACA,cACqD;AAErD,YAAA,GAAA,2BAAA,oBAA6B,SAAS,cAAc;AACpD,WAAU,QAAQ,KAAK,YAAA,GAAA,2BAAA,oBAA8B,QAAQ,cAAc,CAAC;CAE5E,MAAM,yBAAyB;AAC7B,MAAI,CAAC,WACH,QAAO;AAkBT,UAAA,GAAA,2BAAA,oBAfE,MACI,WAAA,GAAA,2BAAA,iBACkB,SAAS,SAAS,cAAc,IAAI,KACpD,KACF,aAAa;GACX;GACA,QAAQ;GACR;GACA;GACA;GACA;GACA;GACD,CAAC,EACN,cAEW;;CAIf,MAAM,CAAC,QAAQ,eAAA,GAAA,MAAA,UAA+B,kBAAkB,CAAC;CAGjE,MAAM,CAAC,WAAW,2BAA2B,gBAAgB;EAC3D;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAGF,EAAA,GAAA,MAAA,iBAAgB;AAUd,0BATkB,aAAa;GAC7B;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CACgC,CAAC;IACjC;EACD;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAEF,QAAO,CAAC,QAAQ,UAAU;;;;;;;;;;AAa5B,SAAS,aAAa,EACpB,SACA,QACA,SACA,eACA,kBACA,eACA,cASS;AACT,KAAI,CAAC,WACH,QAAO;AAGT,KACE,WACA,YAAY,WAAA,GAAA,2BAAA,iBACI,SAAS,SAAS,cAAc,KAAK,OAErD,SAAA,GAAA,2BAAA,oBAA0B,SAAS,cAAc;CAInD,IAAI,eAAeC,0BAAAA,eAAe,iBAAiB;AACnD,KAAI,aACF,iBAAA,GAAA,2BAAA,oBAAkC,cAAc,cAAc;CAIhE,IAAI,mBAAmBC,+BAAAA,kBAAkB;AACzC,KAAI,iBAAiB,WAAW,EAAG,oBAAmB,CAAC,cAAc;AACrE,oBAAmB,iBAAiB,KAAK,OAAA,GAAA,2BAAA,oBACpB,GAAG,cAAc,CACrC;CAGD,IAAI,aAAA,GAAA,2BAAA,iBAEA;EACE,GAAI,UAAU,CAAC,QAAQ,GAAG,EAAE;EAC5B,GAAI,eAAe,CAAC,aAAa,GAAG,EAAE;EACtC,GAAG;EACJ,EACD,SACA,cACD,IAAI;AACP,KAAI,UACF,cAAA,GAAA,2BAAA,oBAA+B,WAAW,cAAc;AAI1D,KAAI,gBAAgB,iBAAiB,UACnC,2BAAA,eAAe,kBAAkB,UAAU;AAI7C,QAAO;;AAGT,SAAS,gBAAgB,EACvB,QACA,SACA,eACA,YACA,kBACA,eACA,cAS6D;AAC7D,WAAA,GAAA,2BAAA,oBAA4B,QAAQ,cAAc;CAElD,MAAM,2BAA2B,cAA8B;AAC7D,MAAI,CAAC,WACH,QAAO;EAGT,MAAM,mBAAA,GAAA,2BAAA,qBAAA,GAAA,2BAAA,iBACY,WAAW,SAAS,cAAc,IAChD,UACA,eACF,cACD;AACD,MAAI,oBAAoB,UACtB,SAAQ,MAAA,GAAA,sCAAA,gCACyB,iBAAiB,WAAWC,6BAAAA,aAAa,CACzE;AAGH,aAAW,gBAAgB;AAE3B,SAAO;;CAGT,MAAM,aAAa,cAA4B;AAC7C,MAAI,CAAC,WACH;AAEF,eAAA,GAAA,2BAAA,oBAA+B,WAAW,cAAc;AAExD,4BAAA,eAAe,kBADS,wBAAwB,UACA,CAAC;;AAEnD,QAAO,CAAC,WAAW,wBAAwB"}
1
+ {"version":3,"file":"useDetermineLocale.js","names":["libraryDefaultLocale","defaultLocaleCookieName","nativeStoreGet","getNativeLocales","PACKAGE_NAME"],"sources":["../../../../../src/provider/hooks/locale/useDetermineLocale.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport type { Dispatch, SetStateAction } from 'react';\nimport {\n determineLocale,\n resolveAliasLocale,\n} from '@generaltranslation/format';\nimport { defaultLocaleCookieName } from '@generaltranslation/react-core/internal';\nimport { libraryDefaultLocale } from 'generaltranslation/internal';\nimport type { CustomMapping } from '@generaltranslation/format/types';\nimport type {\n UseDetermineLocaleParams,\n UseDetermineLocaleReturn,\n} from '@generaltranslation/react-core/types';\nimport { createUnsupportedLocaleWarning } from '@generaltranslation/react-core/errors';\nimport { PACKAGE_NAME } from '../../../errors-dir/constants';\nimport { getNativeLocales } from '../../../utils/getNativeLocales';\nimport { nativeStoreGet, nativeStoreSet } from '../../../utils/nativeStore';\n\nexport function useDetermineLocale({\n locale: _locale = '',\n defaultLocale = libraryDefaultLocale,\n locales = [],\n localeCookieName = defaultLocaleCookieName,\n ssr = false, // not relevant\n customMapping,\n enableI18n,\n}: UseDetermineLocaleParams): UseDetermineLocaleReturn {\n // resolve alias locale\n _locale = resolveAliasLocale(_locale, customMapping);\n locales = locales.map((locale) => resolveAliasLocale(locale, customMapping));\n\n const initializeLocale = () => {\n if (!enableI18n) {\n return defaultLocale;\n }\n const result = resolveAliasLocale(\n ssr\n ? _locale\n ? determineLocale(_locale, locales, customMapping) || ''\n : ''\n : getNewLocale({\n _locale,\n locale: _locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n }),\n customMapping\n );\n return result;\n };\n\n // maintaining the state of locale\n const [locale, _setLocale] = useState<string>(initializeLocale());\n\n // Functions for setting internal locale state\n const [setLocale, setLocaleWithoutPersist] = createSetLocale({\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n _setLocale,\n customMapping,\n enableI18n,\n });\n\n // check browser for locales\n useEffect(() => {\n const newLocale = getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n });\n setLocaleWithoutPersist(newLocale);\n }, [\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n ]);\n\n return [locale, setLocale];\n}\n\n// ----- HELPER FUNCTIONS ---- //\n\n/**\n * Choose a locale to use\n * (1) use provided locale\n * (2) use cookie locale\n * (3) use preferred locale\n * (5) fallback to defaultLocale\n * Update the cookie locale to be correct\n */\nfunction getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n _locale: string;\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): string {\n if (!enableI18n) {\n return defaultLocale;\n }\n // No change, return\n if (\n _locale &&\n _locale === locale &&\n determineLocale(_locale, locales, customMapping) === locale\n ) {\n return resolveAliasLocale(_locale, customMapping);\n }\n\n // Check for locale in native store\n let storedLocale = nativeStoreGet(localeCookieName);\n if (storedLocale) {\n storedLocale = resolveAliasLocale(storedLocale, customMapping);\n }\n\n // check user's configured locales\n let preferredLocales = getNativeLocales();\n if (preferredLocales.length === 0) preferredLocales = [defaultLocale];\n preferredLocales = preferredLocales.map((l) =>\n resolveAliasLocale(l, customMapping)\n );\n\n // determine locale\n let newLocale =\n determineLocale(\n [\n ...(_locale ? [_locale] : []), // prefer user passed locale\n ...(storedLocale ? [storedLocale] : []), // then prefer stored locale\n ...preferredLocales, // then prefer browser locale\n ],\n locales,\n customMapping\n ) || defaultLocale;\n if (newLocale) {\n newLocale = resolveAliasLocale(newLocale, customMapping);\n }\n\n // if stored locale not valid, change it back to whatever we use for fallback\n if (storedLocale && storedLocale !== newLocale) {\n nativeStoreSet(localeCookieName, newLocale);\n }\n\n // return new locale\n return newLocale;\n}\n\nfunction createSetLocale({\n locale,\n locales,\n defaultLocale,\n _setLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n _setLocale: Dispatch<SetStateAction<string>>;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): [(newLocale: string) => void, (newLocale: string) => void] {\n locale = resolveAliasLocale(locale, customMapping);\n // Just update the internal state, don't persist it\n const setLocaleWithoutPersist = (newLocale: string): string => {\n if (!enableI18n) {\n return defaultLocale;\n }\n // validate locale\n const validatedLocale = resolveAliasLocale(\n determineLocale(newLocale, locales, customMapping) ||\n locale ||\n defaultLocale,\n customMapping\n );\n if (validatedLocale !== newLocale) {\n console.warn(\n createUnsupportedLocaleWarning(validatedLocale, newLocale, PACKAGE_NAME)\n );\n }\n // persist locale\n _setLocale(validatedLocale);\n\n return validatedLocale;\n };\n // update locale and persist it in native store\n const setLocale = (newLocale: string): void => {\n if (!enableI18n) {\n return;\n }\n newLocale = resolveAliasLocale(newLocale, customMapping);\n const validatedLocale = setLocaleWithoutPersist(newLocale);\n nativeStoreSet(localeCookieName, validatedLocale);\n };\n return [setLocale, setLocaleWithoutPersist];\n}\n"],"mappings":";;;;;;;;;;;AAkBA,SAAgB,mBAAmB,EACjC,QAAQ,UAAU,IAClB,gBAAgBA,4BAAAA,sBAChB,UAAU,EAAE,EACZ,mBAAmBC,wCAAAA,yBACnB,MAAM,OACN,eACA,cACqD;AAErD,YAAA,GAAA,2BAAA,oBAA6B,SAAS,cAAc;AACpD,WAAU,QAAQ,KAAK,YAAA,GAAA,2BAAA,oBAA8B,QAAQ,cAAc,CAAC;CAE5E,MAAM,yBAAyB;AAC7B,MAAI,CAAC,WACH,QAAO;AAkBT,UAAA,GAAA,2BAAA,oBAfE,MACI,WAAA,GAAA,2BAAA,iBACkB,SAAS,SAAS,cAAc,IAAI,KACpD,KACF,aAAa;GACX;GACA,QAAQ;GACR;GACA;GACA;GACA;GACA;GACD,CAAC,EACN,cAEW;;CAIf,MAAM,CAAC,QAAQ,eAAA,GAAA,MAAA,UAA+B,kBAAkB,CAAC;CAGjE,MAAM,CAAC,WAAW,2BAA2B,gBAAgB;EAC3D;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAGF,EAAA,GAAA,MAAA,iBAAgB;AAUd,0BATkB,aAAa;GAC7B;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CACgC,CAAC;IACjC;EACD;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAEF,QAAO,CAAC,QAAQ,UAAU;;;;;;;;;;AAa5B,SAAS,aAAa,EACpB,SACA,QACA,SACA,eACA,kBACA,eACA,cASS;AACT,KAAI,CAAC,WACH,QAAO;AAGT,KACE,WACA,YAAY,WAAA,GAAA,2BAAA,iBACI,SAAS,SAAS,cAAc,KAAK,OAErD,SAAA,GAAA,2BAAA,oBAA0B,SAAS,cAAc;CAInD,IAAI,eAAeC,0BAAAA,eAAe,iBAAiB;AACnD,KAAI,aACF,iBAAA,GAAA,2BAAA,oBAAkC,cAAc,cAAc;CAIhE,IAAI,mBAAmBC,+BAAAA,kBAAkB;AACzC,KAAI,iBAAiB,WAAW,EAAG,oBAAmB,CAAC,cAAc;AACrE,oBAAmB,iBAAiB,KAAK,OAAA,GAAA,2BAAA,oBACpB,GAAG,cAAc,CACrC;CAGD,IAAI,aAAA,GAAA,2BAAA,iBAEA;EACE,GAAI,UAAU,CAAC,QAAQ,GAAG,EAAE;EAC5B,GAAI,eAAe,CAAC,aAAa,GAAG,EAAE;EACtC,GAAG;EACJ,EACD,SACA,cACD,IAAI;AACP,KAAI,UACF,cAAA,GAAA,2BAAA,oBAA+B,WAAW,cAAc;AAI1D,KAAI,gBAAgB,iBAAiB,UACnC,2BAAA,eAAe,kBAAkB,UAAU;AAI7C,QAAO;;AAGT,SAAS,gBAAgB,EACvB,QACA,SACA,eACA,YACA,kBACA,eACA,cAS6D;AAC7D,WAAA,GAAA,2BAAA,oBAA4B,QAAQ,cAAc;CAElD,MAAM,2BAA2B,cAA8B;AAC7D,MAAI,CAAC,WACH,QAAO;EAGT,MAAM,mBAAA,GAAA,2BAAA,qBAAA,GAAA,2BAAA,iBACY,WAAW,SAAS,cAAc,IAChD,UACA,eACF,cACD;AACD,MAAI,oBAAoB,UACtB,SAAQ,MAAA,GAAA,sCAAA,gCACyB,iBAAiB,WAAWC,6BAAAA,aAAa,CACzE;AAGH,aAAW,gBAAgB;AAE3B,SAAO;;CAGT,MAAM,aAAa,cAA4B;AAC7C,MAAI,CAAC,WACH;AAEF,eAAA,GAAA,2BAAA,oBAA+B,WAAW,cAAc;AAExD,4BAAA,eAAe,kBADS,wBAAwB,UACA,CAAC;;AAEnD,QAAO,CAAC,WAAW,wBAAwB"}
@@ -3,10 +3,22 @@ require("../_virtual/_rolldown/runtime.js");
3
3
  const require_NativeGtReactNative = require("../NativeGtReactNative.js");
4
4
  const require_errors_dir_warnings = require("../errors-dir/warnings.js");
5
5
  let react_native = require("react-native");
6
+ let _generaltranslation_react_core_internal = require("@generaltranslation/react-core/internal");
6
7
  //#region src/utils/nativeStore.ts
7
8
  /**
8
- * Native store interface, used to replace cookie behavior from gt-react
9
+ * Get the locale from the native store.
10
+ *
11
+ * This reads persisted native storage directly, so it can temporarily be out of
12
+ * sync with React state while a locale change is in progress.
13
+ *
14
+ * If GTProvider uses a custom localeCookieName, pass that same value as the key.
15
+ *
16
+ * @param key - The key to get the locale from
17
+ * @returns The locale from the native store
9
18
  */
19
+ function getLocaleFromNativeStore(key = _generaltranslation_react_core_internal.defaultLocaleCookieName) {
20
+ return nativeStoreGet(key);
21
+ }
10
22
  /**
11
23
  * Get a value from the native store
12
24
  * @param key - The key to get the value for
@@ -39,6 +51,7 @@ function nativeStoreSet(key, value) {
39
51
  require_NativeGtReactNative.default.nativeStoreSet(key, value);
40
52
  }
41
53
  //#endregion
54
+ exports.getLocaleFromNativeStore = getLocaleFromNativeStore;
42
55
  exports.nativeStoreGet = nativeStoreGet;
43
56
  exports.nativeStoreSet = nativeStoreSet;
44
57
 
@@ -1 +1 @@
1
- {"version":3,"file":"nativeStore.js","names":["Platform","ssrUnsupportedWarning","GtReactNative"],"sources":["../../../src/utils/nativeStore.ts"],"sourcesContent":["import { Platform } from 'react-native';\nimport GtReactNative from '../NativeGtReactNative';\nimport { ssrUnsupportedWarning } from '../errors-dir/warnings';\n\n/**\n * Native store interface, used to replace cookie behavior from gt-react\n */\n\n/**\n * Get a value from the native store\n * @param key - The key to get the value for\n * @returns The value for the key\n */\nexport function nativeStoreGet(key: string): string | null {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return null;\n }\n return localStorage.getItem(key);\n }\n return GtReactNative.nativeStoreGet(key);\n}\n\n/**\n * Set a value in the native store\n * @param key - The key to set the value for\n * @param value - The value to set\n */\nexport function nativeStoreSet(key: string, value: string): void {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return;\n }\n localStorage.setItem(key, value);\n return;\n }\n GtReactNative.nativeStoreSet(key, value);\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,SAAgB,eAAe,KAA4B;AACzD,KAAIA,aAAAA,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAKC,4BAAAA,sBAAsB;AACnC,UAAO;;AAET,SAAO,aAAa,QAAQ,IAAI;;AAElC,QAAOC,4BAAAA,QAAc,eAAe,IAAI;;;;;;;AAQ1C,SAAgB,eAAe,KAAa,OAAqB;AAC/D,KAAIF,aAAAA,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAKC,4BAAAA,sBAAsB;AACnC;;AAEF,eAAa,QAAQ,KAAK,MAAM;AAChC;;AAEF,6BAAA,QAAc,eAAe,KAAK,MAAM"}
1
+ {"version":3,"file":"nativeStore.js","names":["defaultLocaleCookieName","Platform","ssrUnsupportedWarning","GtReactNative"],"sources":["../../../src/utils/nativeStore.ts"],"sourcesContent":["import { defaultLocaleCookieName } from '@generaltranslation/react-core/internal';\nimport { Platform } from 'react-native';\nimport GtReactNative from '../NativeGtReactNative';\nimport { ssrUnsupportedWarning } from '../errors-dir/warnings';\n\n/**\n * Get the locale from the native store.\n *\n * This reads persisted native storage directly, so it can temporarily be out of\n * sync with React state while a locale change is in progress.\n *\n * If GTProvider uses a custom localeCookieName, pass that same value as the key.\n *\n * @param key - The key to get the locale from\n * @returns The locale from the native store\n */\nexport function getLocaleFromNativeStore(\n key = defaultLocaleCookieName\n): string | null {\n return nativeStoreGet(key);\n}\n\n/**\n * Get a value from the native store\n * @param key - The key to get the value for\n * @returns The value for the key\n */\nexport function nativeStoreGet(key: string): string | null {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return null;\n }\n return localStorage.getItem(key);\n }\n return GtReactNative.nativeStoreGet(key);\n}\n\n/**\n * Set a value in the native store\n * @param key - The key to set the value for\n * @param value - The value to set\n */\nexport function nativeStoreSet(key: string, value: string): void {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return;\n }\n localStorage.setItem(key, value);\n return;\n }\n GtReactNative.nativeStoreSet(key, value);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgBA,SAAgB,yBACd,MAAMA,wCAAAA,yBACS;AACf,QAAO,eAAe,IAAI;;;;;;;AAQ5B,SAAgB,eAAe,KAA4B;AACzD,KAAIC,aAAAA,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAKC,4BAAAA,sBAAsB;AACnC,UAAO;;AAET,SAAO,aAAa,QAAQ,IAAI;;AAElC,QAAOC,4BAAAA,QAAc,eAAe,IAAI;;;;;;;AAQ1C,SAAgB,eAAe,KAAa,OAAqB;AAC/D,KAAIF,aAAAA,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAKC,4BAAAA,sBAAsB;AACnC;;AAEF,eAAa,QAAQ,KAAK,MAAM;AAChC;;AAEF,6BAAA,QAAc,eAAe,KAAK,MAAM"}
@@ -1,5 +1,6 @@
1
1
  import { GTProvider } from './provider/GTProvider';
2
+ import { getLocaleFromNativeStore } from './utils/nativeStore';
2
3
  import { T, useGT, useTranslations, useDefaultLocale, useLocale, useRegion, Var, Num, Currency, DateTime, Static, Plural, Branch, useVersionId, useLocales, useLocaleSelector, useSetLocale, useGTClass, useLocaleProperties, useRegionSelector, useLocaleDirection, useMessages, msg, decodeMsg, decodeOptions, gtFallback, mFallback, declareStatic, declareVar, decodeVars } from '@generaltranslation/react-core';
3
4
  import type { DictionaryTranslationOptions, InlineTranslationOptions, RuntimeTranslationOptions } from '@generaltranslation/react-core/types';
4
- export { Static, Var, Num, Currency, DateTime, T, GTProvider, Plural, Branch, useGT, useTranslations, useDefaultLocale, useLocale, useLocales, useSetLocale, useLocaleSelector, useRegion, useRegionSelector, useGTClass, useLocaleProperties, useLocaleDirection, useVersionId, type DictionaryTranslationOptions, type InlineTranslationOptions, type RuntimeTranslationOptions, msg, decodeMsg, decodeOptions, useMessages, gtFallback, mFallback, declareStatic, declareVar, decodeVars, };
5
+ export { Static, Var, Num, Currency, DateTime, T, GTProvider, Plural, Branch, useGT, useTranslations, useDefaultLocale, useLocale, useLocales, useSetLocale, useLocaleSelector, useRegion, useRegionSelector, useGTClass, useLocaleProperties, useLocaleDirection, useVersionId, getLocaleFromNativeStore, type DictionaryTranslationOptions, type InlineTranslationOptions, type RuntimeTranslationOptions, msg, decodeMsg, decodeOptions, useMessages, gtFallback, mFallback, declareStatic, declareVar, decodeVars, };
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,OAAO,EACL,CAAC,EACD,KAAK,EACL,eAAe,EACf,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,GAAG,EACH,GAAG,EACH,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,MAAM,EACN,MAAM,EACN,YAAY,EACZ,UAAU,EACV,iBAAiB,EACjB,YAAY,EACZ,UAAU,EACV,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,WAAW,EACX,GAAG,EACH,SAAS,EACT,aAAa,EACb,UAAU,EACV,SAAS,EACT,aAAa,EACb,UAAU,EACV,UAAU,EACX,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EACV,4BAA4B,EAC5B,wBAAwB,EACxB,yBAAyB,EAC1B,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EACL,MAAM,EACN,GAAG,EACH,GAAG,EACH,QAAQ,EACR,QAAQ,EACR,CAAC,EACD,UAAU,EACV,MAAM,EACN,MAAM,EACN,KAAK,EACL,eAAe,EACf,gBAAgB,EAChB,SAAS,EACT,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,SAAS,EACT,iBAAiB,EACjB,UAAU,EACV,mBAAmB,EACnB,kBAAkB,EAClB,YAAY,EACZ,KAAK,4BAA4B,EACjC,KAAK,wBAAwB,EAC7B,KAAK,yBAAyB,EAC9B,GAAG,EACH,SAAS,EACT,aAAa,EACb,WAAW,EACX,UAAU,EACV,SAAS,EACT,aAAa,EACb,UAAU,EACV,UAAU,GACX,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAE/D,OAAO,EACL,CAAC,EACD,KAAK,EACL,eAAe,EACf,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,GAAG,EACH,GAAG,EACH,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,MAAM,EACN,MAAM,EACN,YAAY,EACZ,UAAU,EACV,iBAAiB,EACjB,YAAY,EACZ,UAAU,EACV,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,WAAW,EACX,GAAG,EACH,SAAS,EACT,aAAa,EACb,UAAU,EACV,SAAS,EACT,aAAa,EACb,UAAU,EACV,UAAU,EACX,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EACV,4BAA4B,EAC5B,wBAAwB,EACxB,yBAAyB,EAC1B,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EACL,MAAM,EACN,GAAG,EACH,GAAG,EACH,QAAQ,EACR,QAAQ,EACR,CAAC,EACD,UAAU,EACV,MAAM,EACN,MAAM,EACN,KAAK,EACL,eAAe,EACf,gBAAgB,EAChB,SAAS,EACT,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,SAAS,EACT,iBAAiB,EACjB,UAAU,EACV,mBAAmB,EACnB,kBAAkB,EAClB,YAAY,EACZ,wBAAwB,EACxB,KAAK,4BAA4B,EACjC,KAAK,wBAAwB,EAC7B,KAAK,yBAAyB,EAC9B,GAAG,EACH,SAAS,EACT,aAAa,EACb,WAAW,EACX,UAAU,EACV,SAAS,EACT,aAAa,EACb,UAAU,EACV,UAAU,GACX,CAAC"}
@@ -1,3 +1,4 @@
1
+ import { getLocaleFromNativeStore } from "./utils/nativeStore.js";
1
2
  import { GTProvider } from "./provider/GTProvider.js";
2
3
  import { Branch, Currency, DateTime, Num, Plural, Static, T, Var, declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, gtFallback, mFallback, msg, useDefaultLocale, useGT, useGTClass, useLocale, useLocaleDirection, useLocaleProperties, useLocaleSelector, useLocales, useMessages, useRegion, useRegionSelector, useSetLocale, useTranslations, useVersionId } from "@generaltranslation/react-core";
3
- export { Branch, Currency, DateTime, GTProvider, Num, Plural, Static, T, Var, declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, gtFallback, mFallback, msg, useDefaultLocale, useGT, useGTClass, useLocale, useLocaleDirection, useLocaleProperties, useLocaleSelector, useLocales, useMessages, useRegion, useRegionSelector, useSetLocale, useTranslations, useVersionId };
4
+ export { Branch, Currency, DateTime, GTProvider, Num, Plural, Static, T, Var, declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, getLocaleFromNativeStore, gtFallback, mFallback, msg, useDefaultLocale, useGT, useGTClass, useLocale, useLocaleDirection, useLocaleProperties, useLocaleSelector, useLocales, useMessages, useRegion, useRegionSelector, useSetLocale, useTranslations, useVersionId };
@@ -1 +1 @@
1
- {"version":3,"file":"useDetermineLocale.d.ts","sourceRoot":"","sources":["../../../../../src/provider/hooks/locale/useDetermineLocale.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,EACzB,MAAM,sCAAsC,CAAC;AAM9C,wBAAgB,kBAAkB,CAAC,EACjC,MAAM,EAAE,OAAY,EACpB,aAAoC,EACpC,OAAY,EACZ,gBAA8C,EAC9C,GAAW,EAAE,eAAe;AAC5B,aAAa,EACb,UAAU,GACX,EAAE,wBAAwB,GAAG,wBAAwB,CAiErD"}
1
+ {"version":3,"file":"useDetermineLocale.d.ts","sourceRoot":"","sources":["../../../../../src/provider/hooks/locale/useDetermineLocale.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,EACzB,MAAM,sCAAsC,CAAC;AAM9C,wBAAgB,kBAAkB,CAAC,EACjC,MAAM,EAAE,OAAY,EACpB,aAAoC,EACpC,OAAY,EACZ,gBAA0C,EAC1C,GAAW,EAAE,eAAe;AAC5B,aAAa,EACb,UAAU,GACX,EAAE,wBAAwB,GAAG,wBAAwB,CAiErD"}
@@ -3,10 +3,11 @@ import { getNativeLocales } from "../../../utils/getNativeLocales.js";
3
3
  import { nativeStoreGet, nativeStoreSet } from "../../../utils/nativeStore.js";
4
4
  import { useEffect, useState } from "react";
5
5
  import { determineLocale, resolveAliasLocale } from "@generaltranslation/format";
6
+ import { defaultLocaleCookieName } from "@generaltranslation/react-core/internal";
6
7
  import { libraryDefaultLocale } from "generaltranslation/internal";
7
8
  import { createUnsupportedLocaleWarning } from "@generaltranslation/react-core/errors";
8
9
  //#region src/provider/hooks/locale/useDetermineLocale.ts
9
- function useDetermineLocale({ locale: _locale = "", defaultLocale = libraryDefaultLocale, locales = [], localeCookieName = "generaltranslation.locale", ssr = false, customMapping, enableI18n }) {
10
+ function useDetermineLocale({ locale: _locale = "", defaultLocale = libraryDefaultLocale, locales = [], localeCookieName = defaultLocaleCookieName, ssr = false, customMapping, enableI18n }) {
10
11
  _locale = resolveAliasLocale(_locale, customMapping);
11
12
  locales = locales.map((locale) => resolveAliasLocale(locale, customMapping));
12
13
  const initializeLocale = () => {
@@ -1 +1 @@
1
- {"version":3,"file":"useDetermineLocale.js","names":[],"sources":["../../../../../src/provider/hooks/locale/useDetermineLocale.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport type { Dispatch, SetStateAction } from 'react';\nimport {\n determineLocale,\n resolveAliasLocale,\n} from '@generaltranslation/format';\nimport { libraryDefaultLocale } from 'generaltranslation/internal';\nimport type { CustomMapping } from '@generaltranslation/format/types';\nimport type {\n UseDetermineLocaleParams,\n UseDetermineLocaleReturn,\n} from '@generaltranslation/react-core/types';\nimport { createUnsupportedLocaleWarning } from '@generaltranslation/react-core/errors';\nimport { PACKAGE_NAME } from '../../../errors-dir/constants';\nimport { getNativeLocales } from '../../../utils/getNativeLocales';\nimport { nativeStoreGet, nativeStoreSet } from '../../../utils/nativeStore';\n\nexport function useDetermineLocale({\n locale: _locale = '',\n defaultLocale = libraryDefaultLocale,\n locales = [],\n localeCookieName = 'generaltranslation.locale',\n ssr = false, // not relevant\n customMapping,\n enableI18n,\n}: UseDetermineLocaleParams): UseDetermineLocaleReturn {\n // resolve alias locale\n _locale = resolveAliasLocale(_locale, customMapping);\n locales = locales.map((locale) => resolveAliasLocale(locale, customMapping));\n\n const initializeLocale = () => {\n if (!enableI18n) {\n return defaultLocale;\n }\n const result = resolveAliasLocale(\n ssr\n ? _locale\n ? determineLocale(_locale, locales, customMapping) || ''\n : ''\n : getNewLocale({\n _locale,\n locale: _locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n }),\n customMapping\n );\n return result;\n };\n\n // maintaining the state of locale\n const [locale, _setLocale] = useState<string>(initializeLocale());\n\n // Functions for setting internal locale state\n const [setLocale, setLocaleWithoutPersist] = createSetLocale({\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n _setLocale,\n customMapping,\n enableI18n,\n });\n\n // check browser for locales\n useEffect(() => {\n const newLocale = getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n });\n setLocaleWithoutPersist(newLocale);\n }, [\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n ]);\n\n return [locale, setLocale];\n}\n\n// ----- HELPER FUNCTIONS ---- //\n\n/**\n * Choose a locale to use\n * (1) use provided locale\n * (2) use cookie locale\n * (3) use preferred locale\n * (5) fallback to defaultLocale\n * Update the cookie locale to be correct\n */\nfunction getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n _locale: string;\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): string {\n if (!enableI18n) {\n return defaultLocale;\n }\n // No change, return\n if (\n _locale &&\n _locale === locale &&\n determineLocale(_locale, locales, customMapping) === locale\n ) {\n return resolveAliasLocale(_locale, customMapping);\n }\n\n // Check for locale in native store\n let storedLocale = nativeStoreGet(localeCookieName);\n if (storedLocale) {\n storedLocale = resolveAliasLocale(storedLocale, customMapping);\n }\n\n // check user's configured locales\n let preferredLocales = getNativeLocales();\n if (preferredLocales.length === 0) preferredLocales = [defaultLocale];\n preferredLocales = preferredLocales.map((l) =>\n resolveAliasLocale(l, customMapping)\n );\n\n // determine locale\n let newLocale =\n determineLocale(\n [\n ...(_locale ? [_locale] : []), // prefer user passed locale\n ...(storedLocale ? [storedLocale] : []), // then prefer stored locale\n ...preferredLocales, // then prefer browser locale\n ],\n locales,\n customMapping\n ) || defaultLocale;\n if (newLocale) {\n newLocale = resolveAliasLocale(newLocale, customMapping);\n }\n\n // if stored locale not valid, change it back to whatever we use for fallback\n if (storedLocale && storedLocale !== newLocale) {\n nativeStoreSet(localeCookieName, newLocale);\n }\n\n // return new locale\n return newLocale;\n}\n\nfunction createSetLocale({\n locale,\n locales,\n defaultLocale,\n _setLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n _setLocale: Dispatch<SetStateAction<string>>;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): [(newLocale: string) => void, (newLocale: string) => void] {\n locale = resolveAliasLocale(locale, customMapping);\n // Just update the internal state, don't persist it\n const setLocaleWithoutPersist = (newLocale: string): string => {\n if (!enableI18n) {\n return defaultLocale;\n }\n // validate locale\n const validatedLocale = resolveAliasLocale(\n determineLocale(newLocale, locales, customMapping) ||\n locale ||\n defaultLocale,\n customMapping\n );\n if (validatedLocale !== newLocale) {\n console.warn(\n createUnsupportedLocaleWarning(validatedLocale, newLocale, PACKAGE_NAME)\n );\n }\n // persist locale\n _setLocale(validatedLocale);\n\n return validatedLocale;\n };\n // update locale and persist it in native store\n const setLocale = (newLocale: string): void => {\n if (!enableI18n) {\n return;\n }\n newLocale = resolveAliasLocale(newLocale, customMapping);\n const validatedLocale = setLocaleWithoutPersist(newLocale);\n nativeStoreSet(localeCookieName, validatedLocale);\n };\n return [setLocale, setLocaleWithoutPersist];\n}\n"],"mappings":";;;;;;;;AAiBA,SAAgB,mBAAmB,EACjC,QAAQ,UAAU,IAClB,gBAAgB,sBAChB,UAAU,EAAE,EACZ,mBAAmB,6BACnB,MAAM,OACN,eACA,cACqD;AAErD,WAAU,mBAAmB,SAAS,cAAc;AACpD,WAAU,QAAQ,KAAK,WAAW,mBAAmB,QAAQ,cAAc,CAAC;CAE5E,MAAM,yBAAyB;AAC7B,MAAI,CAAC,WACH,QAAO;AAkBT,SAhBe,mBACb,MACI,UACE,gBAAgB,SAAS,SAAS,cAAc,IAAI,KACpD,KACF,aAAa;GACX;GACA,QAAQ;GACR;GACA;GACA;GACA;GACA;GACD,CAAC,EACN,cAEW;;CAIf,MAAM,CAAC,QAAQ,cAAc,SAAiB,kBAAkB,CAAC;CAGjE,MAAM,CAAC,WAAW,2BAA2B,gBAAgB;EAC3D;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAGF,iBAAgB;AAUd,0BATkB,aAAa;GAC7B;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CACgC,CAAC;IACjC;EACD;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAEF,QAAO,CAAC,QAAQ,UAAU;;;;;;;;;;AAa5B,SAAS,aAAa,EACpB,SACA,QACA,SACA,eACA,kBACA,eACA,cASS;AACT,KAAI,CAAC,WACH,QAAO;AAGT,KACE,WACA,YAAY,UACZ,gBAAgB,SAAS,SAAS,cAAc,KAAK,OAErD,QAAO,mBAAmB,SAAS,cAAc;CAInD,IAAI,eAAe,eAAe,iBAAiB;AACnD,KAAI,aACF,gBAAe,mBAAmB,cAAc,cAAc;CAIhE,IAAI,mBAAmB,kBAAkB;AACzC,KAAI,iBAAiB,WAAW,EAAG,oBAAmB,CAAC,cAAc;AACrE,oBAAmB,iBAAiB,KAAK,MACvC,mBAAmB,GAAG,cAAc,CACrC;CAGD,IAAI,YACF,gBACE;EACE,GAAI,UAAU,CAAC,QAAQ,GAAG,EAAE;EAC5B,GAAI,eAAe,CAAC,aAAa,GAAG,EAAE;EACtC,GAAG;EACJ,EACD,SACA,cACD,IAAI;AACP,KAAI,UACF,aAAY,mBAAmB,WAAW,cAAc;AAI1D,KAAI,gBAAgB,iBAAiB,UACnC,gBAAe,kBAAkB,UAAU;AAI7C,QAAO;;AAGT,SAAS,gBAAgB,EACvB,QACA,SACA,eACA,YACA,kBACA,eACA,cAS6D;AAC7D,UAAS,mBAAmB,QAAQ,cAAc;CAElD,MAAM,2BAA2B,cAA8B;AAC7D,MAAI,CAAC,WACH,QAAO;EAGT,MAAM,kBAAkB,mBACtB,gBAAgB,WAAW,SAAS,cAAc,IAChD,UACA,eACF,cACD;AACD,MAAI,oBAAoB,UACtB,SAAQ,KACN,+BAA+B,iBAAiB,WAAW,aAAa,CACzE;AAGH,aAAW,gBAAgB;AAE3B,SAAO;;CAGT,MAAM,aAAa,cAA4B;AAC7C,MAAI,CAAC,WACH;AAEF,cAAY,mBAAmB,WAAW,cAAc;AAExD,iBAAe,kBADS,wBAAwB,UACA,CAAC;;AAEnD,QAAO,CAAC,WAAW,wBAAwB"}
1
+ {"version":3,"file":"useDetermineLocale.js","names":[],"sources":["../../../../../src/provider/hooks/locale/useDetermineLocale.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport type { Dispatch, SetStateAction } from 'react';\nimport {\n determineLocale,\n resolveAliasLocale,\n} from '@generaltranslation/format';\nimport { defaultLocaleCookieName } from '@generaltranslation/react-core/internal';\nimport { libraryDefaultLocale } from 'generaltranslation/internal';\nimport type { CustomMapping } from '@generaltranslation/format/types';\nimport type {\n UseDetermineLocaleParams,\n UseDetermineLocaleReturn,\n} from '@generaltranslation/react-core/types';\nimport { createUnsupportedLocaleWarning } from '@generaltranslation/react-core/errors';\nimport { PACKAGE_NAME } from '../../../errors-dir/constants';\nimport { getNativeLocales } from '../../../utils/getNativeLocales';\nimport { nativeStoreGet, nativeStoreSet } from '../../../utils/nativeStore';\n\nexport function useDetermineLocale({\n locale: _locale = '',\n defaultLocale = libraryDefaultLocale,\n locales = [],\n localeCookieName = defaultLocaleCookieName,\n ssr = false, // not relevant\n customMapping,\n enableI18n,\n}: UseDetermineLocaleParams): UseDetermineLocaleReturn {\n // resolve alias locale\n _locale = resolveAliasLocale(_locale, customMapping);\n locales = locales.map((locale) => resolveAliasLocale(locale, customMapping));\n\n const initializeLocale = () => {\n if (!enableI18n) {\n return defaultLocale;\n }\n const result = resolveAliasLocale(\n ssr\n ? _locale\n ? determineLocale(_locale, locales, customMapping) || ''\n : ''\n : getNewLocale({\n _locale,\n locale: _locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n }),\n customMapping\n );\n return result;\n };\n\n // maintaining the state of locale\n const [locale, _setLocale] = useState<string>(initializeLocale());\n\n // Functions for setting internal locale state\n const [setLocale, setLocaleWithoutPersist] = createSetLocale({\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n _setLocale,\n customMapping,\n enableI18n,\n });\n\n // check browser for locales\n useEffect(() => {\n const newLocale = getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n });\n setLocaleWithoutPersist(newLocale);\n }, [\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n ]);\n\n return [locale, setLocale];\n}\n\n// ----- HELPER FUNCTIONS ---- //\n\n/**\n * Choose a locale to use\n * (1) use provided locale\n * (2) use cookie locale\n * (3) use preferred locale\n * (5) fallback to defaultLocale\n * Update the cookie locale to be correct\n */\nfunction getNewLocale({\n _locale,\n locale,\n locales,\n defaultLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n _locale: string;\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): string {\n if (!enableI18n) {\n return defaultLocale;\n }\n // No change, return\n if (\n _locale &&\n _locale === locale &&\n determineLocale(_locale, locales, customMapping) === locale\n ) {\n return resolveAliasLocale(_locale, customMapping);\n }\n\n // Check for locale in native store\n let storedLocale = nativeStoreGet(localeCookieName);\n if (storedLocale) {\n storedLocale = resolveAliasLocale(storedLocale, customMapping);\n }\n\n // check user's configured locales\n let preferredLocales = getNativeLocales();\n if (preferredLocales.length === 0) preferredLocales = [defaultLocale];\n preferredLocales = preferredLocales.map((l) =>\n resolveAliasLocale(l, customMapping)\n );\n\n // determine locale\n let newLocale =\n determineLocale(\n [\n ...(_locale ? [_locale] : []), // prefer user passed locale\n ...(storedLocale ? [storedLocale] : []), // then prefer stored locale\n ...preferredLocales, // then prefer browser locale\n ],\n locales,\n customMapping\n ) || defaultLocale;\n if (newLocale) {\n newLocale = resolveAliasLocale(newLocale, customMapping);\n }\n\n // if stored locale not valid, change it back to whatever we use for fallback\n if (storedLocale && storedLocale !== newLocale) {\n nativeStoreSet(localeCookieName, newLocale);\n }\n\n // return new locale\n return newLocale;\n}\n\nfunction createSetLocale({\n locale,\n locales,\n defaultLocale,\n _setLocale,\n localeCookieName,\n customMapping,\n enableI18n,\n}: {\n locale: string;\n locales: string[];\n defaultLocale: string;\n localeCookieName: string;\n _setLocale: Dispatch<SetStateAction<string>>;\n customMapping?: CustomMapping;\n enableI18n: boolean;\n}): [(newLocale: string) => void, (newLocale: string) => void] {\n locale = resolveAliasLocale(locale, customMapping);\n // Just update the internal state, don't persist it\n const setLocaleWithoutPersist = (newLocale: string): string => {\n if (!enableI18n) {\n return defaultLocale;\n }\n // validate locale\n const validatedLocale = resolveAliasLocale(\n determineLocale(newLocale, locales, customMapping) ||\n locale ||\n defaultLocale,\n customMapping\n );\n if (validatedLocale !== newLocale) {\n console.warn(\n createUnsupportedLocaleWarning(validatedLocale, newLocale, PACKAGE_NAME)\n );\n }\n // persist locale\n _setLocale(validatedLocale);\n\n return validatedLocale;\n };\n // update locale and persist it in native store\n const setLocale = (newLocale: string): void => {\n if (!enableI18n) {\n return;\n }\n newLocale = resolveAliasLocale(newLocale, customMapping);\n const validatedLocale = setLocaleWithoutPersist(newLocale);\n nativeStoreSet(localeCookieName, validatedLocale);\n };\n return [setLocale, setLocaleWithoutPersist];\n}\n"],"mappings":";;;;;;;;;AAkBA,SAAgB,mBAAmB,EACjC,QAAQ,UAAU,IAClB,gBAAgB,sBAChB,UAAU,EAAE,EACZ,mBAAmB,yBACnB,MAAM,OACN,eACA,cACqD;AAErD,WAAU,mBAAmB,SAAS,cAAc;AACpD,WAAU,QAAQ,KAAK,WAAW,mBAAmB,QAAQ,cAAc,CAAC;CAE5E,MAAM,yBAAyB;AAC7B,MAAI,CAAC,WACH,QAAO;AAkBT,SAhBe,mBACb,MACI,UACE,gBAAgB,SAAS,SAAS,cAAc,IAAI,KACpD,KACF,aAAa;GACX;GACA,QAAQ;GACR;GACA;GACA;GACA;GACA;GACD,CAAC,EACN,cAEW;;CAIf,MAAM,CAAC,QAAQ,cAAc,SAAiB,kBAAkB,CAAC;CAGjE,MAAM,CAAC,WAAW,2BAA2B,gBAAgB;EAC3D;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAGF,iBAAgB;AAUd,0BATkB,aAAa;GAC7B;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CACgC,CAAC;IACjC;EACD;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAEF,QAAO,CAAC,QAAQ,UAAU;;;;;;;;;;AAa5B,SAAS,aAAa,EACpB,SACA,QACA,SACA,eACA,kBACA,eACA,cASS;AACT,KAAI,CAAC,WACH,QAAO;AAGT,KACE,WACA,YAAY,UACZ,gBAAgB,SAAS,SAAS,cAAc,KAAK,OAErD,QAAO,mBAAmB,SAAS,cAAc;CAInD,IAAI,eAAe,eAAe,iBAAiB;AACnD,KAAI,aACF,gBAAe,mBAAmB,cAAc,cAAc;CAIhE,IAAI,mBAAmB,kBAAkB;AACzC,KAAI,iBAAiB,WAAW,EAAG,oBAAmB,CAAC,cAAc;AACrE,oBAAmB,iBAAiB,KAAK,MACvC,mBAAmB,GAAG,cAAc,CACrC;CAGD,IAAI,YACF,gBACE;EACE,GAAI,UAAU,CAAC,QAAQ,GAAG,EAAE;EAC5B,GAAI,eAAe,CAAC,aAAa,GAAG,EAAE;EACtC,GAAG;EACJ,EACD,SACA,cACD,IAAI;AACP,KAAI,UACF,aAAY,mBAAmB,WAAW,cAAc;AAI1D,KAAI,gBAAgB,iBAAiB,UACnC,gBAAe,kBAAkB,UAAU;AAI7C,QAAO;;AAGT,SAAS,gBAAgB,EACvB,QACA,SACA,eACA,YACA,kBACA,eACA,cAS6D;AAC7D,UAAS,mBAAmB,QAAQ,cAAc;CAElD,MAAM,2BAA2B,cAA8B;AAC7D,MAAI,CAAC,WACH,QAAO;EAGT,MAAM,kBAAkB,mBACtB,gBAAgB,WAAW,SAAS,cAAc,IAChD,UACA,eACF,cACD;AACD,MAAI,oBAAoB,UACtB,SAAQ,KACN,+BAA+B,iBAAiB,WAAW,aAAa,CACzE;AAGH,aAAW,gBAAgB;AAE3B,SAAO;;CAGT,MAAM,aAAa,cAA4B;AAC7C,MAAI,CAAC,WACH;AAEF,cAAY,mBAAmB,WAAW,cAAc;AAExD,iBAAe,kBADS,wBAAwB,UACA,CAAC;;AAEnD,QAAO,CAAC,WAAW,wBAAwB"}
@@ -1,6 +1,15 @@
1
1
  /**
2
- * Native store interface, used to replace cookie behavior from gt-react
2
+ * Get the locale from the native store.
3
+ *
4
+ * This reads persisted native storage directly, so it can temporarily be out of
5
+ * sync with React state while a locale change is in progress.
6
+ *
7
+ * If GTProvider uses a custom localeCookieName, pass that same value as the key.
8
+ *
9
+ * @param key - The key to get the locale from
10
+ * @returns The locale from the native store
3
11
  */
12
+ export declare function getLocaleFromNativeStore(key?: string): string | null;
4
13
  /**
5
14
  * Get a value from the native store
6
15
  * @param key - The key to get the value for
@@ -1 +1 @@
1
- {"version":3,"file":"nativeStore.d.ts","sourceRoot":"","sources":["../../../src/utils/nativeStore.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAUzD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAW/D"}
1
+ {"version":3,"file":"nativeStore.d.ts","sourceRoot":"","sources":["../../../src/utils/nativeStore.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,SAA0B,GAC5B,MAAM,GAAG,IAAI,CAEf;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAUzD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAW/D"}
@@ -1,10 +1,22 @@
1
1
  import NativeGtReactNative_default from "../NativeGtReactNative.js";
2
2
  import { ssrUnsupportedWarning } from "../errors-dir/warnings.js";
3
3
  import { Platform } from "react-native";
4
+ import { defaultLocaleCookieName } from "@generaltranslation/react-core/internal";
4
5
  //#region src/utils/nativeStore.ts
5
6
  /**
6
- * Native store interface, used to replace cookie behavior from gt-react
7
+ * Get the locale from the native store.
8
+ *
9
+ * This reads persisted native storage directly, so it can temporarily be out of
10
+ * sync with React state while a locale change is in progress.
11
+ *
12
+ * If GTProvider uses a custom localeCookieName, pass that same value as the key.
13
+ *
14
+ * @param key - The key to get the locale from
15
+ * @returns The locale from the native store
7
16
  */
17
+ function getLocaleFromNativeStore(key = defaultLocaleCookieName) {
18
+ return nativeStoreGet(key);
19
+ }
8
20
  /**
9
21
  * Get a value from the native store
10
22
  * @param key - The key to get the value for
@@ -37,6 +49,6 @@ function nativeStoreSet(key, value) {
37
49
  NativeGtReactNative_default.nativeStoreSet(key, value);
38
50
  }
39
51
  //#endregion
40
- export { nativeStoreGet, nativeStoreSet };
52
+ export { getLocaleFromNativeStore, nativeStoreGet, nativeStoreSet };
41
53
 
42
54
  //# sourceMappingURL=nativeStore.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"nativeStore.js","names":["GtReactNative"],"sources":["../../../src/utils/nativeStore.ts"],"sourcesContent":["import { Platform } from 'react-native';\nimport GtReactNative from '../NativeGtReactNative';\nimport { ssrUnsupportedWarning } from '../errors-dir/warnings';\n\n/**\n * Native store interface, used to replace cookie behavior from gt-react\n */\n\n/**\n * Get a value from the native store\n * @param key - The key to get the value for\n * @returns The value for the key\n */\nexport function nativeStoreGet(key: string): string | null {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return null;\n }\n return localStorage.getItem(key);\n }\n return GtReactNative.nativeStoreGet(key);\n}\n\n/**\n * Set a value in the native store\n * @param key - The key to set the value for\n * @param value - The value to set\n */\nexport function nativeStoreSet(key: string, value: string): void {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return;\n }\n localStorage.setItem(key, value);\n return;\n }\n GtReactNative.nativeStoreSet(key, value);\n}\n"],"mappings":";;;;;;;;;;;;AAaA,SAAgB,eAAe,KAA4B;AACzD,KAAI,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAK,sBAAsB;AACnC,UAAO;;AAET,SAAO,aAAa,QAAQ,IAAI;;AAElC,QAAOA,4BAAc,eAAe,IAAI;;;;;;;AAQ1C,SAAgB,eAAe,KAAa,OAAqB;AAC/D,KAAI,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAK,sBAAsB;AACnC;;AAEF,eAAa,QAAQ,KAAK,MAAM;AAChC;;AAEF,6BAAc,eAAe,KAAK,MAAM"}
1
+ {"version":3,"file":"nativeStore.js","names":["GtReactNative"],"sources":["../../../src/utils/nativeStore.ts"],"sourcesContent":["import { defaultLocaleCookieName } from '@generaltranslation/react-core/internal';\nimport { Platform } from 'react-native';\nimport GtReactNative from '../NativeGtReactNative';\nimport { ssrUnsupportedWarning } from '../errors-dir/warnings';\n\n/**\n * Get the locale from the native store.\n *\n * This reads persisted native storage directly, so it can temporarily be out of\n * sync with React state while a locale change is in progress.\n *\n * If GTProvider uses a custom localeCookieName, pass that same value as the key.\n *\n * @param key - The key to get the locale from\n * @returns The locale from the native store\n */\nexport function getLocaleFromNativeStore(\n key = defaultLocaleCookieName\n): string | null {\n return nativeStoreGet(key);\n}\n\n/**\n * Get a value from the native store\n * @param key - The key to get the value for\n * @returns The value for the key\n */\nexport function nativeStoreGet(key: string): string | null {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return null;\n }\n return localStorage.getItem(key);\n }\n return GtReactNative.nativeStoreGet(key);\n}\n\n/**\n * Set a value in the native store\n * @param key - The key to set the value for\n * @param value - The value to set\n */\nexport function nativeStoreSet(key: string, value: string): void {\n if (Platform.OS === 'web') {\n if (typeof localStorage === 'undefined') {\n // eslint-disable-next-line no-console\n console.warn(ssrUnsupportedWarning);\n return;\n }\n localStorage.setItem(key, value);\n return;\n }\n GtReactNative.nativeStoreSet(key, value);\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,SAAgB,yBACd,MAAM,yBACS;AACf,QAAO,eAAe,IAAI;;;;;;;AAQ5B,SAAgB,eAAe,KAA4B;AACzD,KAAI,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAK,sBAAsB;AACnC,UAAO;;AAET,SAAO,aAAa,QAAQ,IAAI;;AAElC,QAAOA,4BAAc,eAAe,IAAI;;;;;;;AAQ1C,SAAgB,eAAe,KAAa,OAAqB;AAC/D,KAAI,SAAS,OAAO,OAAO;AACzB,MAAI,OAAO,iBAAiB,aAAa;AAEvC,WAAQ,KAAK,sBAAsB;AACnC;;AAEF,eAAa,QAAQ,KAAK,MAAM;AAChC;;AAEF,6BAAc,eAAe,KAAK,MAAM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gt-react-native",
3
- "version": "10.19.19",
3
+ "version": "10.20.0",
4
4
  "description": "An i18n package for React Native",
5
5
  "main": "./dist/commonjs/index.js",
6
6
  "module": "./dist/module/index.js",
package/src/index.tsx CHANGED
@@ -1,4 +1,5 @@
1
1
  import { GTProvider } from './provider/GTProvider';
2
+ import { getLocaleFromNativeStore } from './utils/nativeStore';
2
3
 
3
4
  import {
4
5
  T,
@@ -61,6 +62,7 @@ export {
61
62
  useLocaleProperties,
62
63
  useLocaleDirection,
63
64
  useVersionId,
65
+ getLocaleFromNativeStore,
64
66
  type DictionaryTranslationOptions,
65
67
  type InlineTranslationOptions,
66
68
  type RuntimeTranslationOptions,
@@ -4,6 +4,7 @@ import {
4
4
  determineLocale,
5
5
  resolveAliasLocale,
6
6
  } from '@generaltranslation/format';
7
+ import { defaultLocaleCookieName } from '@generaltranslation/react-core/internal';
7
8
  import { libraryDefaultLocale } from 'generaltranslation/internal';
8
9
  import type { CustomMapping } from '@generaltranslation/format/types';
9
10
  import type {
@@ -19,7 +20,7 @@ export function useDetermineLocale({
19
20
  locale: _locale = '',
20
21
  defaultLocale = libraryDefaultLocale,
21
22
  locales = [],
22
- localeCookieName = 'generaltranslation.locale',
23
+ localeCookieName = defaultLocaleCookieName,
23
24
  ssr = false, // not relevant
24
25
  customMapping,
25
26
  enableI18n,
@@ -1,10 +1,24 @@
1
+ import { defaultLocaleCookieName } from '@generaltranslation/react-core/internal';
1
2
  import { Platform } from 'react-native';
2
3
  import GtReactNative from '../NativeGtReactNative';
3
4
  import { ssrUnsupportedWarning } from '../errors-dir/warnings';
4
5
 
5
6
  /**
6
- * Native store interface, used to replace cookie behavior from gt-react
7
+ * Get the locale from the native store.
8
+ *
9
+ * This reads persisted native storage directly, so it can temporarily be out of
10
+ * sync with React state while a locale change is in progress.
11
+ *
12
+ * If GTProvider uses a custom localeCookieName, pass that same value as the key.
13
+ *
14
+ * @param key - The key to get the locale from
15
+ * @returns The locale from the native store
7
16
  */
17
+ export function getLocaleFromNativeStore(
18
+ key = defaultLocaleCookieName
19
+ ): string | null {
20
+ return nativeStoreGet(key);
21
+ }
8
22
 
9
23
  /**
10
24
  * Get a value from the native store