This repository was archived by the owner on Jul 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathpo2android.js
More file actions
executable file
·141 lines (132 loc) · 5.03 KB
/
Copy pathpo2android.js
File metadata and controls
executable file
·141 lines (132 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
const gettextParser = require( 'gettext-parser' ),
fs = require( 'fs' ),
crypto = require( 'crypto' );
const indent = ' ';
/**
* Encode a raw string into an Android-compatible value
*
* See: https://tekeye.uk/android/examples/android-string-resources-gotchas
* @param {string} unsafeXMLValue input string to be escaped
* @return {string} Escaped string to be copied into the XML <string></string> node
*/
function escapeResourceXML( unsafeXMLValue ) {
// See: https://tekeye.uk/android/examples/android-string-resources-gotchas
// Let's replace XML special characters <, >, &, ", ', \, \t and \n
// Note that this does not support android:textstyle attributes (<b></b>...)
return unsafeXMLValue.replace( /(\r?\n|\r|\t|<|>|&|'|"|\\)/gm, function( character ) {
switch ( character ) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return '\\\'';
case '"': return '\\\"';
case '\r':
case '\n':
case '\r\n': return '\\n';
case '\t': return '\\t';
case '\\': return '\\\\';
}
} );
}
/**
* Android specifics replacements.
*
* @param {string} XMLValue input to apply replacements.
* @return {string} valid string passing Android linter rules.
*/
function androidReplacements( XMLValue ) {
return XMLValue.replace( /(-|\.\.\.)/gm, function( character ) {
switch ( character ) {
case '-': return '–'; // Android lint rule: TypographyDashes.
case '...': return '…'; // Android lint rule: TypographyEllipsis
}
} );
}
/**
* Generate a unique string identifier to use as the `name` property in our xml.
* Try using the string first by stripping any non-alphanumeric characters and cropping it
* Then try hashing the string and appending it to the the sanatized string
* If none of the above makes a unique ref for our string throw an error
*
* @param {string} str raw string
* @param {string} prefix Optional prefix to add to the name
* @return {string} A unique name for this string
*/
const getUniqueName = ( function() {
const names = {};
const ANDROID_MAX_NAME_LENGTH = 100;
const HASH_LENGTH = 8;
return ( str, prefix = 'gutenberg_native_' ) => {
const maxNameLength = ANDROID_MAX_NAME_LENGTH - prefix.length - HASH_LENGTH - 10; // leave some margin just in case
let name = str.replace( /\W+/g, '_' ).toLocaleLowerCase().substring( 0, maxNameLength );
// trim underscores left and right
name = name.replace( /^_+|_+$/g, '' );
// if name exists, use name + hash of the full string
if ( name in names ) {
const strHashShort = crypto.createHash( 'sha1' ).update( str ).digest( 'hex' ).substring( 0, HASH_LENGTH );
name = `${ name }_${ strHashShort }`;
}
// if name still exists
if ( name in names ) {
throw new Error( `Could not generate a unique name for string "${ str }"` );
}
names[ name ] = true;
return `${ prefix }${ name }`;
};
}() );
function po2Android( poInput ) {
const po = gettextParser.po.parse( poInput );
const translations = po.translations[ '' ];
const androidResourcesMap = Object.values( translations ).reduce( ( result, translation ) => {
if ( ! translation.msgid ) {
return result;
}
const uniqueName = getUniqueName( translation.msgid );
const escapedValue = androidReplacements( escapeResourceXML( translation.msgid ) );
const escapedValuePlural = androidReplacements( escapeResourceXML( translation.msgid_plural || '' ) );
const comment = translation.comments.extracted || '';
let localizedEntry = '';
if ( comment ) {
localizedEntry += `${ indent }<!-- ${ comment.replace( '--', '—' ) } -->\n`;
}
if ( translation.msgid_plural ) {
localizedEntry += `${ indent }<string-array name="${ uniqueName }" tools:ignore="UnusedResources">
${ indent }${ indent }<item>${ escapedValue }</item>
${ indent }${ indent }<item>${ escapedValuePlural }</item>
${ indent }</string-array>
`;
} else {
localizedEntry += `${ indent }<string name="${ uniqueName }" tools:ignore="UnusedResources">${ escapedValue }</string>\n`;
}
result[ uniqueName ] = localizedEntry;
return result;
}, {} );
// try to minimize changes in diffs by sorting strings
const androidResourcesSortedList = Object.entries( androidResourcesMap )
.sort( ( left, right ) => left[ 0 ].localeCompare( right[ 0 ] ) )
.map( ( entry ) => entry[ 1 ] );
return `<?xml version="1.0" encoding="utf-8"?>\n<resources xmlns:tools="http://schemas.android.com/tools">\n${ androidResourcesSortedList.join( '' ) }</resources>\n`;
}
if ( require.main === module ) {
if ( process.stdin.isTTY ) {
const potFileName = process.argv[ 2 ];
const destination = process.argv[ 3 ];
const potFileContent = fs.readFileSync( potFileName );
const xmlOutput = po2Android( potFileContent, process.argv[ 3 ] );
fs.writeFileSync( destination, xmlOutput );
} else {
let inputData = '';
process.stdin.on( 'readable', function() {
const chunk = this.read();
if ( chunk !== null ) {
inputData += chunk;
}
} );
process.stdin.on( 'end', function() {
process.stdout.write( po2Android( inputData ) );
} );
}
return;
}
module.exports = po2Android;