timber/wp-i18n-twig
Composer 安装命令:
composer require timber/wp-i18n-twig
包简介
WordPress translations extraction for Twig files with WP-CLI
README 文档
README
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:
- Tags from symfony/twig-brigde:
{% dump %}{% form_theme %}{% stopwatch %}{% trans_default_domain %}{% trans %}
- Tags from twigphp/cache-extra:
{% cache %}
- Tags from symfony/ux
{% component %}{% props %}
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.
- Custom tags other than the supported ones above will result in parsing errors.
- Using non default block/comment/variable tags isn't supported and probably won't ever be.
timber/wp-i18n-twig 适用场景与选型建议
timber/wp-i18n-twig 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.88k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2024 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 timber/wp-i18n-twig 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 timber/wp-i18n-twig 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 62.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2024-01-23