Skip to content

Commit

Permalink
Merge pull request #430 from wp-cli/fix/418-make-json-domain
Browse files Browse the repository at this point in the history
`make-json`: Add new `--domain` argument
  • Loading branch information
swissspidy authored Feb 21, 2025
2 parents e15e904 + c8a94d0 commit c3f6f01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ if the source directory is detected as either a plugin or theme.
Extract JavaScript strings from PO files and add them to individual JSON files.

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

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

[--domain=<domain>]
Text domain to use for the JSON file name. Overrides the default one extracted from the PO file.

[--purge]
Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal.

Expand Down
34 changes: 34 additions & 0 deletions features/makejson.feature
Original file line number Diff line number Diff line change
Expand Up @@ -951,3 +951,37 @@ Feature: Split PO files into JSON files.
And the return code should be 0
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist

Scenario: Allows overriding the text domain to use for the file name
Given an empty foo-theme directory
And a foo-theme/de_DE.po file:
"""
# Copyright (C) 2018 Foo Theme
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-theme\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: foo-theme.js:15
msgid "Foo Theme"
msgstr "Foo Theme"
"""

When I run `wp i18n make-json foo-theme --domain=my-custom-domain`
Then STDOUT should contain:
"""
Success: Created 1 file.
"""
And the return code should be 0
And the foo-theme/my-custom-domain-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist

13 changes: 9 additions & 4 deletions src/MakeJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class MakeJsonCommand extends WP_CLI_Command {
* [<destination>]
* : Path to the destination directory for the resulting JSON files. Defaults to the source directory.
*
* [--domain=<domain>]
* : Text domain to use for the JSON file name. Overrides the default one extracted from the PO file.
*
* [--purge]
* : Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal.
*
Expand Down Expand Up @@ -78,6 +81,7 @@ public function __invoke( $args, $assoc_args ) {
$purge = Utils\get_flag_value( $assoc_args, 'purge', true );
$update_mo_files = Utils\get_flag_value( $assoc_args, 'update-mo-files', true );
$map_paths = Utils\get_flag_value( $assoc_args, 'use-map', false );
$domain = Utils\get_flag_value( $assoc_args, 'domain', '' );

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

if ( $purge ) {
Expand Down Expand Up @@ -222,10 +226,11 @@ static function ( $value ) {
*
* @param string $source_file Path to the source file.
* @param string $destination Path to the destination directory.
* @param array|null $map Source to build file mapping.
* @param array|null $map Source to build file mapping.
* @param string $domain Override text domain to use.
* @return array List of created JSON files.
*/
protected function make_json( $source_file, $destination, $map ) {
protected function make_json( $source_file, $destination, $map, $domain ) {
/** @var Translations[] $mapping */
$mapping = [];
$translations = new Translations();
Expand All @@ -235,7 +240,7 @@ protected function make_json( $source_file, $destination, $map ) {

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

$domain = $translations->getDomain();
$domain = ( ! empty( $domain ) ) ? $domain : $translations->getDomain();

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

0 comments on commit c3f6f01

Please sign in to comment.