gettext-universal 1.0.0 → 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 +4 -1
- package/peak_flow.yml +4 -0
- package/src/config.mjs +8 -0
- package/src/{use-translate.mjs → translate.mjs} +15 -14
- package/src/use-translate-expo.mjs +13 -0
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"gettext-universal": "bin/gettext-universal.mjs"
|
|
4
4
|
},
|
|
5
5
|
"name": "gettext-universal",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.2",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"gettext-universal": "node bin/gettext-universal.mjs",
|
|
@@ -28,5 +28,8 @@
|
|
|
28
28
|
"description": "",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"diggerize": "^1.0.5"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"expo-localization": "*"
|
|
31
34
|
}
|
|
32
35
|
}
|
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()
|
|
@@ -1,11 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {useLocales} from "expo-localization"
|
|
1
|
+
import config from "./config.mjs"
|
|
3
2
|
|
|
4
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
|
+
|
|
5
14
|
let translation
|
|
6
15
|
|
|
7
16
|
for (preferredLocale of preferredLocales) {
|
|
8
|
-
const localeTranslations =
|
|
17
|
+
const localeTranslations = config.getLocales()[preferredLocale]
|
|
9
18
|
|
|
10
19
|
if (!localeTranslations) continue
|
|
11
20
|
|
|
@@ -18,8 +27,8 @@ const translate = (msgId, preferredLocales) => {
|
|
|
18
27
|
}
|
|
19
28
|
|
|
20
29
|
if (!translation) {
|
|
21
|
-
for (const fallback of
|
|
22
|
-
const localeTranslations =
|
|
30
|
+
for (const fallback of config.getFallbacks()) {
|
|
31
|
+
const localeTranslations = config.getLocales()[fallback]
|
|
23
32
|
|
|
24
33
|
if (!localeTranslations) continue
|
|
25
34
|
|
|
@@ -37,12 +46,4 @@ const translate = (msgId, preferredLocales) => {
|
|
|
37
46
|
return translation
|
|
38
47
|
}
|
|
39
48
|
|
|
40
|
-
|
|
41
|
-
const locales = useLocales()
|
|
42
|
-
const preferredLocales = locales.map((localeData) => localeData.languageCode)
|
|
43
|
-
const currentTranslation = useCallback((msgId) => translate(msgId, preferredLocales), [preferredLocales])
|
|
44
|
-
|
|
45
|
-
return currentTranslation
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default useTranslate
|
|
49
|
+
export default translate
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import translate from "./translate.mjs"
|
|
2
|
+
import {useCallback} from "react"
|
|
3
|
+
import {useLocales} from "expo-localization"
|
|
4
|
+
|
|
5
|
+
const useTranslate = () => {
|
|
6
|
+
const locales = useLocales()
|
|
7
|
+
const preferredLocales = locales.map((localeData) => localeData.languageCode)
|
|
8
|
+
const currentTranslation = useCallback((msgId) => translate(msgId, preferredLocales), [preferredLocales])
|
|
9
|
+
|
|
10
|
+
return currentTranslation
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default useTranslate
|