gettext-universal 1.0.1 → 1.0.3
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/.github/dependabot.yml +9 -0
- package/bin/{gettext-universal.mjs → gettext-universal.js} +1 -1
- package/bin/{po2mjs.mjs → po2mjs.js} +1 -1
- package/package.json +9 -5
- package/peak_flow.yml +4 -0
- package/spec/gettext-universal.spec.js +9 -0
- package/spec/support/jasmine.js +14 -0
- package/src/{config.mjs → config.js} +9 -1
- package/src/{po2mjs.mjs → po2mjs.js} +1 -1
- package/src/{use-translate-expo.mjs → translate.js} +15 -14
- package/src/use-translate-expo.js +13 -0
- /package/src/{scanner.mjs → scanner.js} +0 -0
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"bin": {
|
|
3
|
-
"gettext-universal": "bin/gettext-universal.
|
|
3
|
+
"gettext-universal": "bin/gettext-universal.js"
|
|
4
4
|
},
|
|
5
5
|
"name": "gettext-universal",
|
|
6
|
-
"
|
|
6
|
+
"type": "module",
|
|
7
|
+
"version": "1.0.3",
|
|
7
8
|
"main": "index.js",
|
|
8
9
|
"scripts": {
|
|
9
|
-
"gettext-universal": "node bin/gettext-universal.
|
|
10
|
-
"po2mjs": "node bin/po2mjs.
|
|
11
|
-
"test": "
|
|
10
|
+
"gettext-universal": "node bin/gettext-universal.js",
|
|
11
|
+
"po2mjs": "node bin/po2mjs.js",
|
|
12
|
+
"test": "jasmine"
|
|
12
13
|
},
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
@@ -31,5 +32,8 @@
|
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
|
33
34
|
"expo-localization": "*"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"jasmine": "^5.7.1"
|
|
34
38
|
}
|
|
35
39
|
}
|
package/peak_flow.yml
ADDED
|
@@ -5,7 +5,7 @@ class Config {
|
|
|
5
5
|
|
|
6
6
|
loadTranslationsFromRequireContext(requireContext) {
|
|
7
7
|
for (const localeFile of requireContext.keys()) {
|
|
8
|
-
const match = localeFile.match(/^\.\/([a-z]{2}).
|
|
8
|
+
const match = localeFile.match(/^\.\/([a-z]{2}).js$/)
|
|
9
9
|
|
|
10
10
|
if (!match) {
|
|
11
11
|
throw new Error(`Couldn't detect locale from file: ${localeFile}`)
|
|
@@ -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()
|
|
@@ -21,7 +21,7 @@ export default class Po2Mjs {
|
|
|
21
21
|
async readFile(file, ext) {
|
|
22
22
|
const fullPath = `${this.directory}/${file}`
|
|
23
23
|
const baseName = path.basename(file, ext)
|
|
24
|
-
const jsFilePath = `${this.directory}/${baseName}.
|
|
24
|
+
const jsFilePath = `${this.directory}/${baseName}.js`
|
|
25
25
|
const fileContentBuffer = await fs.readFile(fullPath)
|
|
26
26
|
const fileContent = fileContentBuffer.toString()
|
|
27
27
|
const matches = fileContent.matchAll(/#: (.+?)\nmsgid \"(.+?)\"\nmsgstr \"(.+?)\"\n(\n|$)/g)
|
|
@@ -1,11 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {useLocales} from "expo-localization"
|
|
1
|
+
import config from "./config.js"
|
|
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.js"
|
|
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
|
|
File without changes
|