Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/tools/lib/i18n/toJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,32 @@ const assets = require('../../assets-meta.js');

const allLanguages = assets.languages.all;

/**
* The translation system has a configuration whether to return UTF-8 sequences
* or the actual characters. This function inlines UTF-8 sequences to actual characters.
*
* For example, it converts "Keine Produkte erf\u00FCgbar" to "Keine Produkte verfügbar"
* This makes the JSON files more readable and smaller.
*/
function inlineUTF(properties) {
for (const key in properties) {
if (Object.prototype.hasOwnProperty.call(properties, key)) {
try {
// escape double quotes to avoid JSON parse error
const escaped = properties[key].replaceAll("\"", "\\\"");
properties[key] = JSON.parse(`"${escaped}"`); // utilize JSON parser to decode UTF-8 sequences
} catch (e) {
// in case of error, just keep the original string
console.log(`Warning: failed to inline UTF-8 for key "${key}" with value "${properties[key]}"`);
}
}
}
return properties;
}

const convertToJSON = async (file, distPath) => {
const properties = PropertiesReader(file)._properties;
inlineUTF(properties);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Lets assign that to properties because it's not very intuitive that properties will be object

const filename = path.basename(file, path.extname(file));
const language = filename.match(/^messagebundle_(.*?)$/)[1];
if (!allLanguages.includes(language)) {
Expand Down
Loading