Skip to content

Commit 2d65b83

Browse files
committed
fix: i18n
1 parent 4fcc9dd commit 2d65b83

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/i18n/index.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,57 @@ type SupportedLocales = "fr" | "en";
33
class I18n {
44
private locale: SupportedLocales = "fr";
55
private translations: { [key: string]: any } = {};
6+
private isLoaded: boolean = false;
7+
private loadingPromise: Promise<void>;
68

79
constructor() {
8-
this.loadTranslations();
10+
this.loadingPromise = this.loadTranslations();
911
}
1012

1113
private async loadTranslations() {
12-
const { default: fr } = await import("./locales/fr.js");
13-
const { default: en } = await import("./locales/en.js");
14-
this.translations = { fr, en };
14+
try {
15+
const { default: fr } = await import("./locales/fr.js");
16+
const { default: en } = await import("./locales/en.js");
17+
this.translations = { fr, en };
18+
this.isLoaded = true;
19+
} catch (error) {
20+
console.error("Erreur lors du chargement des traductions:", error);
21+
}
22+
}
23+
24+
async waitForLoad() {
25+
await this.loadingPromise;
1526
}
1627

1728
setLocale(locale: SupportedLocales) {
1829
this.locale = locale;
1930
}
2031

2132
t(key: string, params: { [key: string]: any } = {}): string {
33+
if (!this.isLoaded) {
34+
console.warn("Traductions non chargées, retour de la clé:", key);
35+
return key;
36+
}
37+
2238
const keys = key.split(".");
2339
let translation = this.translations[this.locale];
2440

2541
for (const k of keys) {
2642
translation = translation?.[k];
43+
if (translation === undefined) {
44+
console.warn(`Clé de traduction non trouvée: ${key}`);
45+
return key;
46+
}
2747
}
2848

29-
if (!translation) return key;
30-
3149
return this.interpolate(translation, params);
3250
}
3351

3452
private interpolate(text: string, params: { [key: string]: any }): string {
35-
return text.replace(/\{\{(\w+)\}\}/g, (_, key) => params[key]?.toString() || "");
53+
return text.replace(/\{\{(\w+)\}\}/g, (_, key) => {
54+
const value = params[key];
55+
return value !== undefined ? value.toString() : "";
56+
});
3657
}
3758
}
3859

src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const __dirname = new URL(".", import.meta.url).pathname;
3737
async function main() {
3838
let result: any;
3939

40+
// Attendre le chargement des traductions
41+
await i18n.waitForLoad();
42+
// Définir la langue par défaut
43+
i18n.setLocale((process.env.LOCALE as "fr" | "en") || "fr");
44+
4045
// Générer la liste des tâches disponibles
4146
const tasksDir = path.join(__dirname, "tasks");
4247
const taskFiles = fs.readdirSync(tasksDir);
@@ -53,14 +58,14 @@ async function main() {
5358
);
5459

5560
// Demander à l'utilisateur quelle tâche exécuter
56-
const task = await askChoiseListe(i18n.t("common.errors.askTask"), askTask);
61+
const task = await askChoiseListe(i18n.t("common.ask.askTask"), askTask);
5762

5863
// Gestion des fichiers temporaires
5964
const outDir = path.join(__dirname, "..", "outDatas");
6065
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir);
6166
const tempFiles = await getTempJsonFiles(outDir);
6267
const tempData =
63-
tempFiles.length > 0 && (await askUseconfirm(i18n.t("common.errors.askUseTempFile"), true))
68+
tempFiles.length > 0 && (await askUseconfirm(i18n.t("common.ask.askUseTempFile"), true))
6469
? await fs.promises.readFile(path.join(outDir, await askUseTempFile(tempFiles)), "utf-8")
6570
: "";
6671

@@ -96,8 +101,8 @@ async function main() {
96101
// Sauvegarder les résultats en JSON et CSV
97102
await Promise.all([fs.promises.rename(result, jsonFilename), fs.promises.writeFile(csvFilename, csv)]);
98103

99-
console.info(i18n.t("common.errors.infoJsonFileGenerated"), jsonFilename);
100-
console.info(i18n.t("common.errors.infoCsvFileGenerated"), csvFilename);
104+
console.info(i18n.t("common.infos.infoJsonFileGenerated"), jsonFilename);
105+
console.info(i18n.t("common.infos.infoCsvFileGenerated"), csvFilename);
101106
}
102107

103108
main();

0 commit comments

Comments
 (0)