4 * Convert a PHP messages file to a set of JSON messages files.
7 * php generateJsonI18n.php ExtensionName.i18n.php i18n/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup Maintenance
28 require_once __DIR__
. '/Maintenance.php';
31 * Maintenance script to generate JSON i18n files from a PHP i18n file.
33 * @ingroup Maintenance
35 class GenerateJsonI18n
extends Maintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->mDescription
= "Build JSON messages files from a PHP messages file";
40 $this->addArg( 'phpfile', 'PHP file defining a $messages array', false );
41 $this->addArg( 'jsondir', 'Directory to write JSON files to', false );
42 $this->addOption( 'langcode', 'Language code; only needed for converting core i18n files',
44 $this->addOption( 'extension', 'Perform default conversion on an extension',
46 $this->addOption( 'shim-only', 'Only create or update the backward-compatibility shim' );
49 public function execute() {
52 $phpfile = $this->getArg( 0 );
53 $jsondir = $this->getArg( 1 );
54 $extension = $this->getOption( 'extension' );
58 $this->error( "The phpfile is already specified, conflicts with --extension.\n", 1 );
60 $phpfile = "$IP/extensions/$extension/$extension.i18n.php";
64 $this->error( "I'm here for an argument!\n" );
65 $this->maybeHelp( true );
69 $this->transformI18nFile( $phpfile, $jsondir );
72 public function transformI18nFile( $phpfile, $jsondir = null ) {
74 // Assume the json directory should be in the same directory as the
76 $jsondir = dirname( $phpfile ) . "/i18n";
78 if ( !is_dir( $jsondir ) ) {
79 $this->output( "Creating directory $jsondir.\n" );
80 $success = mkdir( $jsondir );
82 $this->error( "Could not create directory $jsondir\n", 1 );
86 if ( $this->hasOption( 'shim-only' ) ) {
87 $this->shimOnly( $phpfile, $jsondir );
92 if ( $jsondir === null ) {
93 $this->error( 'Argument [jsondir] is required unless --shim-only is specified.' );
94 $this->maybeHelp( true );
97 if ( !is_readable( $phpfile ) ) {
98 $this->error( "Error reading $phpfile\n", 1 );
101 $phpfileContents = file_get_contents( $phpfile );
103 if ( !isset( $messages ) ) {
104 $this->error( "PHP file $phpfile does not define \$messages array\n", 1 );
107 $extensionStyle = true;
108 if ( !isset( $messages['en'] ) ||
!is_array( $messages['en'] ) ) {
109 if ( !$this->hasOption( 'langcode' ) ) {
110 $this->error( "PHP file $phpfile does not set language codes, --langcode " .
111 "is required.\n", 1 );
113 $extensionStyle = false;
114 $langcode = $this->getOption( 'langcode' );
115 $messages = array( $langcode => $messages );
116 } elseif ( $this->hasOption( 'langcode' ) ) {
117 $this->output( "Warning: --langcode option set but will not be used.\n" );
120 foreach ( $messages as $langcode => $langmsgs ) {
121 $authors = $this->getAuthorsFromComment( $this->findCommentBefore(
122 $extensionStyle ?
"\$messages['$langcode'] =" : '$messages =',
125 // Make sure the @metadata key is the first key in the output
126 $langmsgs = array_merge(
127 array( '@metadata' => array( 'authors' => $authors ) ),
131 $jsonfile = "$jsondir/$langcode.json";
132 $success = file_put_contents(
134 FormatJson
::encode( $langmsgs, "\t", FormatJson
::ALL_OK
) . "\n"
136 if ( $success === false ) {
137 $this->error( "FAILED to write $jsonfile", 1 );
139 $this->output( "$jsonfile\n" );
142 if ( !$this->hasOption( 'langcode' ) ) {
143 $shim = $this->doShim( $jsondir );
144 file_put_contents( $phpfile, $shim );
147 $this->output( "All done.\n" );
148 $this->output( "Also add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" );
151 protected function shimOnly( $phpfile, $jsondir ) {
152 if ( file_exists( $phpfile ) ) {
153 if ( !is_readable( $phpfile ) ) {
154 $this->error( "Error reading $phpfile\n", 1 );
157 $phpfileContents = file_get_contents( $phpfile );
159 if ( !preg_match( '!"/([^"$]+)/\$csCode.json";!', $phpfileContents, $m ) ) {
160 $this->error( "Cannot recognize $phpfile as a shim.\n", 1 );
163 if ( $jsondir === null ) {
167 $this->output( "Updating existing shim $phpfile\n" );
168 } elseif ( $jsondir === null ) {
169 $this->error( "$phpfile does not exist.\n" .
170 "Argument [jsondir] is required in order to create a new shim.\n", 1 );
172 $this->output( "Creating new shim $phpfile\n" );
175 $shim = $this->doShim( $jsondir );
176 file_put_contents( $phpfile, $shim );
177 $this->output( "All done.\n" );
180 protected function doShim( $jsondir ) {
184 * This is a backwards-compatibility shim, generated by:
185 * https://git.wikimedia.org/blob/mediawiki%2Fcore.git/HEAD/maintenance%2FgenerateJsonI18n.php
187 * Beginning with MediaWiki 1.23, translation strings are stored in json files,
188 * and the EXTENSION.i18n.php file only exists to provide compatibility with
189 * older releases of MediaWiki. For more information about this migration, see:
190 * https://www.mediawiki.org/wiki/Requests_for_comment/Localisation_format
192 * This shim maintains compatibility back to MediaWiki 1.17.
195 if ( !function_exists( '{{FUNC}}' ) ) {
196 function {{FUNC}}( $cache, $code, &$cachedData ) {
197 $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] );
198 foreach ( $codeSequence as $csCode ) {
199 $fileName = dirname( __FILE__ ) . "/{{OUT}}/$csCode.json";
200 if ( is_readable( $fileName ) ) {
201 $data = FormatJson::decode( file_get_contents( $fileName ), true );
202 foreach ( array_keys( $data ) as $key ) {
203 if ( $key === '' || $key[0] === '@' ) {
204 unset( $data[$key] );
207 $cachedData['messages'] = array_merge( $data, $cachedData['messages'] );
210 $cachedData['deps'][] = new FileDependency( $fileName );
215 $GLOBALS['wgHooks']['LocalisationCacheRecache'][] = '{{FUNC}}';
220 $jsondir = str_replace( '\\', '/', $jsondir );
221 $shim = str_replace( '{{OUT}}', $jsondir, $shim );
222 $shim = str_replace( '{{FUNC}}', 'wfJsonI18nShim' . wfRandomString( 16 ), $shim );
228 * Find the documentation comment immediately before a given search string
229 * @param string $needle String to search for
230 * @param string $haystack String to search in
231 * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty
233 protected function findCommentBefore( $needle, $haystack ) {
234 $needlePos = strpos( $haystack, $needle );
235 if ( $needlePos === false ) {
238 // Need to pass a negative offset to strrpos() so it'll search backwards from the
240 $startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) );
241 if ( $startPos === false ) {
245 return substr( $haystack, $startPos, $needlePos - $startPos );
249 * Get an array of author names from a documentation comment containing @author declarations.
250 * @param string $comment Documentation comment
251 * @return array Array of author names (strings)
253 protected function getAuthorsFromComment( $comment ) {
255 preg_match_all( '/@author (.*?)$/m', $comment, $matches );
257 return $matches && $matches[1] ?
$matches[1] : array();
261 $maintClass = "GenerateJsonI18n";
262 require_once RUN_MAINTENANCE_IF_MAIN
;