File size: 4,949 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 * Takes an xgettext-js match and processes it into a form more easily consumed
 * by server-side i18n functions.
 *
 * Returns an object  *
 * Specifically:
 * - facilitates simple string concatenation inside translate,
 * e.g. translate( "A long string " + "broken up over multiple lines" ),
 * - wraps quotes and backslashes for php consumption
 * @param  {Object} match - parser matching object
 * @returns {Object | undefined} data object combining the strings and options passed into translate();
 */
module.exports = function preProcessXGettextJSMatch( match ) {
	const finalProps = { line: match.line };

	if ( ! match.arguments.length ) {
		return;
	}

	const args = match.arguments;
	let options;

	[ 'single', 'plural', 'options' ].slice( 0, args.length ).forEach( function ( field, i ) {
		if ( 'StringLiteral' === args[ i ].type ) {
			finalProps[ field ] = makeDoubleQuoted( args[ i ].extra.raw );
		} else if ( 'BinaryExpression' === args[ i ].type ) {
			finalProps[ field ] = encapsulateString( concatenateBinaryExpression( args[ i ] ) );
		} else if ( 'ObjectExpression' === args[ i ].type && 'undefined' === typeof options ) {
			options = args[ i ];
		} else if ( 'TemplateLiteral' === args[ i ].type ) {
			finalProps[ field ] = makeDoubleQuoted( '`' + args[ i ].quasis[ 0 ].value.raw + '`' );
		}
	} );

	if ( 'undefined' !== typeof options ) {
		// map options to finalProps object
		options.properties.forEach( function ( property ) {
			// key might be an  Identifier (name), or a StringLiteral (value)
			const key = property.key.name || property.key.value;
			if ( 'StringLiteral' === property.value.type ) {
				const keyName = key === 'original' ? 'single' : key;
				finalProps[ keyName ] =
					'comment' === key ? property.value.value : makeDoubleQuoted( property.value.extra.raw );
			} else if ( 'ObjectExpression' === property.value.type && 'original' === key ) {
				// Get pluralization strings. This clause can be removed when all translations
				// are updated to the new approach for plurals.
				property.value.properties.forEach( function ( innerProp ) {
					if ( 'StringLiteral' === innerProp.value.type ) {
						finalProps[ innerProp.key.name || innerProp.key.value ] = makeDoubleQuoted(
							innerProp.value.extra.raw
						);
					}
				} );
			}
		} );
	}

	// We don't care about the actual count value on the server, we just want to
	// register the translation string in GlotPress, and the real count value
	// will be used on the client to determine which plural version to display.
	if ( typeof finalProps.plural !== 'undefined' ) {
		finalProps.count = 1;
	}

	// Brittle test to check for collision of the method name because d3
	// also provides a translate() method. Longer-term solution would be
	// better namespacing.
	if ( ! finalProps.single ) {
		return false;
	}

	return finalProps;
};

/**
 * Long translation strings can be broken into multiple strings concatenated with the + operator.
 * This function concatenates the substrings into a single string.
 * @param  {Object} ASTNode - the BinaryExpression object returned from the AST parser
 * @returns {string}          - the concatenated string
 */
function concatenateBinaryExpression( ASTNode ) {
	if ( ASTNode.operator !== '+' ) {
		return false;
	}

	let result =
		'StringLiteral' === ASTNode.left.type
			? ASTNode.left.value
			: concatenateBinaryExpression( ASTNode.left );
	result +=
		'StringLiteral' === ASTNode.right.type
			? ASTNode.right.value
			: concatenateBinaryExpression( ASTNode.right );

	return result;
}

/**
 * Takes a valid javascript literal (with the quotes included) and returns a double-quoted
 * version of that string
 * @param  {string} literal - origin literal (string with quotes)
 * @returns {string}         - double quote representation of the string
 */
function makeDoubleQuoted( literal ) {
	if ( ! literal || literal.length < 2 ) {
		return undefined;
	}

	// double-quoted string
	if ( literal.charAt( 0 ) === '"' ) {
		return literal.replace( /(\\)/g, '\\$1' );
	}

	// single-quoted string
	if ( literal.charAt( 0 ) === "'" ) {
		return (
			'"' +
			literal
				.substring( 1, literal.length - 1 )
				.replace( /\\'/g, "'" )
				.replace( /(\\|")/g, '\\$1' ) +
			'"'
		);
	}

	// ES6 string
	if ( literal.charAt( 0 ) === '`' ) {
		return (
			'"' +
			literal
				.substring( 1, literal.length - 1 )
				.replace( /`/g, '`' )
				.replace( /(\\|")/g, '\\$1' ) +
			'"'
		);
	}

	return '';
}

/**
 * Takes a string argument and turns it into a valid string representation for most languages/format (with double quotes)
 * Anything else than a string is left unchanged
 * @param  {string} input  - origin string or other type of input
 * @returns {string}        - universal representation of string or input unchanged
 */
function encapsulateString( input ) {
	if ( 'string' !== typeof input ) {
		return input;
	}
	return '"' + input.replace( /(\\|")/g, '\\$1' ) + '"';
}