Skip to content

Commit c3f6f01

Browse files
authored
Merge pull request #430 from wp-cli/fix/418-make-json-domain
`make-json`: Add new `--domain` argument
2 parents e15e904 + c8a94d0 commit c3f6f01

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ if the source directory is detected as either a plugin or theme.
150150
Extract JavaScript strings from PO files and add them to individual JSON files.
151151

152152
~~~
153-
wp i18n make-json <source> [<destination>] [--purge] [--update-mo-files] [--pretty-print] [--use-map=<paths_or_maps>]
153+
wp i18n make-json <source> [<destination>] [--domain=<domain>] [--purge] [--update-mo-files] [--pretty-print] [--use-map=<paths_or_maps>]
154154
~~~
155155

156156
For JavaScript internationalization purposes, WordPress requires translations to be split up into
@@ -167,6 +167,9 @@ about WordPress JavaScript internationalization.
167167
[<destination>]
168168
Path to the destination directory for the resulting JSON files. Defaults to the source directory.
169169

170+
[--domain=<domain>]
171+
Text domain to use for the JSON file name. Overrides the default one extracted from the PO file.
172+
170173
[--purge]
171174
Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal.
172175

features/makejson.feature

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,37 @@ Feature: Split PO files into JSON files.
951951
And the return code should be 0
952952
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
953953

954+
Scenario: Allows overriding the text domain to use for the file name
955+
Given an empty foo-theme directory
956+
And a foo-theme/de_DE.po file:
957+
"""
958+
# Copyright (C) 2018 Foo Theme
959+
# This file is distributed under the same license as the Foo Plugin package.
960+
msgid ""
961+
msgstr ""
962+
"Project-Id-Version: Foo Plugin\n"
963+
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
964+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
965+
"Language-Team: LANGUAGE <LL@li.org>\n"
966+
"Language: de_DE\n"
967+
"MIME-Version: 1.0\n"
968+
"Content-Type: text/plain; charset=UTF-8\n"
969+
"Content-Transfer-Encoding: 8bit\n"
970+
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
971+
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
972+
"X-Domain: foo-theme\n"
973+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
974+
975+
#: foo-theme.js:15
976+
msgid "Foo Theme"
977+
msgstr "Foo Theme"
978+
"""
979+
980+
When I run `wp i18n make-json foo-theme --domain=my-custom-domain`
981+
Then STDOUT should contain:
982+
"""
983+
Success: Created 1 file.
984+
"""
985+
And the return code should be 0
986+
And the foo-theme/my-custom-domain-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
987+

src/MakeJsonCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class MakeJsonCommand extends WP_CLI_Command {
3838
* [<destination>]
3939
* : Path to the destination directory for the resulting JSON files. Defaults to the source directory.
4040
*
41+
* [--domain=<domain>]
42+
* : Text domain to use for the JSON file name. Overrides the default one extracted from the PO file.
43+
*
4144
* [--purge]
4245
* : Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal.
4346
*
@@ -78,6 +81,7 @@ public function __invoke( $args, $assoc_args ) {
7881
$purge = Utils\get_flag_value( $assoc_args, 'purge', true );
7982
$update_mo_files = Utils\get_flag_value( $assoc_args, 'update-mo-files', true );
8083
$map_paths = Utils\get_flag_value( $assoc_args, 'use-map', false );
84+
$domain = Utils\get_flag_value( $assoc_args, 'domain', '' );
8185

8286
if ( Utils\get_flag_value( $assoc_args, 'pretty-print', false ) ) {
8387
$this->json_options |= JSON_PRETTY_PRINT;
@@ -115,7 +119,7 @@ public function __invoke( $args, $assoc_args ) {
115119
/** @var DirectoryIterator $file */
116120
foreach ( $files as $file ) {
117121
if ( $file->isFile() && $file->isReadable() && 'po' === $file->getExtension() ) {
118-
$result = $this->make_json( $file->getRealPath(), $destination, $map );
122+
$result = $this->make_json( $file->getRealPath(), $destination, $map, $domain );
119123
$result_count += count( $result );
120124

121125
if ( $purge ) {
@@ -222,10 +226,11 @@ static function ( $value ) {
222226
*
223227
* @param string $source_file Path to the source file.
224228
* @param string $destination Path to the destination directory.
225-
* @param array|null $map Source to build file mapping.
229+
* @param array|null $map Source to build file mapping.
230+
* @param string $domain Override text domain to use.
226231
* @return array List of created JSON files.
227232
*/
228-
protected function make_json( $source_file, $destination, $map ) {
233+
protected function make_json( $source_file, $destination, $map, $domain ) {
229234
/** @var Translations[] $mapping */
230235
$mapping = [];
231236
$translations = new Translations();
@@ -235,7 +240,7 @@ protected function make_json( $source_file, $destination, $map ) {
235240

236241
$base_file_name = basename( $source_file, '.po' );
237242

238-
$domain = $translations->getDomain();
243+
$domain = ( ! empty( $domain ) ) ? $domain : $translations->getDomain();
239244

240245
if ( $domain && 0 !== strpos( $base_file_name, $domain ) ) {
241246
$base_file_name = "{$domain}-{$base_file_name}";

0 commit comments

Comments
 (0)