Skip to content

Commit e4fde69

Browse files
committed
fix(i18n): handle utf-8 escape sequences in message bundle properties files
1 parent 43e545a commit e4fde69

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

packages/tools/lib/i18n/toJSON.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,32 @@ const assets = require('../../assets-meta.js');
1414

1515
const allLanguages = assets.languages.all;
1616

17+
/**
18+
* The translation system has a configuration whether to return UTF-8 sequences
19+
* or the actual characters. This function inlines UTF-8 sequences to actual characters.
20+
*
21+
* For example, it converts "Keine Produkte erf\u00FCgbar" to "Keine Produkte verfügbar"
22+
* This makes the JSON files more readable and smaller.
23+
*/
24+
function inlineUTF(properties) {
25+
for (const key in properties) {
26+
if (Object.prototype.hasOwnProperty.call(properties, key)) {
27+
try {
28+
// escape double quotes to avoid JSON parse error
29+
const escaped = properties[key].replaceAll("\"", "\\\"");
30+
properties[key] = JSON.parse(`"${escaped}"`); // utilize JSON parser to decode UTF-8 sequences
31+
} catch (e) {
32+
// in case of error, just keep the original string
33+
console.log(`Warning: failed to inline UTF-8 for key "${key}" with value "${properties[key]}"`);
34+
}
35+
}
36+
}
37+
return properties;
38+
}
39+
1740
const convertToJSON = async (file, distPath) => {
1841
const properties = PropertiesReader(file)._properties;
42+
inlineUTF(properties);
1943
const filename = path.basename(file, path.extname(file));
2044
const language = filename.match(/^messagebundle_(.*?)$/)[1];
2145
if (!allLanguages.includes(language)) {

0 commit comments

Comments
 (0)