From c8a94d01c7cfe4c8974879853705d6f08ff99aa4 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 20 Feb 2025 14:31:54 +0100 Subject: [PATCH] `make-json`: Add new `--domain` argument --- README.md | 5 ++++- features/makejson.feature | 34 ++++++++++++++++++++++++++++++++++ src/MakeJsonCommand.php | 13 +++++++++---- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 10adbe2..dd36922 100644 --- a/README.md +++ b/README.md @@ -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 [] [--purge] [--update-mo-files] [--pretty-print] [--use-map=] +wp i18n make-json [] [--domain=] [--purge] [--update-mo-files] [--pretty-print] [--use-map=] ~~~ For JavaScript internationalization purposes, WordPress requires translations to be split up into @@ -167,6 +167,9 @@ about WordPress JavaScript internationalization. [] Path to the destination directory for the resulting JSON files. Defaults to the source directory. + [--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. diff --git a/features/makejson.feature b/features/makejson.feature index 54e97fb..45e719f 100644 --- a/features/makejson.feature +++ b/features/makejson.feature @@ -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 \n" + "Language-Team: LANGUAGE \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 + diff --git a/src/MakeJsonCommand.php b/src/MakeJsonCommand.php index f54673d..7b662a0 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -38,6 +38,9 @@ class MakeJsonCommand extends WP_CLI_Command { * [] * : Path to the destination directory for the resulting JSON files. Defaults to the source directory. * + * [--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. * @@ -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; @@ -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 ) { @@ -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(); @@ -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}";