gettext-universal 1.0.7 → 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 CHANGED
@@ -4,7 +4,7 @@
4
4
  },
5
5
  "name": "gettext-universal",
6
6
  "type": "module",
7
- "version": "1.0.7",
7
+ "version": "1.0.9",
8
8
  "main": "index.js",
9
9
  "scripts": {
10
10
  "gettext-universal": "node bin/gettext-universal.js",
@@ -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/config.js CHANGED
@@ -1,3 +1,5 @@
1
+ import events from "./events.js"
2
+
1
3
  class Config {
2
4
  constructor() {
3
5
  this.locales = {}
@@ -28,6 +30,7 @@ class Config {
28
30
 
29
31
  setLocale(locale) {
30
32
  this.locale = locale
33
+ events.emit("onLocaleChange", {locale})
31
34
  }
32
35
  }
33
36
 
package/src/events.js ADDED
@@ -0,0 +1,5 @@
1
+ import EventEmitter from "events"
2
+
3
+ const eventEmitter = new EventEmitter()
4
+
5
+ export default eventEmitter
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
- for (const fallback of config.getFallbacks()) {
40
- const localeTranslations = config.getLocales()[fallback]
39
+ const fallbacks = config.getFallbacks()
41
40
 
42
- if (!localeTranslations) continue
41
+ if (fallbacks) {
42
+ for (const fallback of config.getFallbacks()) {
43
+ const localeTranslations = config.getLocales()[fallback]
43
44
 
44
- const localeTranslation = localeTranslations[msgId]
45
+ if (!localeTranslations) continue
45
46
 
46
- if (localeTranslation) {
47
- translation = localeTranslation
48
- break
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
 
@@ -1,25 +1,15 @@
1
- import {digg} from "diggerize"
2
- import EventEmitter from "events"
1
+ import config from "./config.js"
2
+ import events from "./events.js"
3
3
  import translate from "./translate.js"
4
4
  import {createContext, useCallback, useContext, useMemo, useState} from "react"
5
5
  import useEventEmitter from "@kaspernj/api-maker/build/use-event-emitter"
6
6
  import {useLocales} from "expo-localization"
7
7
 
8
- const eventEmitter = new EventEmitter()
9
8
  const TranslateContext = createContext()
10
9
 
11
- const shared = {
12
- locale: null
13
- }
14
-
15
- const setLocale = (locale) => {
16
- shared.locale = locale
17
- eventEmitter.emit("changeLocale", {locale})
18
- }
19
-
20
10
  const WithTranslate = ({children, ...restProps}) => {
21
11
  const locales = useLocales()
22
- const [locale, setLocale] = useState(digg(shared, "locale"))
12
+ const [locale, setLocale] = useState(config.getLocale())
23
13
 
24
14
  const actualLocales = useMemo(() => {
25
15
  const actualLocales = []
@@ -41,7 +31,7 @@ const WithTranslate = ({children, ...restProps}) => {
41
31
  setLocale(locale)
42
32
  }, [])
43
33
 
44
- useEventEmitter(eventEmitter, "changeLocale", onChangeLocale)
34
+ useEventEmitter(events, "onLocaleChange", onChangeLocale)
45
35
 
46
36
  const restPropsKeys = Object.keys(restProps)
47
37
 
@@ -76,14 +66,14 @@ const useTranslateExpo = () => {
76
66
  return preferredLocales
77
67
  }, [localeContext?.locale, locales])
78
68
 
79
- const currentTranslation = useCallback((msgId, args = {}) => {
80
- args.locales = preferredLocales
69
+ const currentTranslation = useCallback((msgId, variables, args = {}) => {
70
+ args.locales ||= preferredLocales
81
71
 
82
- return translate(msgId, args)
72
+ return translate(msgId, variables, args)
83
73
  }, [preferredLocales])
84
74
 
85
75
  return currentTranslation
86
76
  }
87
77
 
88
- export {setLocale, WithTranslate}
78
+ export {WithTranslate}
89
79
  export default useTranslateExpo