gettext-universal 1.0.1 → 1.0.2
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/package.json +1 -1
- package/peak_flow.yml +4 -0
- package/src/config.mjs +8 -0
- package/src/translate.mjs +49 -0
- package/src/use-translate-expo.mjs +1 -36
package/package.json
CHANGED
package/peak_flow.yml
ADDED
package/src/config.mjs
CHANGED
|
@@ -18,9 +18,17 @@ class Config {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
getFallbacks = () => this.fallbacks
|
|
22
|
+
getLocale = () => this.locale
|
|
23
|
+
getLocales = () => this.locales
|
|
24
|
+
|
|
21
25
|
setFallbacks(fallbacks) {
|
|
22
26
|
this.fallbacks = fallbacks
|
|
23
27
|
}
|
|
28
|
+
|
|
29
|
+
setLocale(locale) {
|
|
30
|
+
this.locale = locale
|
|
31
|
+
}
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
const config = new Config()
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import config from "./config.mjs"
|
|
2
|
+
|
|
3
|
+
const translate = (msgId, preferredLocales) => {
|
|
4
|
+
if (!preferredLocales) {
|
|
5
|
+
if (config.getLocale()) {
|
|
6
|
+
preferredLocales = [config.getLocale()]
|
|
7
|
+
} else {
|
|
8
|
+
console.error("No 'preferredLocales' was given and a locale wasn't set in the configuration either")
|
|
9
|
+
|
|
10
|
+
return msgId
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let translation
|
|
15
|
+
|
|
16
|
+
for (preferredLocale of preferredLocales) {
|
|
17
|
+
const localeTranslations = config.getLocales()[preferredLocale]
|
|
18
|
+
|
|
19
|
+
if (!localeTranslations) continue
|
|
20
|
+
|
|
21
|
+
const localeTranslation = localeTranslations[msgId]
|
|
22
|
+
|
|
23
|
+
if (localeTranslation) {
|
|
24
|
+
translation = localeTranslation
|
|
25
|
+
break
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!translation) {
|
|
30
|
+
for (const fallback of config.getFallbacks()) {
|
|
31
|
+
const localeTranslations = config.getLocales()[fallback]
|
|
32
|
+
|
|
33
|
+
if (!localeTranslations) continue
|
|
34
|
+
|
|
35
|
+
const localeTranslation = localeTranslations[msgId]
|
|
36
|
+
|
|
37
|
+
if (localeTranslation) {
|
|
38
|
+
translation = localeTranslation
|
|
39
|
+
break
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!translation) translation = msgId
|
|
45
|
+
|
|
46
|
+
return translation
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default translate
|
|
@@ -1,42 +1,7 @@
|
|
|
1
|
+
import translate from "./translate.mjs"
|
|
1
2
|
import {useCallback} from "react"
|
|
2
3
|
import {useLocales} from "expo-localization"
|
|
3
4
|
|
|
4
|
-
const translate = (msgId, preferredLocales) => {
|
|
5
|
-
let translation
|
|
6
|
-
|
|
7
|
-
for (preferredLocale of preferredLocales) {
|
|
8
|
-
const localeTranslations = locales[preferredLocale]
|
|
9
|
-
|
|
10
|
-
if (!localeTranslations) continue
|
|
11
|
-
|
|
12
|
-
const localeTranslation = localeTranslations[msgId]
|
|
13
|
-
|
|
14
|
-
if (localeTranslation) {
|
|
15
|
-
translation = localeTranslation
|
|
16
|
-
break
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (!translation) {
|
|
21
|
-
for (const fallback of fallbacks) {
|
|
22
|
-
const localeTranslations = locales[fallback]
|
|
23
|
-
|
|
24
|
-
if (!localeTranslations) continue
|
|
25
|
-
|
|
26
|
-
const localeTranslation = localeTranslations[msgId]
|
|
27
|
-
|
|
28
|
-
if (localeTranslation) {
|
|
29
|
-
translation = localeTranslation
|
|
30
|
-
break
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (!translation) translation = msgId
|
|
36
|
-
|
|
37
|
-
return translation
|
|
38
|
-
}
|
|
39
|
-
|
|
40
5
|
const useTranslate = () => {
|
|
41
6
|
const locales = useLocales()
|
|
42
7
|
const preferredLocales = locales.map((localeData) => localeData.languageCode)
|