1 | import { NativeModulesProxy, Platform } from 'expo-core';
|
2 | const { ExpoLocalization } = NativeModulesProxy;
|
3 | const isObject = obj => obj && obj.constructor && obj.constructor === Object;
|
4 | class LocaleStore {
|
5 | constructor(locales) {
|
6 | if (isObject(locales) === false || Object.keys(locales).length <= 1) {
|
7 | throw new Error('Locales input must be an object and non-empty');
|
8 | }
|
9 | const copy = { ...locales };
|
10 | const allGivenLocales = new Set(Object.keys(copy));
|
11 | let currentLocaleName = [...allGivenLocales][0];
|
12 | const defaultLocale = copy[currentLocaleName];
|
13 | const defaultLocaleName = currentLocaleName;
|
14 | const defaultLocalePhrases = new Set(Object.keys(defaultLocale));
|
15 | let currentLocale = defaultLocale;
|
16 | const setLocaleHelper = (newLocaleName, cb) => {
|
17 | if (allGivenLocales.has(newLocaleName) === false) {
|
18 | if (__DEV__) {
|
19 | const s = `${newLocaleName} is not a valid locale, known locales are ${[
|
20 | ...allGivenLocales,
|
21 | ]}`;
|
22 | console.error(s);
|
23 | }
|
24 | }
|
25 | if (newLocaleName === currentLocaleName) {
|
26 | if (__DEV__) {
|
27 | console.warn('New Locale is the same as last, locale not changed');
|
28 | }
|
29 | return;
|
30 | }
|
31 | currentLocale = copy[newLocaleName];
|
32 | currentLocaleName = newLocaleName;
|
33 | cb && cb();
|
34 | };
|
35 | const localizationValueHelper = name => {
|
36 | if (defaultLocalePhrases.has(name)) {
|
37 | const result = currentLocale[name];
|
38 | if (result !== undefined) {
|
39 | return result !== undefined ? result : defaultLocale[name];
|
40 | }
|
41 | else {
|
42 | const fallback = defaultLocale[name];
|
43 | if (fallback === undefined) {
|
44 | if (__DEV__) {
|
45 | let s = `Fallback locale ${defaultLocaleName} is missing a string value for ${name}`;
|
46 | console.error(s);
|
47 | }
|
48 | return '';
|
49 | }
|
50 | return fallback;
|
51 | }
|
52 | }
|
53 | };
|
54 | if (Platform.OS === 'android') {
|
55 | let localizedValues = new Set();
|
56 | for (const values of Object.values(copy)) {
|
57 | Object.keys(values).forEach(s => localizedValues.add(s));
|
58 | }
|
59 | const proxy = {};
|
60 | Object.defineProperty(proxy, 'setLocale', {
|
61 | get() {
|
62 | return setLocaleHelper;
|
63 | },
|
64 | });
|
65 | const methods = new Set(['setLocale']);
|
66 | for (const name of localizedValues.keys()) {
|
67 | if (methods.has(name)) {
|
68 | throw new Error(`Cannot use ${name} as a locale name`);
|
69 | }
|
70 | Object.defineProperty(proxy, name, {
|
71 | get() {
|
72 | return localizationValueHelper(name);
|
73 | },
|
74 | });
|
75 | }
|
76 | return proxy;
|
77 | }
|
78 | else if (Platform.OS === 'ios') {
|
79 | const handlers = { setLocale: setLocaleHelper };
|
80 | const methods = new Set(Object.keys(handlers));
|
81 | return new Proxy(this, {
|
82 | set() {
|
83 | if (__DEV__) {
|
84 | console.warn('Setting anything directly on the localization store is a no op');
|
85 | }
|
86 | return false;
|
87 | },
|
88 | get(target, name) {
|
89 | if (typeof name !== 'string') {
|
90 | return target[name];
|
91 | }
|
92 | else {
|
93 | if (defaultLocalePhrases.has(name)) {
|
94 | return localizationValueHelper(name);
|
95 | }
|
96 | else if (methods.has(name)) {
|
97 | return handlers[name];
|
98 | }
|
99 | else {
|
100 | return target[name];
|
101 | }
|
102 | }
|
103 | },
|
104 | });
|
105 | }
|
106 | else {
|
107 | throw new Error(`Unsupported platform at moment for localization: ${Platform.OS}`);
|
108 | }
|
109 | }
|
110 | }
|
111 | function warnDeprecated(deprecated, replacement) {
|
112 | console.warn(`Expo.DangerZone.Localization.${deprecated} is deprecated. Use \`Expo.Localization.${replacement}\` instead.`);
|
113 | }
|
114 | export default {
|
115 | ...ExpoLocalization,
|
116 | getCurrentDeviceCountryAsync() {
|
117 | warnDeprecated('getCurrentDeviceCountryAsync()', 'country');
|
118 | return ExpoLocalization.country;
|
119 | },
|
120 | getCurrentLocaleAsync() {
|
121 | warnDeprecated('getCurrentLocaleAsync()', 'locale');
|
122 | return ExpoLocalization.locale.replace('-', '_');
|
123 | },
|
124 | getCurrentTimeZoneAsync() {
|
125 | warnDeprecated('getCurrentTimeZoneAsync()', 'timezone');
|
126 | return ExpoLocalization.timezone;
|
127 | },
|
128 | getPreferredLocalesAsync() {
|
129 | warnDeprecated('getPreferredLocalesAsync()', 'locales');
|
130 | return ExpoLocalization.locales;
|
131 | },
|
132 | getISOCurrencyCodesAsync() {
|
133 | warnDeprecated('getISOCurrencyCodesAsync()', 'isoCurrencyCodes');
|
134 | return ExpoLocalization.isoCurrencyCodes;
|
135 | },
|
136 | LocaleStore,
|
137 | };
|
138 |
|
\ | No newline at end of file |