gettext-universal 1.0.8 → 1.0.9
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/spec/gettext-universal.spec.js +19 -0
- package/src/translate.js +22 -9
- package/src/use-translate-expo.js +3 -3
package/package.json
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
|
+
import config from "../src/config.js"
|
|
1
2
|
import translate from "../src/translate.js"
|
|
2
3
|
|
|
3
4
|
describe("gettext-universal", () => {
|
|
4
5
|
it("falls back to the given msgId", () => {
|
|
6
|
+
config.setLocale("en")
|
|
7
|
+
config.locales = {
|
|
8
|
+
en: {}
|
|
9
|
+
}
|
|
10
|
+
|
|
5
11
|
const result = translate("Hello world")
|
|
6
12
|
|
|
7
13
|
expect(result).toEqual("Hello world")
|
|
8
14
|
})
|
|
15
|
+
|
|
16
|
+
it("replaces placeholders with variables", () => {
|
|
17
|
+
config.setLocale("en")
|
|
18
|
+
config.locales = {
|
|
19
|
+
"en": {
|
|
20
|
+
"Hello name": "Hello %{name}"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const result = translate("Hello name", {name: "Kasper"})
|
|
25
|
+
|
|
26
|
+
expect(result).toEqual("Hello Kasper")
|
|
27
|
+
})
|
|
9
28
|
})
|
package/src/translate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import config from "./config.js"
|
|
2
2
|
|
|
3
|
-
const translate = (msgId, args) => {
|
|
3
|
+
const translate = (msgId, variables, args) => {
|
|
4
4
|
let preferredLocales
|
|
5
5
|
|
|
6
6
|
if (Array.isArray(args)) {
|
|
@@ -22,7 +22,7 @@ const translate = (msgId, args) => {
|
|
|
22
22
|
|
|
23
23
|
let translation
|
|
24
24
|
|
|
25
|
-
for (preferredLocale of preferredLocales) {
|
|
25
|
+
for (const preferredLocale of preferredLocales) {
|
|
26
26
|
const localeTranslations = config.getLocales()[preferredLocale]
|
|
27
27
|
|
|
28
28
|
if (!localeTranslations) continue
|
|
@@ -36,16 +36,20 @@ const translate = (msgId, args) => {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
if (!translation) {
|
|
39
|
-
|
|
40
|
-
const localeTranslations = config.getLocales()[fallback]
|
|
39
|
+
const fallbacks = config.getFallbacks()
|
|
41
40
|
|
|
42
|
-
|
|
41
|
+
if (fallbacks) {
|
|
42
|
+
for (const fallback of config.getFallbacks()) {
|
|
43
|
+
const localeTranslations = config.getLocales()[fallback]
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
if (!localeTranslations) continue
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const localeTranslation = localeTranslations[msgId]
|
|
48
|
+
|
|
49
|
+
if (localeTranslation) {
|
|
50
|
+
translation = localeTranslation
|
|
51
|
+
break
|
|
52
|
+
}
|
|
49
53
|
}
|
|
50
54
|
}
|
|
51
55
|
}
|
|
@@ -58,6 +62,15 @@ const translate = (msgId, args) => {
|
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
64
|
|
|
65
|
+
if (variables) {
|
|
66
|
+
for (const key in variables) {
|
|
67
|
+
const value = variables[key]
|
|
68
|
+
const replaceKey = `%{${key}}`
|
|
69
|
+
|
|
70
|
+
translation = translation.replaceAll(replaceKey, value)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
61
74
|
return translation
|
|
62
75
|
}
|
|
63
76
|
|
|
@@ -66,10 +66,10 @@ const useTranslateExpo = () => {
|
|
|
66
66
|
return preferredLocales
|
|
67
67
|
}, [localeContext?.locale, locales])
|
|
68
68
|
|
|
69
|
-
const currentTranslation = useCallback((msgId, args = {}) => {
|
|
70
|
-
args.locales
|
|
69
|
+
const currentTranslation = useCallback((msgId, variables, args = {}) => {
|
|
70
|
+
args.locales ||= preferredLocales
|
|
71
71
|
|
|
72
|
-
return translate(msgId, args)
|
|
72
|
+
return translate(msgId, variables, args)
|
|
73
73
|
}, [preferredLocales])
|
|
74
74
|
|
|
75
75
|
return currentTranslation
|