File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,8 +14,32 @@ const assets = require('../../assets-meta.js');
1414
1515const 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+
1740const 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 ( / ^ m e s s a g e b u n d l e _ ( .* ?) $ / ) [ 1 ] ;
2145 if ( ! allLanguages . includes ( language ) ) {
You can’t perform that action at this time.
0 commit comments