@kemdict/gettext 0.1.3 → 0.2.0
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/README.md +8 -2
- package/lib/loaders-node.js +54 -0
- package/lib/loaders.js +12 -42
- package/package.json +5 -1
- package/types/loaders-node.d.ts +17 -0
- package/types/loaders-node.d.ts.map +1 -0
- package/types/loaders.d.ts +6 -14
- package/types/loaders.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -66,7 +66,7 @@ gettext('The world is a funny place')
|
|
|
66
66
|
|
|
67
67
|
```js
|
|
68
68
|
import Gettext from '@kemdict/gettext'
|
|
69
|
-
import { loadTranslations } from '@kemdict/gettext/loaders'
|
|
69
|
+
import { loadTranslations } from '@kemdict/gettext/loaders-node'
|
|
70
70
|
const gt = new Gettext({
|
|
71
71
|
// this reads and parses all .po files under path/to/locales
|
|
72
72
|
// their filenames are the locale names ('de.po' creates an entry for 'de')
|
|
@@ -124,7 +124,7 @@ Translate a string with context like pgettext.
|
|
|
124
124
|
|
|
125
125
|
Translate a string with context and plural handling like npgettext.
|
|
126
126
|
|
|
127
|
-
### Loaders ("@kemdict/gettext/loaders.js")
|
|
127
|
+
### Loaders that require Node-compatible environments ("@kemdict/gettext/loaders-node.js")
|
|
128
128
|
|
|
129
129
|
#### bindtextdomain(domain, ...localesDirs) → Record<string, GetTextTranslations>
|
|
130
130
|
|
|
@@ -146,6 +146,12 @@ pgettext("@title:window", "Choose Folder") // 選擇資料夾 in zh_TW
|
|
|
146
146
|
|
|
147
147
|
Load PO files from `dir`. `dir` should contain one PO file for each locale, like `<dir>/zh_TW.po`, `<dir>/de.po`, `<dir>/sv.po`, and so on.
|
|
148
148
|
|
|
149
|
+
### Loaders that don't ("@kemdict/gettext/loaders.js")
|
|
150
|
+
|
|
151
|
+
#### loadTranslationsFromObject(obj) → Record<string, GetTextTranslations>
|
|
152
|
+
|
|
153
|
+
Take an object like the result of doing `import.meta.glob("*.po", { eager: true, query: "?raw" })` in Vite, then return an object mapping language codes to parsed translations. There should be one entry for each locale, like `zh_TW.po`, `de.po`, `sv.po`, and so on.
|
|
154
|
+
|
|
149
155
|
## License
|
|
150
156
|
|
|
151
157
|
MIT
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** @import {Catalog, Locale} from "./gettext.js" */
|
|
2
|
+
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { po, mo } from "gettext-parser";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Load MO translations the directories `localesDirs`.
|
|
9
|
+
*
|
|
10
|
+
* These directories should be arranged similar to /usr/share/locale, like
|
|
11
|
+
* <dir>/<locale>/LC_MESSAGES/<domain>.mo.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} domain
|
|
14
|
+
* @param {...string} localesDirs
|
|
15
|
+
*/
|
|
16
|
+
export function bindtextdomain(domain, ...localesDirs) {
|
|
17
|
+
/** @type Record<Locale, Catalog> */
|
|
18
|
+
const catalogs = {};
|
|
19
|
+
for (const localesDir of localesDirs) {
|
|
20
|
+
if (!fs.existsSync(localesDir)) continue;
|
|
21
|
+
for (const locale of fs.readdirSync(localesDir)) {
|
|
22
|
+
const localePath = path.join(localesDir, locale);
|
|
23
|
+
if (!fs.statSync(localePath).isDirectory()) continue;
|
|
24
|
+
const messagesPath = path.join(localePath, "LC_MESSAGES");
|
|
25
|
+
if (!fs.existsSync(messagesPath)) continue;
|
|
26
|
+
if (!fs.statSync(messagesPath).isDirectory()) continue;
|
|
27
|
+
const domainPath = path.join(messagesPath, `${domain}.mo`);
|
|
28
|
+
if (!fs.existsSync(domainPath)) continue;
|
|
29
|
+
|
|
30
|
+
const translations = mo.parse(fs.readFileSync(domainPath));
|
|
31
|
+
catalogs[locale] = translations;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return catalogs;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Load PO translations from `dir`.
|
|
39
|
+
* `dir` should be structured like <dir>/<locale>.po.
|
|
40
|
+
* @param {string} dir
|
|
41
|
+
*/
|
|
42
|
+
export function loadTranslations(dir) {
|
|
43
|
+
/** @type Record<Locale, Catalog> */
|
|
44
|
+
const catalogs = {};
|
|
45
|
+
for (const file of fs.readdirSync(dir)) {
|
|
46
|
+
if (file.endsWith(".po")) {
|
|
47
|
+
const translations = po.parse(
|
|
48
|
+
fs.readFileSync(path.join(dir, file)),
|
|
49
|
+
);
|
|
50
|
+
catalogs[file.slice(0, -3)] = translations;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return catalogs;
|
|
54
|
+
}
|
package/lib/loaders.js
CHANGED
|
@@ -1,53 +1,23 @@
|
|
|
1
1
|
/** @import {Catalog, Locale} from "./gettext.js" */
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import * as path from "node:path";
|
|
5
|
-
import { po, mo } from "gettext-parser";
|
|
3
|
+
import { po } from "gettext-parser";
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
|
-
* Load
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* <dir>/<locale>/LC_MESSAGES/<domain>.mo.
|
|
12
|
-
*
|
|
13
|
-
* @param {string} domain
|
|
14
|
-
* @param {...string} localesDirs
|
|
6
|
+
* Load PO translations from an object mapping file paths to content strings,
|
|
7
|
+
* such as those from Vite's import.meta.glob.
|
|
8
|
+
* @param {Record<string, string | { default: string }>} obj
|
|
15
9
|
*/
|
|
16
|
-
export function
|
|
10
|
+
export function loadTranslationsFromObject(obj) {
|
|
17
11
|
/** @type Record<Locale, Catalog> */
|
|
18
12
|
const catalogs = {};
|
|
19
|
-
for (const
|
|
20
|
-
if (!fs.existsSync(localesDir)) continue;
|
|
21
|
-
for (const locale of fs.readdirSync(localesDir)) {
|
|
22
|
-
const localePath = path.join(localesDir, locale);
|
|
23
|
-
if (!fs.statSync(localePath).isDirectory()) continue;
|
|
24
|
-
const messagesPath = path.join(localePath, "LC_MESSAGES");
|
|
25
|
-
if (!fs.existsSync(messagesPath)) continue;
|
|
26
|
-
if (!fs.statSync(messagesPath).isDirectory()) continue;
|
|
27
|
-
const domainPath = path.join(messagesPath, `${domain}.mo`);
|
|
28
|
-
if (!fs.existsSync(domainPath)) continue;
|
|
29
|
-
|
|
30
|
-
const translations = mo.parse(fs.readFileSync(domainPath));
|
|
31
|
-
catalogs[locale] = translations;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return catalogs;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Load PO translations from `dir`.
|
|
39
|
-
* `dir` should be structured like <dir>/<locale>.po.
|
|
40
|
-
* @param {string} dir
|
|
41
|
-
*/
|
|
42
|
-
export function loadTranslations(dir) {
|
|
43
|
-
/** @type Record<Locale, Catalog> */
|
|
44
|
-
const catalogs = {};
|
|
45
|
-
for (const file of fs.readdirSync(dir)) {
|
|
13
|
+
for (const [file, content] of Object.entries(obj)) {
|
|
46
14
|
if (file.endsWith(".po")) {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
);
|
|
50
|
-
|
|
15
|
+
const actualContent =
|
|
16
|
+
typeof content === "string" ? content : content.default;
|
|
17
|
+
const translations = po.parse(actualContent);
|
|
18
|
+
// We don't have access to node:path.basename here.
|
|
19
|
+
const basename = file.replace(/^.*[\/\\]/, "").slice(0, -3);
|
|
20
|
+
catalogs[basename] = translations;
|
|
51
21
|
}
|
|
52
22
|
}
|
|
53
23
|
return catalogs;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kemdict/gettext",
|
|
3
3
|
"description": "A JavaScript implementation of gettext, a localization framework",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -22,6 +22,10 @@
|
|
|
22
22
|
"types": "./types/gettext.d.ts",
|
|
23
23
|
"default": "./lib/gettext.js"
|
|
24
24
|
},
|
|
25
|
+
"./loaders-node.js": {
|
|
26
|
+
"types": "./types/loaders-node.d.ts",
|
|
27
|
+
"default": "./lib/loaders-node.js"
|
|
28
|
+
},
|
|
25
29
|
"./loaders.js": {
|
|
26
30
|
"types": "./types/loaders.d.ts",
|
|
27
31
|
"default": "./lib/loaders.js"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load MO translations the directories `localesDirs`.
|
|
3
|
+
*
|
|
4
|
+
* These directories should be arranged similar to /usr/share/locale, like
|
|
5
|
+
* <dir>/<locale>/LC_MESSAGES/<domain>.mo.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} domain
|
|
8
|
+
* @param {...string} localesDirs
|
|
9
|
+
*/
|
|
10
|
+
export function bindtextdomain(domain: string, ...localesDirs: string[]): Record<string, import("gettext-parser").GetTextTranslations>;
|
|
11
|
+
/**
|
|
12
|
+
* Load PO translations from `dir`.
|
|
13
|
+
* `dir` should be structured like <dir>/<locale>.po.
|
|
14
|
+
* @param {string} dir
|
|
15
|
+
*/
|
|
16
|
+
export function loadTranslations(dir: string): Record<string, import("gettext-parser").GetTextTranslations>;
|
|
17
|
+
//# sourceMappingURL=loaders-node.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loaders-node.d.ts","sourceRoot":"","sources":["../lib/loaders-node.js"],"names":[],"mappings":"AAMA;;;;;;;;GAQG;AACH,uCAHW,MAAM,kBACH,MAAM,EAAA,gEAqBnB;AAED;;;;GAIG;AACH,sCAFW,MAAM,gEAchB"}
|
package/types/loaders.d.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Load
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* <dir>/<locale>/LC_MESSAGES/<domain>.mo.
|
|
6
|
-
*
|
|
7
|
-
* @param {string} domain
|
|
8
|
-
* @param {...string} localesDirs
|
|
2
|
+
* Load PO translations from an object mapping file paths to content strings,
|
|
3
|
+
* such as those from Vite's import.meta.glob.
|
|
4
|
+
* @param {Record<string, string | { default: string }>} obj
|
|
9
5
|
*/
|
|
10
|
-
export function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* `dir` should be structured like <dir>/<locale>.po.
|
|
14
|
-
* @param {string} dir
|
|
15
|
-
*/
|
|
16
|
-
export function loadTranslations(dir: string): Record<string, import("gettext-parser").GetTextTranslations>;
|
|
6
|
+
export function loadTranslationsFromObject(obj: Record<string, string | {
|
|
7
|
+
default: string;
|
|
8
|
+
}>): Record<string, import("gettext-parser").GetTextTranslations>;
|
|
17
9
|
//# sourceMappingURL=loaders.d.ts.map
|
package/types/loaders.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loaders.d.ts","sourceRoot":"","sources":["../lib/loaders.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"loaders.d.ts","sourceRoot":"","sources":["../lib/loaders.js"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,gDAFW,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,gEAgBtD"}
|