From 5e6c3b17cebd8c9b12cbecff0620f7d18dec9508 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 17 Dec 2013 17:56:30 +0100 Subject: [PATCH] Script to convert PHP i18n to JSON Change-Id: I769d5a106bf6214127cee2056cd64c18ca4182c6 --- maintenance/generateJsonI18n.php | 130 +++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 maintenance/generateJsonI18n.php diff --git a/maintenance/generateJsonI18n.php b/maintenance/generateJsonI18n.php new file mode 100644 index 0000000000..f3f89437ae --- /dev/null +++ b/maintenance/generateJsonI18n.php @@ -0,0 +1,130 @@ +mDescription = "Build JSON messages files from a PHP messages file"; + $this->addArg( 'phpfile', 'PHP file defining a $messages array', true ); + $this->addArg( 'jsondir', 'Directory to write JSON files to', true ); + $this->addOption( 'langcode', 'Language code; only needed for converting core i18n files', + false, true ); + } + + public function execute() { + $phpfile = $this->getArg( 0 ); + $jsondir = $this->getArg( 1 ); + + if ( !is_readable( $phpfile ) ) { + $this->error( "Error reading $phpfile\n", 1 ); + } + include $phpfile; + $phpfileContents = file_get_contents( $phpfile ); + + if ( !isset( $messages ) ) { + $this->error( "PHP file $phpfile does not define \$messages array\n", 1 ); + } + + $extensionStyle = true; + if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) { + if ( !$this->hasOption( 'langcode' ) ) { + $this->error( "PHP file $phpfile does not set language codes, --langcode " . + "is required.\n", 1 ); + } + $extensionStyle = false; + $langcode = $this->getOption( 'langcode' ); + $messages = array( $langcode => $messages ); + } else if ( $this->hasOption( 'langcode' ) ) { + $this->output( "Warning: --langcode option set but will not be used.\n" ); + } + + foreach ( $messages as $langcode => $langmsgs ) { + $authors = $this->getAuthorsFromComment( $this->findCommentBefore( + $extensionStyle ? "\$messages['$langcode'] =" : '$messages =', + $phpfileContents + ) ); + // Make sure the @metadata key is the first key in the output + $langmsgs = array_merge( + array( '@metadata' => array( 'authors' => $authors ) ), + $langmsgs + ); + + $jsonfile = "$jsondir/$langcode.json"; + $success = file_put_contents( + $jsonfile, + FormatJson::encode( $langmsgs, true, FormatJson::ALL_OK ) + ); + if ( $success === false ) { + $this->error( "FAILED to write $jsonfile", 1 ); + } + $this->output( "$jsonfile\n" ); + } + $this->output( "All done.\n" ); + } + + /** + * Find the documentation comment immediately before a given search string + * @param string $needle String to search for + * @param string $haystack String to search in + * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty + */ + protected function findCommentBefore( $needle, $haystack ) { + $needlePos = strpos( $haystack, $needle ); + if ( $needlePos === false ) { + return ''; + } + // Need to pass a negative offset to strrpos() so it'll search backwards from the + // offset + $startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) ); + if ( $startPos === false ) { + return ''; + } + + return substr( $haystack, $startPos, $needlePos - $startPos ); + } + + /** + * Get an array of author names from a documentation comment containing @author declarations. + * @param string $comment Documentation comment + * @return Array of author names (strings) + */ + protected function getAuthorsFromComment( $comment ) { + $matches = null; + preg_match_all( '/@author (.*?)$/m', $comment, $matches ); + return $matches && $matches[1] ? $matches[1] : array(); + } +} + +$maintClass = "GenerateJsonI18n"; +require_once RUN_MAINTENANCE_IF_MAIN; -- 2.20.1