gettext-universal 1.0.4 → 1.0.6
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 +52 -2
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
|
}
|
|
@@ -1,7 +1,52 @@
|
|
|
1
|
+
import EventEmitter from "events"
|
|
1
2
|
import translate from "./translate.js"
|
|
2
|
-
import {useCallback} from "react"
|
|
3
|
+
import {createContext, useCallback, useMemo, useState} from "react"
|
|
4
|
+
import useEventEmitter from "@kaspernj/api-maker/build/use-event-emitter"
|
|
3
5
|
import {useLocales} from "expo-localization"
|
|
4
6
|
|
|
7
|
+
const eventEmitter = new EventEmitter()
|
|
8
|
+
const TranslateContext = createContext()
|
|
9
|
+
|
|
10
|
+
const WithTranslate = ({children, ...restProps}) => {
|
|
11
|
+
const restPropsKeys = Object.keys(restProps)
|
|
12
|
+
|
|
13
|
+
if (restPropsKeys.length > 0) {
|
|
14
|
+
throw new Error(`Unknown props given: ${restPropsKeys.join(", ")}`)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const locales = useLocales()
|
|
18
|
+
const [locale, setLocale] = useState()
|
|
19
|
+
const actualLocales = useMemo(() => {
|
|
20
|
+
const actualLocales = []
|
|
21
|
+
|
|
22
|
+
if (locale) {
|
|
23
|
+
actualLocales.push(locale)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (const locale of locales) {
|
|
27
|
+
actualLocales.push(locale.languageCode)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return actualLocales
|
|
31
|
+
}, [locale, locales])
|
|
32
|
+
|
|
33
|
+
const contextData = useMemo(() => ({locales: actualLocales}), [actualLocales])
|
|
34
|
+
|
|
35
|
+
const onChangeLocale = useCallback(({locale}) => {
|
|
36
|
+
console.log("onChangeLocale", args)
|
|
37
|
+
|
|
38
|
+
setLocale(locale)
|
|
39
|
+
}, [])
|
|
40
|
+
|
|
41
|
+
useEventEmitter(eventEmitter, "changeLocale", onChangeLocale)
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<TranslateContext.Provider value={contextData}>
|
|
45
|
+
{children}
|
|
46
|
+
</TranslateContext.Provider>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
5
50
|
const useTranslateExpo = () => {
|
|
6
51
|
const locales = useLocales()
|
|
7
52
|
let preferredLocales
|
|
@@ -10,9 +55,14 @@ const useTranslateExpo = () => {
|
|
|
10
55
|
preferredLocales = locales?.map((localeData) => localeData.languageCode)
|
|
11
56
|
}
|
|
12
57
|
|
|
13
|
-
const currentTranslation = useCallback((msgId
|
|
58
|
+
const currentTranslation = useCallback((msgId, args = {}) => {
|
|
59
|
+
args.locales = preferredLocales
|
|
60
|
+
|
|
61
|
+
return translate(msgId, args)
|
|
62
|
+
}, [preferredLocales])
|
|
14
63
|
|
|
15
64
|
return currentTranslation
|
|
16
65
|
}
|
|
17
66
|
|
|
67
|
+
export {WithTranslate}
|
|
18
68
|
export default useTranslateExpo
|