WordPress translations extraction for Twig files with WP-CLI
Find a file
github-actions[bot] 4d3c41c070
Some checks failed
Release / release (push) Failing after 2s
Code Quality Checks / code-quality (push) Has been cancelled
Testing / test (push) Has been cancelled
chore(main): release 1.1.0
2025-11-28 14:40:26 +01:00
.github/workflows ci: Update googleapis/release-please-action 2025-11-28 14:39:38 +01:00
features chore: Fix duplicate feature name 2025-11-28 14:39:38 +01:00
src chore: Fix PHPStan issues 2025-11-28 14:39:38 +01:00
stubs chore: Fix PHPStan issues 2025-11-28 14:39:38 +01:00
.distignore ci: update release please (#3) 2024-02-07 07:56:19 +01:00
.editorconfig feat: release wp-i18n-twig 0.1.0 2024-01-18 22:08:42 +01:00
.gitignore feat: release wp-i18n-twig 0.1.0 2024-01-18 22:08:42 +01:00
.release-please-manifest.json chore(main): release 1.1.0 2025-11-28 14:40:26 +01:00
behat.yml feat: release wp-i18n-twig 0.1.0 2024-01-18 22:08:42 +01:00
CHANGELOG.md chore(main): release 1.1.0 2025-11-28 14:40:26 +01:00
composer.json chore: Normalize composer.json 2025-11-28 14:39:38 +01:00
CONTRIBUTING.md chore!: Add contributing 2024-04-25 21:47:25 +02:00
FUNDING.yml chore: add funding 2024-02-07 21:34:00 +01:00
i18n-twig-command.php chore: Fix PHPStan issues 2025-11-28 14:39:38 +01:00
LICENSE chore: add licence 2024-02-07 21:41:10 +01:00
phpcs.xml.dist chore: Fix PHPStan issues 2025-11-28 14:39:38 +01:00
phpstan.neon.dist chore: Fix PHPStan issues 2025-11-28 14:39:38 +01:00
README.md docs: fix typo (#6) 2024-04-25 21:51:09 +02:00
release-please-config.json ci: update release please (#3) 2024-02-07 07:56:19 +01:00
wp-cli.yml feat: release wp-i18n-twig 0.1.0 2024-01-18 22:08:42 +01:00

wp-i18n-twig

tests

Parsing WordPress translations in Twig/Timber templates has always been quirky and not very convenient. Especially since WordPress started grabbing translations from multiple sources (theme stylesheet, {block,theme}.json, block editor JS, PHP files) and now also outputing in multiple formats (po, php).

WP-CLI being the recommended way to create/update POT files, timber/wp-i18n-twig provides (almost native) Twig translation extraction by overriding the default wp i18n make-pot.

Install

Because WP-CLI default commands are not meant to be overridden, there are two working ways to install timber/wp-i18n-twig:

Global

If you want to use it with a global installed wp (/usr/local/bin/wp or similar), you have to install it as a WP-CLI package:

wp package install timber/wp-i18n-twig

Local

You can also install the package at the project level using composer and run WP-CLI from the local vendor/bin/wp:

composer require timber/wp-i18n-twig

Usage

Usage is the same than wp i18n make-pot command.

wp i18n make-pot /path/to/my/theme languages/my-theme.pot

--debug flag is handy if you want to check the templates that have been parsed (and watch for parsing errors):

wp i18n make-pot /path/to/my/theme languages/my-theme.pot --debug

Please refer to the wp i18n command documentation for more detailed information.

To match wp i18n make-pot behavior, a --skip-twig flag has been added in case skipping Twig files extraction is needed.

Translations

timber/wp-i18n-twig handles the same translations functions and feature scope wp i18n does.

It does also support translators comments, at an evil cost.

Adding translators comments in your Twig templates works like it would in PHP, place it on the same line or the line before the translation string:

{# translators: Translators 1! #}
{{ __( 'hello world', 'foo-theme' ) }}

{# Translators: Translators 2! #}
{{ __( 'foo', 'foo-theme' ) }}

{# translators: this should get extracted. #} {{ __( 'baba', 'foo-theme' ) }}

{# translators: boo #} {# translators: this should get extracted too. #} {# some other comment #} {{__( 'bubu', 'foo-theme' ) }}

Custom tags

When working with Twig, you may require popular Twig libraries that register custom tags (such as {% dump %}, {% cache %}, etc.). timber/wp-i18n-twig will automatically load the token parsers for the following tags:

Important

For the token parsers to be loaded, you must use the local version of timber/wp-i18n-twig (vendor/bin/wp).

Limitations

To make this package work in any Twig environment, templates are not compiled to PHP but only parsed. This main benefit is that it's (almost) configuration agnostic (custom filters, functions, template location).

However, since Twig is highly configurable, there are still some (minor) limitations.