Strings from Blade-syntaxt don't show up in sage.pot

Im having an issue translating my theme. My strings wont appear in the ./resources/lang/sage.pot file in my theme-folder.

I have these four test-strings in my theme:

{{ __('translate me from Blade 1', 'sage') }}
{!! __('translate me from Blade 2', 'sage') !!}

  echo __('translate me from PHP 1', 'sage');
  echo __('translate me from PHP 2', 'sage');  

When I run 'yarn translate' only the string “translate me from PHP 2’” will appear. Which is the only one in plain ol’ php.

First I thought that my WP-CLI was outdated, but now I am on the latest version.

Do you guys had same experience?

Here is my package.json:

"scripts": {
    "dev": "bud dev",
    "build": "bud build",
    "translate": "yarn translate:pot && yarn translate:update",
    "translate:pot": "wp i18n make-pot . ./resources/lang/sage.pot --include=\"app,resources\"",
    "translate:update": "for filename in ./resources/lang/*.po; do msgmerge -U $filename ./resources/lang/sage.pot; done; rm -f ./resources/lang/*.po~",
    "translate:compile": "yarn translate:mo && yarn translate:js",
    "translate:js": "wp i18n make-json ./resources/lang --pretty-print",
    "translate:mo": "wp i18n make-mo ./resources/lang ./resources/lang"

I DO get a small error when the script runs the translate:update command. But I dont think that is causing the issue.

You need wp i18n release 2.3.0 for the Blade-PHP language feature:

If there are still issues you can also install and use wp i18n from its current master until a new release is made: Add support for PHP-Blade files by strarsis · Pull Request #304 · wp-cli/i18n-command · GitHub

Hi Strasis
Thank you for your reply. I tried (maybe I did it wrong) to update i18n to ver. 2.3.0
Now I get this error when running yarn translate:

I ran this command to update
wp package install
(I wasn’t sure what to do with the zip file from your link)

Yes, this happens with the current release of wp i18n, a fix was already merged, but no release yet:

You can install and use wp i18n from source directly to use that fix.
You would then invoke wp i18n using the path to the cloned directory.

1 Like

Addendum: It turns out that when using --include with wp i18n the theme.json has to be explicitly included, too:

This change will make wp i18n use the theme.json, but also trigger an issue with current wp i18n release (theme.json iteration bug), so you should use the current source.

OK. Thank you. I will try again tomorrow. It is almost midnight here now. Thanks again :slight_smile:

So, I’ve tried spending some time on this today. I think its very difficult for me to make it work.
I’ve triede to find out whick version of i18n I am using. I think I’ve managed to downgrade my version by manually deleting the contets of this folder ~ /Users/bo/.wp-cli/packages/vendor/wp-cli/i18n-command
I have also tried to uninstall wp-cli and I have added “theme.json” to my translate script

    "translate:pot": "wp i18n make-pot . ./resources/lang/sage.pot --include=\"app,resources,./theme.json\"",

So now I just get this error - which is the same I got before to begin with:

And when I try to upgrade i18n (wp package install I’m back to this error:

How do I know the version of my current i18n? Can’t I just delete everything and get a clean install of wp cli and then add “theme.json” to my --include list :slight_smile:

@el_skak: This is how I use it with wp i18n from source (with all the yet to be released bug fixes included):

  1. (cwd into ~/src)
  2. git clone
  3. (cwd into ~/src/i18n-command)
  4. composer install to install the wp i18n dependencies.

Sage theme package.json:

    "translate": "yarn translate:pot && yarn translate:update",
    "translate:pot": "~/src/i18n-command/vendor/bin/wp i18n make-pot . ./resources/lang/sage.pot --include=\"theme.json,app,resources\"",
    "translate:update": "for filename in ./resources/lang/*.po; do msgmerge -U $filename ./resources/lang/sage.pot; done; rm -f ./resources/lang/*.po~",
    "translate:compile": "yarn translate:mo && yarn translate:js",
    "translate:js": "~/src/i18n-command/vendor/bin/wp i18n make-json ./resources/lang --pretty-print",
    "translate:mo": "~/src/i18n-command/vendor/bin/wp i18n make-mo ./resources/lang ./resources/lang"

(Don’t forget to change the sage.pot back to the theme name/slug/domain, if you prefer and did that).
There are other ways of course, but this is the simplest and least intrusive way IMHO.

I hope there will be a new release of wp i18n soon, as it also contains a new merge command that allows getting rid of the shell script of translate:update.

Holy cow. It worked… After some head scratching :slight_smile:
I only kept on trying to solve this because you have been so helpful. Otherwise, I would probably have given up.

First, I got an error saying that wp-cli was missing a file to translate. Then I deleted WP-cli completely
I used this guide:

On top of that, I deleted the ~/.wp-cli-folder.

And then I ran the yarn translate command. And it worked…
I still get an error from the translate:update command. But that is only until you have created your .po and .mo files.

I also added this to setup.php in the sage-theme so that WP knows where to find the language-files.

// Load language files
add_action('after_setup_theme', function () {
    load_theme_textdomain('sage', get_template_directory() . '/resources/lang');

Thanks a lot for your help, @strasis

You are welcome. Most issues should be fixed when the next wp i18n release is out.

Yes, this is because a shell script is used for merging the PO files with the newly generated POT files, it doesn’t check for non-existing PO files, but the error can be just ignored.
As mentioned in the previous post, the next release of wp i18n also has a merge command, so this shell script isn’t necessary anymore and no errors will then appear when there are no PO files yet.

I will bookmark this thread until next time I need to translate my theme. That’s for sure. :slight_smile: Thanks again.

The translation docs need updating (was already mentioned in one of the package.json PRs), I should add some notes about the usage (when the new wp i18n version releases probably).

@el_skak: As you are developing a multilingual WordPress site, you will probably also be interested in managing the core/plugins/themes languages. When you are also using Trellis, this guide may be interesting:

Update: wp i18n released a new version that addresses all these issues and also includes a command that updates PO files from POT files (merge). This is great news as this eliminates all external tools and shell-scripts for handling translation (gettext/json). Now wp itself also needs a new release so the bundled wp i18n is the latest one.

In the meantime, the latest release of wp i18n, regardless of current wp CLI version can be installed using
wp package install wp-cli/i18n-command:dev-main
(thanks @swissspidy (GitHub)) and then used as usual (wp i18n)

Thanks, @strarsis. You have been very helpfull. :pray: I will bookmark this thread :slight_smile: