@remvst/localization 1.0.1 → 2.0.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/.github/workflows/check.yaml +1 -1
- package/.prettierignore +1 -0
- package/README.md +10 -7
- package/lib/commands/find-strings.d.ts +5 -0
- package/lib/commands/find-strings.js +26 -0
- package/lib/commands/localize.d.ts +9 -0
- package/lib/commands/localize.js +67 -0
- package/lib/commands/to-typescript.d.ts +2 -1
- package/lib/commands/to-typescript.js +14 -4
- package/lib/index.js +63 -94
- package/package.json +11 -6
- package/src/commands/find-strings.ts +38 -0
- package/src/commands/localize.ts +104 -0
- package/src/commands/to-typescript.ts +25 -7
- package/src/index.ts +89 -113
- package/test/my-script.js +3 -0
- package/tsconfig.json +12 -12
- package/lib/commands/combine-json.d.ts +0 -7
- package/lib/commands/combine-json.js +0 -30
- package/lib/commands/google-translate.d.ts +0 -6
- package/lib/commands/google-translate.js +0 -20
- package/lib/commands/parse-csv.d.ts +0 -7
- package/lib/commands/parse-csv.js +0 -31
- package/lib/model/translation-set.d.ts +0 -21
- package/lib/model/translation-set.js +0 -92
- package/src/commands/combine-json.ts +0 -39
- package/src/commands/google-translate.ts +0 -32
- package/src/commands/parse-csv.ts +0 -41
- package/src/model/translation-set.ts +0 -119
- package/test/localization.json +0 -15
- package/test/polyglot.csv +0 -645
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
export type Key = string;
|
|
2
|
-
export type Locale = string;
|
|
3
|
-
export type LocalizedItem = Map<Locale, string>;
|
|
4
|
-
|
|
5
|
-
export class TranslationSet {
|
|
6
|
-
|
|
7
|
-
private readonly locales = new Set<Locale>();
|
|
8
|
-
private readonly translations = new Map<Key, LocalizedItem>();
|
|
9
|
-
|
|
10
|
-
add(
|
|
11
|
-
key: Key,
|
|
12
|
-
locale: Locale,
|
|
13
|
-
localization: string,
|
|
14
|
-
) {
|
|
15
|
-
this.locales.add(locale);
|
|
16
|
-
|
|
17
|
-
if (!this.translations.has(key)) {
|
|
18
|
-
this.translations.set(key, new Map());
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
this.translations.get(key)!.set(locale, localization);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
fromLocalization(
|
|
25
|
-
language: Locale,
|
|
26
|
-
localization: string,
|
|
27
|
-
): LocalizedItem | null {
|
|
28
|
-
const search = localization.toLowerCase();
|
|
29
|
-
for (const localizedItem of this.translations.values()) {
|
|
30
|
-
if (localizedItem.get(language)?.toLowerCase() === search) {
|
|
31
|
-
return localizedItem;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
toJSON() {
|
|
38
|
-
const res: {[key: string]: {[key: string]: string}} = {};
|
|
39
|
-
for (const [key, localizedItem] of this.translations.entries()) {
|
|
40
|
-
res[key] = {};
|
|
41
|
-
for (const [locale, translation] of localizedItem.entries()) {
|
|
42
|
-
res[key][locale] = translation;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return res;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
static fromJSON(json: {[key: string]: {[key: string]: string}}): TranslationSet {
|
|
49
|
-
const res = new TranslationSet();
|
|
50
|
-
for (const [key, localizedItem] of Object.entries(json)) {
|
|
51
|
-
for (const [locale, translation] of Object.entries(localizedItem)) {
|
|
52
|
-
res.add(key, locale, translation);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return res;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
toTypeScript(): string {
|
|
59
|
-
let generatedFileContent = '';
|
|
60
|
-
|
|
61
|
-
generatedFileContent += `export type Locale = ${Array.from(this.locales).map(key => JSON.stringify(key)).join(' | ')};\n\n`;
|
|
62
|
-
generatedFileContent += `export type LocalizedKey = ${Array.from(this.translations.keys()).map(key => JSON.stringify(key)).join(' | ')};\n\n`;
|
|
63
|
-
generatedFileContent += `export type LocalizationItem = {[key in Locale]?: string};\n\n`;
|
|
64
|
-
|
|
65
|
-
generatedFileContent += 'export const LOCALIZATION: {[key in LocalizedKey]: LocalizationItem} = {\n';
|
|
66
|
-
|
|
67
|
-
for (const [key, localizedItem] of this.translations.entries()) {
|
|
68
|
-
generatedFileContent += ` ${JSON.stringify(key)}: {\n`;
|
|
69
|
-
|
|
70
|
-
for (const [locale, translation] of localizedItem.entries()) {
|
|
71
|
-
if (!translation) continue;
|
|
72
|
-
generatedFileContent += ` ${JSON.stringify(locale)}: ${JSON.stringify(translation)},\n`;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
generatedFileContent += ` },\n`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
generatedFileContent += '};\n\n';
|
|
79
|
-
|
|
80
|
-
generatedFileContent += `let LOCALE: Locale = 'en';\n\n`;
|
|
81
|
-
|
|
82
|
-
generatedFileContent += `export function setLocale(locale: string) {\n`;
|
|
83
|
-
generatedFileContent += ` LOCALE = locale.split('-')[0] as Locale\n`;
|
|
84
|
-
generatedFileContent += `}\n\n`;
|
|
85
|
-
|
|
86
|
-
generatedFileContent += `export function localize(key: LocalizedKey) {\n`;
|
|
87
|
-
generatedFileContent += ` const localizationItem: LocalizationItem = LOCALIZATION[key];\n`;
|
|
88
|
-
generatedFileContent += ` return localizationItem[LOCALE] || localizationItem.en;\n`;
|
|
89
|
-
generatedFileContent += `}\n`;
|
|
90
|
-
|
|
91
|
-
return generatedFileContent;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async applyFallbacks(
|
|
95
|
-
locales: Locale[],
|
|
96
|
-
fallback: (key: Key, locale: Locale, localizedItem: LocalizedItem) => Promise<string | null>,
|
|
97
|
-
) {
|
|
98
|
-
for (const [key, localizedItem] of this.translations.entries()) {
|
|
99
|
-
for (const locale of locales) {
|
|
100
|
-
let translation = localizedItem.get(locale) || null;
|
|
101
|
-
if (translation) continue;
|
|
102
|
-
|
|
103
|
-
try {
|
|
104
|
-
translation = await fallback(key, locale, localizedItem);
|
|
105
|
-
} catch (err) {
|
|
106
|
-
console.error(err);
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (!translation) {
|
|
111
|
-
console.warn(`Unable to find fallback for ${key}`);
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
this.add(key, locale, translation);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
package/test/localization.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"ok": { "en": "Okay" },
|
|
3
|
-
"confirm": { "en": "Confirm" },
|
|
4
|
-
"cancel": { "en": "Cancel" },
|
|
5
|
-
"back": { "en": "Back" },
|
|
6
|
-
"play": { "en": "Play" },
|
|
7
|
-
"time": { "en": "Time" },
|
|
8
|
-
"some-Fancy-Key?": { "en": "Fancy key" },
|
|
9
|
-
"backToMainMenu": { "en": "Back To Main Menu", "fr": "Retour au menu principal" },
|
|
10
|
-
"difficulty": { "en": "Difficulty" },
|
|
11
|
-
"difficultyNormal": { "en": "Normal" },
|
|
12
|
-
"difficultyHard": { "en": "Hard" },
|
|
13
|
-
"difficultyEasy": { "en": "Easy" },
|
|
14
|
-
"difficultyVeryEasy": { "en": "Very Easy" }
|
|
15
|
-
}
|