Skip to main content

export to_xlsx

Export i18n files into a xlsx file, created by exceljs

Command

# Display help for export to_xlsx
npx @jy95/i18n-tools export to_xlsx --help

Purpose

Suppose you have several i18n locales such as :

fr.json
{
"commons":{
"myNestedKey":"Hello world FR",
"myNestedArray":[
"1 FR",
"2 FR",
"3 FR"
]
},
"array":[
"1 FR",
"2 FR",
"3 FR"
],
"simpleKey":"[FR] not setted key",
"Key with spaces":[
{
"test":"42 is the answer"
}
],
"Missing key in DE":"present"
}

This command helps you to turn them into a single xlsx file such as this one.

Export example

Examples of settings

npx @jy95/i18n-tools export to_xlsx --settings "/absolutePath/to/settings1.json"
settings1.json
{
"files":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\files.json",
"columns":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\columns.json",
"worksheetName":"Settings 1 - Worksheet",
"filename":"settings1-output",
"outputDir":"D:\\TEMP\\TEMP"
}
files.json
{
"FR":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\fr.json",
"NL":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\nl.json",
"DE":"D:\\TEMP\\TEMP\\tests-for-export\\correct\\de.json"
}
columns.json
[
{
"locale":"FR",
"label":"French translation"
},
{
"locale":"NL",
"label":"Dutch translation"
},
{
"locale":"DE",
"label":"German translation"
}
]

FAQ

I only want a subset of the data. How can I achieve that ?

Simply add the resultsFilter option in your settings.json or settings.js :

tip

Reminder - the type of the function parameter :

type I18N_Merged_Data = {
technical_key: string;
labels: {
[locale: string]: string;
};
}[];
settings.js
"resultsFilter": function(data /*: I18N_Merged_Data*/) {
return data.filter((row) =>
// Takes rows that have at least a missing label in one i18n file such as "Missing key in DE" case
// Object.keys(row.labels).length !== 3 ||
Object
.values(row.labels)
// Takes rows that have at least one empty label or contains a given prefix
.some(
(label) =>
label.length === 0 ||
["[FR]", "[NL]", "[DE]"].some((prefix) => label.startsWith(prefix))
)
);
}

OR

settings.json
"resultsFilter": "D:\\TEMP\\TEMP\\resultsFilter.js"
resultsFilter.js
module.exports = function(data /*: I18N_Merged_Data*/) {
return data.filter((row) =>
// Takes rows that have at least a missing label in one i18n file such as "Missing key in DE" case
// Object.keys(row.labels).length !== 3 ||
Object
.values(row.labels)
// Takes rows that have at least one empty label or contains a given prefix
.some(
(label) =>
label.length === 0 ||
["[FR]", "[NL]", "[DE]"].some((prefix) => label.startsWith(prefix))
)
);
}
I want the locales in a given order in the result file. How can I achieve that ?

Simply update the columns option with your given order in your settings.json or settings.js, such as :

settings.js
"columns": [
{
"locale":"NL",
"label":"Dutch translation"
},
{
"locale":"FR",
"label":"French translation"
}
]
I only work with flat JSON file(s). How can I make this command work ?

Simply set option keySeparator to false in your settings.json or settings.js, such as :

settings.json
{
"keySeparator": false
}