Skip to content

Bug: wp-cli i18n make-json Generates Incorrect source Path with --use-map in Newer Versions #456

@raychuang

Description

@raychuang

Bug Report

Describe the current, buggy behavior

When using wp i18n make-json with the --use-map flag, newer versions of wp-cli (e.g., 2.12.0) generate an incorrect source path in the output JSON file if the source file in the map is in a different directory than the destination file. This appears to be a regression, as this scenario was handled correctly in older versions (e.g., 2.10.0).

This bug breaks the JavaScript translation loading mechanism in WordPress, as the generated JSON file's source field does not match the path of the enqueued script, leading to a hash mismatch.

To Reproduce
Steps to reproduce the behavior:

  1. Set up the project structure:
.
├── package.json
├── languages/
│   └── my-plugin-zh_CN.po
├── src/
│   ├── admin.js
│   └── components/
│       └── MyComponent.js
└── build/
    └── admin.js
  1. Configure package.json :
  • The build script compiles src/admin.js to build/admin.js .
  • The make-json script uses a map to associate strings from a component ( src/components/MyComponent.js ) with the final compiled asset ( build/admin.js ).
{
  "name": "my-plugin",
  "version": "1.0.0",
  "scripts": {
    "build": "wp-scripts build src/admin.js",
    "make-pot": "wp i18n make-pot . languages/my-plugin.pot --exclude=node_modules,build",
    "make-json": "wp i18n make-json languages --no-purge --use-map='{\"src/components/MyComponent.js\":\"build/admin.js\"}'"
  },
  "devDependencies": {
    "@wordpress/scripts": "^27.9.0"
  }
}
  1. Create the source files:
  • src/admin.js : This is the entry point that imports the component.
import './components/MyComponent.js';
  • src/components/MyComponent.js : This file contains the translatable string.
import { __ } from '@wordpress/i18n';

__('Hello World', 'my-plugin');
  1. Create a .po file ( languages/my-plugin-zh_CN.po ) with a translation, ensuring the reference points to the original source component:
#: src/components/MyComponent.js:3
msgid "Hello World"
msgstr "你好世界"

Expected Behavior

The command should generate a JSON file (e.g., languages/my-plugin-zh_CN-....json ) where the source field inside the JSON is exactly build/admin.js , as specified in the --use-map argument.

{
    "translation-revision-date": "...",
    "generator": "WP-CLI/2.12.0",
    "source": "build/admin.js",
    "domain": "messages",
    "locale_data": { ... }
}

Actual Behavior

The command generates a JSON file with a malformed source path, such as builds/a.js or another incorrect variation. This appears to be caused by a bug in path resolution or concatenation when the source file's directory ( src/components/ ) differs from the destination file's directory ( build/ ).

Environment

  • OS: Linux
  • wp-cli version: 2.12.0 (Problem exists)
  • wp-cli version (for comparison): 2.10.0 (Works correctly)
  • i18n-command version: The version bundled with the respective wp-cli releases. Workaround
    A temporary workaround is to alter the map to point to the source entry-point file instead of the final build file. This works because @wordpress/scripts registers the source path with WordPress for translations.
"make-json": "wp i18n make-json languages --no-purge --use-map='{\"src/components/MyComponent.js\":\"src/admin.js\"}'"

While this workaround functions, it is counter-intuitive, as the purpose of the map is to point to the final, enqueued script path. It also fails if the build tool registers the build path instead.

Additional Context

This is a regression from wp-cli v2.10.0, where the same configuration worked as expected. A comparison of src/MakeJsonCommand.php between the versions reveals significant changes in path handling that likely introduced this bug. The issue causes significant confusion and breaks the standard i18n workflow for JavaScript-heavy plugins and themes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions