gettext-universal 1.0.3 → 1.0.5
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/src/translate.js +17 -2
- package/src/use-translate-expo.js +13 -4
package/package.json
CHANGED
package/src/translate.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import config from "./config.js"
|
|
2
2
|
|
|
3
|
-
const translate = (msgId,
|
|
3
|
+
const translate = (msgId, args) => {
|
|
4
|
+
let preferredLocales
|
|
5
|
+
|
|
6
|
+
if (Array.isArray(args)) {
|
|
7
|
+
preferredLocales = args
|
|
8
|
+
args = null
|
|
9
|
+
} else if (args?.locales) {
|
|
10
|
+
preferredLocales = args.locales
|
|
11
|
+
}
|
|
12
|
+
|
|
4
13
|
if (!preferredLocales) {
|
|
5
14
|
if (config.getLocale()) {
|
|
6
15
|
preferredLocales = [config.getLocale()]
|
|
@@ -41,7 +50,13 @@ const translate = (msgId, preferredLocales) => {
|
|
|
41
50
|
}
|
|
42
51
|
}
|
|
43
52
|
|
|
44
|
-
if (!translation)
|
|
53
|
+
if (!translation) {
|
|
54
|
+
if (args?.defaultValue) {
|
|
55
|
+
translation = args.defaultValue
|
|
56
|
+
} else {
|
|
57
|
+
translation = msgId
|
|
58
|
+
}
|
|
59
|
+
}
|
|
45
60
|
|
|
46
61
|
return translation
|
|
47
62
|
}
|
|
@@ -2,12 +2,21 @@ import translate from "./translate.js"
|
|
|
2
2
|
import {useCallback} from "react"
|
|
3
3
|
import {useLocales} from "expo-localization"
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const useTranslateExpo = () => {
|
|
6
6
|
const locales = useLocales()
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
let preferredLocales
|
|
8
|
+
|
|
9
|
+
if (Array.isArray(locales)) {
|
|
10
|
+
preferredLocales = locales?.map((localeData) => localeData.languageCode)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const currentTranslation = useCallback((msgId, args = {}) => {
|
|
14
|
+
args.locales = preferredLocales
|
|
15
|
+
|
|
16
|
+
return translate(msgId, args)
|
|
17
|
+
}, [preferredLocales])
|
|
9
18
|
|
|
10
19
|
return currentTranslation
|
|
11
20
|
}
|
|
12
21
|
|
|
13
|
-
export default
|
|
22
|
+
export default useTranslateExpo
|