File size: 2,962 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
/**
 * Determine the correct GlotPress i18n function to use based on the input:
 * __(), _n(), _nx(), _x()
 * @param {Object} properties - properties describing translation request
 * @returns {string}            returns the function name
 */
function getGlotPressFunction( properties ) {
	let wpFunc = [ '_' ];

	if ( properties.plural ) {
		wpFunc.push( 'n' );
	}
	if ( properties.context ) {
		wpFunc.push( 'x' );
	}

	wpFunc = wpFunc.join( '' );

	if ( 1 === wpFunc.length ) {
		return '__';
	}

	return wpFunc;
}

/**
 * Generate each line of equivalent php from a matching `translate()`
 * request found in the client code
 * @param  {Object} properties - properties describing translation request
 * @param  {string} textdomain - optional string to be added as a textdomain value
 * @returns {string}            the equivalent php code for each translation request
 */
function buildPHPString( properties, textdomain ) {
	const wpFunc = getGlotPressFunction( properties );
	const response = [];
	const closing = textdomain ? ', "' + textdomain.replace( /"/g, '\\"' ) + '" ),' : ' ),';
	const stringFromFunc = {
		__: '__( ' + properties.single + closing,
		_x: '_x( ' + [ properties.single, properties.context ].join( ', ' ) + closing,
		_nx:
			'_nx( ' +
			[ properties.single, properties.plural, properties.count, properties.context ].join( ', ' ) +
			closing,
		_n: '_n( ' + [ properties.single, properties.plural, properties.count ].join( ', ' ) + closing,
	};

	// translations with comments get a preceding comment in the php code
	if ( properties.comment ) {
		// replace */ with *\/ to prevent translators from accidentally running arbitrary code
		response.push( '/* translators: ' + properties.comment.replace( /\*\//g, '*\\/' ) + ' */' );
	}

	let string = stringFromFunc[ wpFunc ];

	if ( properties.line ) {
		string += ' // ' + properties.line;
	}

	response.push( string );

	return response.join( '\n' );
}

/**
 * Takes read file and generates a string representation of a file with
 * equivalent WordPress-style translate functions. Also prepends with some
 * necessary time and number translations.
 * @param  {Array}  matches              Matches
 * @param  {Object} options              Options
 * @param  {string} options.phpArrayName Name of the array in the php resulting php file
 * @param  {string} options.projectName  Project name
 * @param  {string} options.textDomain   Text domain
 * @returns {string}                      string representation of the final php file
 */
module.exports = function formatInPHP( matches, options ) {
	const arrayName = options.phpArrayName || options.projectName + '_i18n_strings';
	return [
		// start of the php file
		'<?php',
		'/* THIS IS A GENERATED FILE. DO NOT EDIT DIRECTLY. */',
		'$' + arrayName + ' = array(',
		matches.map( ( element ) => buildPHPString( element, options.textdomain ) ).join( '\n' ),
		');',
		'/* THIS IS THE END OF THE GENERATED FILE */',
	].join( '\n' );
};