Skip to content

Commit 54a3f15

Browse files
authored
fix(i18n): handle utf-8 escape sequences in message bundle properties files (#12784)
1 parent 255df25 commit 54a3f15

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

packages/tools/lib/i18n/toJSON.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,31 @@ 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) => {
18-
const properties = PropertiesReader(file)._properties;
41+
const properties = inlineUTF(PropertiesReader(file)._properties);
1942
const filename = path.basename(file, path.extname(file));
2043
const language = filename.match(/^messagebundle_(.*?)$/)[1];
2144
if (!allLanguages.includes(language)) {

0 commit comments

Comments
 (0)