承接 jaimeperez/twig-configurable-i18n 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jaimeperez/twig-configurable-i18n

最新稳定版本:v2.1

Composer 安装命令:

composer create-project jaimeperez/twig-configurable-i18n

包简介

This is an extension on top of Twig's i18n extension, allowing you to customize which functions to use for translations.

README 文档

README

This is a hack to get Twig's native internationalization extension to work with other translation systems than PHP's native gettext extension. That way, you can use other implementation, such as gettext/gettext or even your own one, provided a function is available to perform translations for singular strings, and another one for plural strings.

Installation

If you are using composer, installation is as easy as requiring this package:

composer require jaimeperez/twig-configurable-i18n

If you are not using composer, you will need to manually include all (four) source files in this project from your script, and make sure you have the twig/extension package installed as well.

Usage

Using this extension is extremely simple, and won't require you to modify your code or existing templates, but just a couple of modifications to the PHP scripts where you want internationalization.

First, create aliases for two classes so that the ones defined in this extension override the default ones:

use JaimePerez\TwigConfigurableI18n\Twig\Environment as Twig_Environment;
use JaimePerez\TwigConfigurableI18n\Twig\Extensions\Extension\I18n as Twig_Extensions_Extension_I18n;

The first line allows you to redefine the twig environment class, so that you can keep the configuration options you pass to the constructor. The second line installs this extension instead of the native one. Your code will use now this extension without further modifications, and PHP's native gettext will continue to be used.

Now, you are ready to configure the extension to use different translation functions. You will do that by passing options to the Twig environment class you have just imported:

$twig = new Twig_Environment($loader, array(
    'translation_function' => 'translate',
    'translation_function_plural' => 'translate_plural'
));

These are the only two supported options. translation_function lets you specify a function that is capable to translate strings in singular, being equivalent to gettext(). On the other hand, translate_function_plural lets you specify a function to handle the translation of strings in plural. This one is equivalent to ngettext().

You are all set. Twig will use the translate() and translate_plural() functions you have just specified, instead of the native gettext() and ngettext() functions. Of course, you will need to specify some functions that actually exist, but that depends on what implementation of gettext you want to use. If, for example, you plan to use Oscar Otero's pure-PHP implementation gettext/gettext, you can do the following:

use \Gettext\Translator as Translator;

...

$this->translator = new Translator();
$this->translator->register();

...

$twig = new Twig_Environment($loader, array(
    'translation_function' => '__',
    'translation_function_plural' => 'n__'
));

jaimeperez/twig-configurable-i18n 适用场景与选型建议

jaimeperez/twig-configurable-i18n 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.55M 次下载、GitHub Stars 达 6, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「i18n」 「twig」 「translation」 「extension」 「gettext」 「internationalization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jaimeperez/twig-configurable-i18n 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jaimeperez/twig-configurable-i18n 我们能提供哪些服务?
定制开发 / 二次开发

基于 jaimeperez/twig-configurable-i18n 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.55M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 25
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-2.1
  • 更新时间: 未知