gt-react-native 10.19.16 → 10.19.17
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.
- package/dist/commonjs/NativeGtReactNative.js +9 -7
- package/dist/commonjs/NativeGtReactNative.js.map +1 -1
- package/dist/commonjs/_virtual/_rolldown/runtime.js +23 -0
- package/dist/commonjs/errors-dir/constants.js +5 -6
- package/dist/commonjs/errors-dir/constants.js.map +1 -1
- package/dist/commonjs/errors-dir/errors.js +12 -7
- package/dist/commonjs/errors-dir/errors.js.map +1 -1
- package/dist/commonjs/errors-dir/warnings.js +34 -15
- package/dist/commonjs/errors-dir/warnings.js.map +1 -1
- package/dist/commonjs/index.js +35 -194
- package/dist/commonjs/internal.js +3 -13
- package/dist/commonjs/plugin-dir/index.js +35 -51
- package/dist/commonjs/plugin-dir/index.js.map +1 -1
- package/dist/commonjs/plugin-dir/types.js +24 -9
- package/dist/commonjs/plugin-dir/types.js.map +1 -1
- package/dist/commonjs/plugin-dir/utils/resolveLocales.js +52 -69
- package/dist/commonjs/plugin-dir/utils/resolveLocales.js.map +1 -1
- package/dist/commonjs/plugin.js +5 -13
- package/dist/commonjs/provider/GTProvider.js +22 -20
- package/dist/commonjs/provider/GTProvider.js.map +1 -1
- package/dist/commonjs/provider/hooks/locale/useDetermineLocale.js +96 -160
- package/dist/commonjs/provider/hooks/locale/useDetermineLocale.js.map +1 -1
- package/dist/commonjs/provider/hooks/useRegionState.js +32 -44
- package/dist/commonjs/provider/hooks/useRegionState.js.map +1 -1
- package/dist/commonjs/tools/testLocalePolyfill.js +125 -146
- package/dist/commonjs/tools/testLocalePolyfill.js.map +1 -1
- package/dist/commonjs/types/config.js +1 -6
- package/dist/commonjs/utils/getNativeLocales.js +36 -54
- package/dist/commonjs/utils/getNativeLocales.js.map +1 -1
- package/dist/commonjs/utils/nativeStore.js +37 -42
- package/dist/commonjs/utils/nativeStore.js.map +1 -1
- package/dist/commonjs/utils/utils.js +10 -14
- package/dist/commonjs/utils/utils.js.map +1 -1
- package/dist/module/NativeGtReactNative.d.ts.map +1 -0
- package/dist/module/NativeGtReactNative.js +5 -3
- package/dist/module/NativeGtReactNative.js.map +1 -1
- package/dist/module/errors-dir/constants.d.ts.map +1 -0
- package/dist/module/errors-dir/constants.js +4 -2
- package/dist/module/errors-dir/constants.js.map +1 -1
- package/dist/module/errors-dir/errors.d.ts.map +1 -0
- package/dist/module/errors-dir/errors.js +11 -3
- package/dist/module/errors-dir/errors.js.map +1 -1
- package/dist/{typescript/commonjs/src → module}/errors-dir/warnings.d.ts +1 -1
- package/dist/module/errors-dir/warnings.d.ts.map +1 -0
- package/dist/module/errors-dir/warnings.js +31 -11
- package/dist/module/errors-dir/warnings.js.map +1 -1
- package/dist/module/index.d.ts.map +1 -0
- package/dist/module/index.js +2 -5
- package/dist/module/internal.d.ts.map +1 -0
- package/dist/module/internal.js +2 -4
- package/dist/module/plugin-dir/index.d.ts.map +1 -0
- package/dist/module/plugin-dir/index.js +28 -45
- package/dist/module/plugin-dir/index.js.map +1 -1
- package/dist/module/plugin-dir/types.d.ts.map +1 -0
- package/dist/module/plugin-dir/types.js +22 -5
- package/dist/module/plugin-dir/types.js.map +1 -1
- package/dist/module/plugin-dir/utils/resolveLocales.d.ts.map +1 -0
- package/dist/module/plugin-dir/utils/resolveLocales.js +46 -62
- package/dist/module/plugin-dir/utils/resolveLocales.js.map +1 -1
- package/dist/module/plugin.d.ts.map +1 -0
- package/dist/module/plugin.js +2 -4
- package/dist/module/provider/GTProvider.d.ts.map +1 -0
- package/dist/module/provider/GTProvider.js +15 -14
- package/dist/module/provider/GTProvider.js.map +1 -1
- package/dist/module/provider/hooks/locale/useDetermineLocale.d.ts.map +1 -0
- package/dist/module/provider/hooks/locale/useDetermineLocale.js +91 -153
- package/dist/module/provider/hooks/locale/useDetermineLocale.js.map +1 -1
- package/dist/module/provider/hooks/useRegionState.d.ts.map +1 -0
- package/dist/module/provider/hooks/useRegionState.js +29 -39
- package/dist/module/provider/hooks/useRegionState.js.map +1 -1
- package/dist/module/tools/testLocalePolyfill.d.ts.map +1 -0
- package/dist/module/tools/testLocalePolyfill.js +124 -143
- package/dist/module/tools/testLocalePolyfill.js.map +1 -1
- package/dist/module/types/config.d.ts.map +1 -0
- package/dist/module/types/config.js +0 -3
- package/dist/module/utils/getNativeLocales.d.ts.map +1 -0
- package/dist/module/utils/getNativeLocales.js +35 -51
- package/dist/module/utils/getNativeLocales.js.map +1 -1
- package/dist/module/utils/nativeStore.d.ts.map +1 -0
- package/dist/module/utils/nativeStore.js +35 -38
- package/dist/module/utils/nativeStore.js.map +1 -1
- package/dist/module/utils/utils.d.ts.map +1 -0
- package/dist/module/utils/utils.js +10 -11
- package/dist/module/utils/utils.js.map +1 -1
- package/package.json +15 -37
- package/src/errors-dir/errors.ts +8 -1
- package/src/errors-dir/warnings.ts +30 -9
- package/dist/commonjs/index.js.map +0 -1
- package/dist/commonjs/internal.js.map +0 -1
- package/dist/commonjs/package.json +0 -1
- package/dist/commonjs/plugin.js.map +0 -1
- package/dist/commonjs/types/config.js.map +0 -1
- package/dist/module/index.js.map +0 -1
- package/dist/module/internal.js.map +0 -1
- package/dist/module/package.json +0 -1
- package/dist/module/plugin.js.map +0 -1
- package/dist/module/types/config.js.map +0 -1
- package/dist/typescript/commonjs/package.json +0 -1
- package/dist/typescript/commonjs/src/NativeGtReactNative.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/errors-dir/constants.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/errors-dir/errors.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/errors-dir/warnings.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/index.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/internal.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/plugin-dir/index.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/plugin-dir/types.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/plugin-dir/utils/resolveLocales.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/plugin.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/provider/GTProvider.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/provider/hooks/locale/useDetermineLocale.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/provider/hooks/useRegionState.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/tools/testLocalePolyfill.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/types/config.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/utils/getNativeLocales.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/utils/nativeStore.d.ts.map +0 -1
- package/dist/typescript/commonjs/src/utils/utils.d.ts.map +0 -1
- package/dist/typescript/module/package.json +0 -1
- package/dist/typescript/module/src/NativeGtReactNative.d.ts +0 -10
- package/dist/typescript/module/src/NativeGtReactNative.d.ts.map +0 -1
- package/dist/typescript/module/src/errors-dir/constants.d.ts +0 -2
- package/dist/typescript/module/src/errors-dir/constants.d.ts.map +0 -1
- package/dist/typescript/module/src/errors-dir/errors.d.ts +0 -2
- package/dist/typescript/module/src/errors-dir/errors.d.ts.map +0 -1
- package/dist/typescript/module/src/errors-dir/warnings.d.ts +0 -5
- package/dist/typescript/module/src/errors-dir/warnings.d.ts.map +0 -1
- package/dist/typescript/module/src/index.d.ts +0 -5
- package/dist/typescript/module/src/index.d.ts.map +0 -1
- package/dist/typescript/module/src/internal.d.ts +0 -2
- package/dist/typescript/module/src/internal.d.ts.map +0 -1
- package/dist/typescript/module/src/plugin-dir/index.d.ts +0 -6
- package/dist/typescript/module/src/plugin-dir/index.d.ts.map +0 -1
- package/dist/typescript/module/src/plugin-dir/types.d.ts +0 -13
- package/dist/typescript/module/src/plugin-dir/types.d.ts.map +0 -1
- package/dist/typescript/module/src/plugin-dir/utils/resolveLocales.d.ts +0 -13
- package/dist/typescript/module/src/plugin-dir/utils/resolveLocales.d.ts.map +0 -1
- package/dist/typescript/module/src/plugin.d.ts +0 -2
- package/dist/typescript/module/src/plugin.d.ts.map +0 -1
- package/dist/typescript/module/src/provider/GTProvider.d.ts +0 -4
- package/dist/typescript/module/src/provider/GTProvider.d.ts.map +0 -1
- package/dist/typescript/module/src/provider/hooks/locale/useDetermineLocale.d.ts +0 -4
- package/dist/typescript/module/src/provider/hooks/locale/useDetermineLocale.d.ts.map +0 -1
- package/dist/typescript/module/src/provider/hooks/useRegionState.d.ts +0 -3
- package/dist/typescript/module/src/provider/hooks/useRegionState.d.ts.map +0 -1
- package/dist/typescript/module/src/tools/testLocalePolyfill.d.ts +0 -8
- package/dist/typescript/module/src/tools/testLocalePolyfill.d.ts.map +0 -1
- package/dist/typescript/module/src/types/config.d.ts +0 -30
- package/dist/typescript/module/src/types/config.d.ts.map +0 -1
- package/dist/typescript/module/src/utils/getNativeLocales.d.ts +0 -10
- package/dist/typescript/module/src/utils/getNativeLocales.d.ts.map +0 -1
- package/dist/typescript/module/src/utils/nativeStore.d.ts +0 -16
- package/dist/typescript/module/src/utils/nativeStore.d.ts.map +0 -1
- package/dist/typescript/module/src/utils/utils.d.ts +0 -3
- package/dist/typescript/module/src/utils/utils.d.ts.map +0 -1
- /package/dist/{typescript/commonjs/src → module}/NativeGtReactNative.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/errors-dir/constants.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/errors-dir/errors.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/index.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/internal.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/plugin-dir/index.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/plugin-dir/types.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/plugin-dir/utils/resolveLocales.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/plugin.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/provider/GTProvider.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/provider/hooks/locale/useDetermineLocale.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/provider/hooks/useRegionState.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/tools/testLocalePolyfill.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/types/config.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/utils/getNativeLocales.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/utils/nativeStore.d.ts +0 -0
- /package/dist/{typescript/commonjs/src → module}/utils/utils.d.ts +0 -0
|
@@ -1,161 +1,99 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import { useEffect, useState } from 'react';
|
|
4
|
-
import { determineLocale, resolveAliasLocale } from '@generaltranslation/format';
|
|
5
|
-
import { libraryDefaultLocale } from 'generaltranslation/internal';
|
|
6
|
-
import { createUnsupportedLocaleWarning } from '@generaltranslation/react-core/errors';
|
|
7
1
|
import { PACKAGE_NAME } from "../../../errors-dir/constants.js";
|
|
8
2
|
import { getNativeLocales } from "../../../utils/getNativeLocales.js";
|
|
9
3
|
import { nativeStoreGet, nativeStoreSet } from "../../../utils/nativeStore.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
localeCookieName,
|
|
61
|
-
customMapping,
|
|
62
|
-
enableI18n
|
|
63
|
-
});
|
|
64
|
-
setLocaleWithoutPersist(newLocale);
|
|
65
|
-
}, [_locale, locale, locales, defaultLocale, localeCookieName, customMapping, enableI18n]);
|
|
66
|
-
return [locale, setLocale];
|
|
4
|
+
import { useEffect, useState } from "react";
|
|
5
|
+
import { determineLocale, resolveAliasLocale } from "@generaltranslation/format";
|
|
6
|
+
import { libraryDefaultLocale } from "generaltranslation/internal";
|
|
7
|
+
import { createUnsupportedLocaleWarning } from "@generaltranslation/react-core/errors";
|
|
8
|
+
//#region src/provider/hooks/locale/useDetermineLocale.ts
|
|
9
|
+
function useDetermineLocale({ locale: _locale = "", defaultLocale = libraryDefaultLocale, locales = [], localeCookieName = "generaltranslation.locale", ssr = false, customMapping, enableI18n }) {
|
|
10
|
+
_locale = resolveAliasLocale(_locale, customMapping);
|
|
11
|
+
locales = locales.map((locale) => resolveAliasLocale(locale, customMapping));
|
|
12
|
+
const initializeLocale = () => {
|
|
13
|
+
if (!enableI18n) return defaultLocale;
|
|
14
|
+
return resolveAliasLocale(ssr ? _locale ? determineLocale(_locale, locales, customMapping) || "" : "" : getNewLocale({
|
|
15
|
+
_locale,
|
|
16
|
+
locale: _locale,
|
|
17
|
+
locales,
|
|
18
|
+
defaultLocale,
|
|
19
|
+
localeCookieName,
|
|
20
|
+
customMapping,
|
|
21
|
+
enableI18n
|
|
22
|
+
}), customMapping);
|
|
23
|
+
};
|
|
24
|
+
const [locale, _setLocale] = useState(initializeLocale());
|
|
25
|
+
const [setLocale, setLocaleWithoutPersist] = createSetLocale({
|
|
26
|
+
locale,
|
|
27
|
+
locales,
|
|
28
|
+
defaultLocale,
|
|
29
|
+
localeCookieName,
|
|
30
|
+
_setLocale,
|
|
31
|
+
customMapping,
|
|
32
|
+
enableI18n
|
|
33
|
+
});
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
setLocaleWithoutPersist(getNewLocale({
|
|
36
|
+
_locale,
|
|
37
|
+
locale,
|
|
38
|
+
locales,
|
|
39
|
+
defaultLocale,
|
|
40
|
+
localeCookieName,
|
|
41
|
+
customMapping,
|
|
42
|
+
enableI18n
|
|
43
|
+
}));
|
|
44
|
+
}, [
|
|
45
|
+
_locale,
|
|
46
|
+
locale,
|
|
47
|
+
locales,
|
|
48
|
+
defaultLocale,
|
|
49
|
+
localeCookieName,
|
|
50
|
+
customMapping,
|
|
51
|
+
enableI18n
|
|
52
|
+
]);
|
|
53
|
+
return [locale, setLocale];
|
|
67
54
|
}
|
|
68
|
-
|
|
69
|
-
// ----- HELPER FUNCTIONS ---- //
|
|
70
|
-
|
|
71
55
|
/**
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
function getNewLocale({
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
// Check for locale in native store
|
|
97
|
-
let storedLocale = nativeStoreGet(localeCookieName);
|
|
98
|
-
if (storedLocale) {
|
|
99
|
-
storedLocale = resolveAliasLocale(storedLocale, customMapping);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// check user's configured locales
|
|
103
|
-
let preferredLocales = getNativeLocales();
|
|
104
|
-
if (preferredLocales.length === 0) preferredLocales = [defaultLocale];
|
|
105
|
-
preferredLocales = preferredLocales.map(l => resolveAliasLocale(l, customMapping));
|
|
106
|
-
|
|
107
|
-
// determine locale
|
|
108
|
-
let newLocale = determineLocale([...(_locale ? [_locale] : []),
|
|
109
|
-
// prefer user passed locale
|
|
110
|
-
...(storedLocale ? [storedLocale] : []),
|
|
111
|
-
// then prefer stored locale
|
|
112
|
-
...preferredLocales // then prefer browser locale
|
|
113
|
-
], locales, customMapping) || defaultLocale;
|
|
114
|
-
if (newLocale) {
|
|
115
|
-
newLocale = resolveAliasLocale(newLocale, customMapping);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// if stored locale not valid, change it back to whatever we use for fallback
|
|
119
|
-
if (storedLocale && storedLocale !== newLocale) {
|
|
120
|
-
nativeStoreSet(localeCookieName, newLocale);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// return new locale
|
|
124
|
-
return newLocale;
|
|
56
|
+
* Choose a locale to use
|
|
57
|
+
* (1) use provided locale
|
|
58
|
+
* (2) use cookie locale
|
|
59
|
+
* (3) use preferred locale
|
|
60
|
+
* (5) fallback to defaultLocale
|
|
61
|
+
* Update the cookie locale to be correct
|
|
62
|
+
*/
|
|
63
|
+
function getNewLocale({ _locale, locale, locales, defaultLocale, localeCookieName, customMapping, enableI18n }) {
|
|
64
|
+
if (!enableI18n) return defaultLocale;
|
|
65
|
+
if (_locale && _locale === locale && determineLocale(_locale, locales, customMapping) === locale) return resolveAliasLocale(_locale, customMapping);
|
|
66
|
+
let storedLocale = nativeStoreGet(localeCookieName);
|
|
67
|
+
if (storedLocale) storedLocale = resolveAliasLocale(storedLocale, customMapping);
|
|
68
|
+
let preferredLocales = getNativeLocales();
|
|
69
|
+
if (preferredLocales.length === 0) preferredLocales = [defaultLocale];
|
|
70
|
+
preferredLocales = preferredLocales.map((l) => resolveAliasLocale(l, customMapping));
|
|
71
|
+
let newLocale = determineLocale([
|
|
72
|
+
..._locale ? [_locale] : [],
|
|
73
|
+
...storedLocale ? [storedLocale] : [],
|
|
74
|
+
...preferredLocales
|
|
75
|
+
], locales, customMapping) || defaultLocale;
|
|
76
|
+
if (newLocale) newLocale = resolveAliasLocale(newLocale, customMapping);
|
|
77
|
+
if (storedLocale && storedLocale !== newLocale) nativeStoreSet(localeCookieName, newLocale);
|
|
78
|
+
return newLocale;
|
|
125
79
|
}
|
|
126
|
-
function createSetLocale({
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// validate locale
|
|
142
|
-
const validatedLocale = resolveAliasLocale(determineLocale(newLocale, locales, customMapping) || locale || defaultLocale, customMapping);
|
|
143
|
-
if (validatedLocale !== newLocale) {
|
|
144
|
-
console.warn(createUnsupportedLocaleWarning(validatedLocale, newLocale, PACKAGE_NAME));
|
|
145
|
-
}
|
|
146
|
-
// persist locale
|
|
147
|
-
_setLocale(validatedLocale);
|
|
148
|
-
return validatedLocale;
|
|
149
|
-
};
|
|
150
|
-
// update locale and persist it in native store
|
|
151
|
-
const setLocale = newLocale => {
|
|
152
|
-
if (!enableI18n) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
newLocale = resolveAliasLocale(newLocale, customMapping);
|
|
156
|
-
const validatedLocale = setLocaleWithoutPersist(newLocale);
|
|
157
|
-
nativeStoreSet(localeCookieName, validatedLocale);
|
|
158
|
-
};
|
|
159
|
-
return [setLocale, setLocaleWithoutPersist];
|
|
80
|
+
function createSetLocale({ locale, locales, defaultLocale, _setLocale, localeCookieName, customMapping, enableI18n }) {
|
|
81
|
+
locale = resolveAliasLocale(locale, customMapping);
|
|
82
|
+
const setLocaleWithoutPersist = (newLocale) => {
|
|
83
|
+
if (!enableI18n) return defaultLocale;
|
|
84
|
+
const validatedLocale = resolveAliasLocale(determineLocale(newLocale, locales, customMapping) || locale || defaultLocale, customMapping);
|
|
85
|
+
if (validatedLocale !== newLocale) console.warn(createUnsupportedLocaleWarning(validatedLocale, newLocale, PACKAGE_NAME));
|
|
86
|
+
_setLocale(validatedLocale);
|
|
87
|
+
return validatedLocale;
|
|
88
|
+
};
|
|
89
|
+
const setLocale = (newLocale) => {
|
|
90
|
+
if (!enableI18n) return;
|
|
91
|
+
newLocale = resolveAliasLocale(newLocale, customMapping);
|
|
92
|
+
nativeStoreSet(localeCookieName, setLocaleWithoutPersist(newLocale));
|
|
93
|
+
};
|
|
94
|
+
return [setLocale, setLocaleWithoutPersist];
|
|
160
95
|
}
|
|
96
|
+
//#endregion
|
|
97
|
+
export { useDetermineLocale };
|
|
98
|
+
|
|
161
99
|
//# sourceMappingURL=useDetermineLocale.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRegionState.d.ts","sourceRoot":"","sources":["../../../../src/provider/hooks/useRegionState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EACrB,MAAM,sCAAsC,CAAC;AAsB9C,wBAAgB,cAAc,CAAC,EAC7B,OAAO,EACP,GAAG,EACH,gBAAgB,GACjB,EAAE,oBAAoB,GAAG,oBAAoB,CAoB7C"}
|
|
@@ -1,43 +1,33 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import { useEffect, useState } from 'react';
|
|
4
1
|
import { nativeStoreGet, nativeStoreSet } from "../../utils/nativeStore.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// if state not valid, change it to newRegion
|
|
14
|
-
if (cookieRegion && cookieRegion !== newRegion) {
|
|
15
|
-
nativeStoreSet(regionCookieName, newRegion || '');
|
|
16
|
-
}
|
|
17
|
-
return newRegion;
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
//#region src/provider/hooks/useRegionState.ts
|
|
4
|
+
function getNewRegion({ _region, regionCookieName }) {
|
|
5
|
+
const cookieRegion = nativeStoreGet(regionCookieName) || void 0;
|
|
6
|
+
const newRegion = _region || cookieRegion;
|
|
7
|
+
if (cookieRegion && cookieRegion !== newRegion) nativeStoreSet(regionCookieName, newRegion || "");
|
|
8
|
+
return newRegion;
|
|
18
9
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
region,
|
|
40
|
-
setRegion
|
|
41
|
-
};
|
|
10
|
+
function useRegionState({ _region, ssr, regionCookieName }) {
|
|
11
|
+
const [region, _setRegion] = useState(ssr ? void 0 : getNewRegion({
|
|
12
|
+
_region,
|
|
13
|
+
regionCookieName
|
|
14
|
+
}));
|
|
15
|
+
const setRegion = (newRegion) => {
|
|
16
|
+
_setRegion(newRegion);
|
|
17
|
+
nativeStoreSet(regionCookieName, newRegion || "");
|
|
18
|
+
};
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
_setRegion(getNewRegion({
|
|
21
|
+
_region,
|
|
22
|
+
regionCookieName
|
|
23
|
+
}));
|
|
24
|
+
}, [_region, regionCookieName]);
|
|
25
|
+
return {
|
|
26
|
+
region,
|
|
27
|
+
setRegion
|
|
28
|
+
};
|
|
42
29
|
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { useRegionState };
|
|
32
|
+
|
|
43
33
|
//# sourceMappingURL=useRegionState.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"useRegionState.js","names":[],"sources":["../../../../src/provider/hooks/useRegionState.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport type {\n UseRegionStateParams,\n UseRegionStateReturn,\n} from '@generaltranslation/react-core/types';\nimport { nativeStoreGet, nativeStoreSet } from '../../utils/nativeStore';\n\nfunction getNewRegion({\n _region,\n regionCookieName,\n}: {\n _region: string | undefined;\n regionCookieName: string;\n}) {\n // Check for region in native store\n const cookieRegion = nativeStoreGet(regionCookieName) || undefined;\n const newRegion = _region || cookieRegion;\n\n // if state not valid, change it to newRegion\n if (cookieRegion && cookieRegion !== newRegion) {\n nativeStoreSet(regionCookieName, newRegion || '');\n }\n\n return newRegion;\n}\n\nexport function useRegionState({\n _region,\n ssr,\n regionCookieName,\n}: UseRegionStateParams): UseRegionStateReturn {\n const [region, _setRegion] = useState<string | undefined>(\n ssr\n ? undefined\n : getNewRegion({\n _region,\n regionCookieName,\n })\n );\n const setRegion = (newRegion: string | undefined) => {\n _setRegion(newRegion);\n nativeStoreSet(regionCookieName, newRegion || '');\n };\n useEffect(() => {\n _setRegion(getNewRegion({ _region, regionCookieName }));\n }, [_region, regionCookieName]);\n return {\n region,\n setRegion,\n };\n}\n"],"mappings":";;;AAOA,SAAS,aAAa,EACpB,SACA,oBAIC;CAED,MAAM,eAAe,eAAe,iBAAiB,IAAI,KAAA;CACzD,MAAM,YAAY,WAAW;AAG7B,KAAI,gBAAgB,iBAAiB,UACnC,gBAAe,kBAAkB,aAAa,GAAG;AAGnD,QAAO;;AAGT,SAAgB,eAAe,EAC7B,SACA,KACA,oBAC6C;CAC7C,MAAM,CAAC,QAAQ,cAAc,SAC3B,MACI,KAAA,IACA,aAAa;EACX;EACA;EACD,CAAC,CACP;CACD,MAAM,aAAa,cAAkC;AACnD,aAAW,UAAU;AACrB,iBAAe,kBAAkB,aAAa,GAAG;;AAEnD,iBAAgB;AACd,aAAW,aAAa;GAAE;GAAS;GAAkB,CAAC,CAAC;IACtD,CAAC,SAAS,iBAAiB,CAAC;AAC/B,QAAO;EACL;EACA;EACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testLocalePolyfill.d.ts","sourceRoot":"","sources":["../../../src/tools/testLocalePolyfill.ts"],"names":[],"mappings":"AAEA,wBAAgB,kBAAkB,CAAC,MAAM,OAAuB;;;;;;EAiM/D"}
|