I'm trying to use NestJS-i18n for internationalization in my NestJS application. Currently, I have all my translation keys and values stored in a JSON file. However, I have a requirement to switch to an Excel file for managing translations.
I've been searching for a way to do this, but I haven't found any documentation or examples on how to use an Excel file with NestJS-i18n.
Does anyone have any ideas or suggestions on how I could achieve this? Any help would be greatly appreciated. Thank you.
you can convert your excel file to Json using xlsx
package:
here enter code hereis an example of parser:
use sheet_to_json
method for parsing xcell to json
Key | en | fr |
---|---|---|
greeting | Hello | Bonjour |
welcome_message | Welcome to | Bienvenue à |
import * as xlsx from 'xlsx';
export class ExcelParser {
constructor(private readonly options: ExcelParserOptions) {}
async parse(): Promise<Record<string, any>> {
const workbook = xlsx.readFile(this.options.path);
const worksheet = workbook.Sheets[this.options.sheetName];
const data : any[] = xlsx.utils.sheet_to_json(worksheet, {
header: 1,
raw: false,
});
const keys = data[0].slice(1);
const translations = {};
for (let i = 1; i < data.length; i++) {
const row = data[i];
const key = row[0];
translations[key] = {};
for (let j = 1; j < keys.length + 1; j++) {
const language = keys[j - 1];
const value = row[j];
translations[key][language] = value;
}
}
return translations;
}
}
export interface ExcelParserOptions {
path: string;
sheetName: string;
}