-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpot-to-php.js
More file actions
106 lines (87 loc) · 3.19 KB
/
Copy pathpot-to-php.js
File metadata and controls
106 lines (87 loc) · 3.19 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
#!/usr/bin/env node
/* eslint-disable import/no-extraneous-dependencies */
// This file is from wp-i18n tools
/**
* External dependencies
*/
const gettextParser = require('gettext-parser');
const { isEmpty } = require('lodash');
const fs = require('fs');
const TAB = ' ';
const NEWLINE = '\n';
const args = process.argv.slice(2);
const fileHeader = [
'<?php',
'/* THIS IS A GENERATED FILE. DO NOT EDIT DIRECTLY. */',
`\$${args[2].replace(/[|&;$%@"<>()+-_,]/g, "_")}_i18n_strings = array(`,
].join(NEWLINE) + NEWLINE;
const fileFooter = NEWLINE
+ [');', '/* THIS IS THE END OF THE GENERATED FILE */'].join(NEWLINE)
+ NEWLINE;
/**
* Escapes single quotes.
*
* @param {string} input The string to be escaped.
* @return {string} The escaped string.
*/
function escapeSingleQuotes(input) {
return input.replace(/'/g, "\\'");
}
/**
* Converts a translation parsed from the POT file to lines of WP PHP.
*
* @param {Object} translation The translation to convert.
* @param {string} textdomain The text domain to use in the WordPress translation function call.
* @param {string} context The context for the translation.
* @return {string} Lines of PHP that match the translation.
*/
function convertTranslationToPHP(translation, textdomain, context = '') {
let php = '';
// The format of gettext-js matches the terminology in gettext itself.
let original = translation.msgid;
if (original !== '') {
original = escapeSingleQuotes(original);
if (isEmpty(translation.msgid_plural)) {
if (isEmpty(context)) {
php += `${TAB }'${ original }' => __('${ original }', '${ textdomain }')`;
} else {
php
+= `${TAB
}'${ original }' => _x('${ original }', '${ translation.msgctxt }', '${ textdomain }')`;
}
} else {
const plural = escapeSingleQuotes(translation.msgid_plural);
if (isEmpty(context)) {
php
+= `${TAB
}'${ original }' => _n_noop('${ original }', '${ plural }', '${ textdomain }')`;
} else {
php
+= `${TAB
}'${ original }' => _nx_noop('${ original }', '${ plural }', '${ translation.msgctxt }', '${ textdomain }')`;
}
}
}
return php;
}
function convertPOTToPHP(potFile, phpFile, options) {
const poContents = fs.readFileSync(potFile);
const parsedPO = gettextParser.po.parse(poContents);
let output = [];
for (const context of Object.keys(parsedPO.translations)) {
const translations = parsedPO.translations[context];
const newOutput = Object.values(translations)
.map((translation) => convertTranslationToPHP(
translation,
options.textdomain,
context,
))
.filter((php) => php !== '');
output = [...output, ...newOutput];
}
const fileOutput = fileHeader + output.join(`,${ NEWLINE }${NEWLINE}`) + fileFooter;
fs.writeFileSync(phpFile, fileOutput);
}
convertPOTToPHP(args[0], args[1], {
textdomain: args[2],
});