imponeer/smarty-translate
Composer 安装命令:
composer require imponeer/smarty-translate
包简介
Library that provides trans modifier and block smarty extensions based on Symfony Translations contracts
README 文档
README
Smarty Translate
Seamlessly integrate Symfony Translation with Smarty templates
This library adds a new Smarty block function and variable modifier called trans that integrates with any Symfony Translation Contracts compatible library. It allows you to easily translate your Smarty templates using the powerful Symfony translation system.
Installation
The recommended way to install this package is through Composer:
composer require imponeer/smarty-translate
Alternatively, you can manually include the files from the src/ directory in your project.
Setup
Basic Setup
To register the translation extension with Smarty, add the extension class to your Smarty instance:
// Create a Symfony Translator instance $translator = new \Symfony\Component\Translation\Translator('en'); // ... configure your translator ... // Create a Smarty instance $smarty = new \Smarty(); // Register the translation extension $smarty->addExtension( new \Imponeer\Smarty\Extensions\Translate\TranslationSmartyExtension($translator) );
Using with Symfony Container
To integrate with Symfony, you can leverage autowiring, which is the recommended approach for modern Symfony applications:
# config/services.yaml services: # Enable autowiring and autoconfiguration _defaults: autowire: true autoconfigure: true # Register your application's services App\: resource: '../src/*' exclude: '../src/{DependencyInjection,Entity,Tests,Kernel.php}' # Configure Smarty with the extension # The TranslationSmartyExtension will be autowired automatically \Smarty\Smarty: calls: - [addExtension, ['@Imponeer\Smarty\Extensions\Translate\TranslationSmartyExtension']]
Then in your application code, you can simply retrieve the pre-configured Smarty instance:
// Get the Smarty instance with the translation extension already added $smarty = $container->get(\Smarty\Smarty::class);
For more information about Symfony's Dependency Injection Container, see the official documentation.
Using with PHP-DI
With PHP-DI container, you can take advantage of autowiring for a very simple configuration:
use function DI\create; use function DI\get; return [ // Register the translator (assuming you have a translator factory elsewhere) // \Symfony\Contracts\Translation\TranslatorInterface::class => factory(...), // The Translation Extension is autowired by default when using class names // Configure Smarty with the extension \Smarty\Smarty::class => create() ->method('addExtension', get(\Imponeer\Smarty\Extensions\Translate\TranslationSmartyExtension::class)) ];
Then in your application code, you can retrieve the Smarty instance:
// Get the configured Smarty instance $smarty = $container->get(\Smarty\Smarty::class);
For more information about PHP-DI Container, see the official documentation.
Using with League Container
If you're using League Container, you can register the extension like this:
// Create the container $container = new \League\Container\Container(); // Register the translator $container->add(\Symfony\Contracts\Translation\TranslatorInterface::class, function() { $translator = new \Symfony\Component\Translation\Translator('en'); // Configure translator... return $translator; }); // Register Smarty with the translation extension $container->add(\Smarty\Smarty::class, function() use ($container) { $smarty = new \Smarty\Smarty(); // Configure Smarty... // Create and add the translation extension directly $extension = new \Imponeer\Smarty\Extensions\Translate\TranslationSmartyExtension( $container->get(\Symfony\Contracts\Translation\TranslatorInterface::class) ); $smarty->addExtension($extension); return $smarty; });
Then in your application code, you can retrieve the Smarty instance:
// Get the configured Smarty instance $smarty = $container->get(\Smarty\Smarty::class);
For more information about League Container, see the official documentation.
Usage
Once the extension is registered, you can use it in your Smarty templates in two ways:
1. Using the Block Function
The block function allows you to translate blocks of text:
{* Basic usage *} <{trans}>Hello, world!<{/trans}> {* With a specific domain *} <{trans domain='admin'}>_ADMIN_WELCOME<{/trans}> {* With parameters *} <{trans parameters=['name' => 'John']}>Hello, {name}!<{/trans}> {* With domain and locale *} <{trans domain='messages' locale='fr' parameters=['name' => 'John']}>Hello, {name}!<{/trans}>
2. Using the Variable Modifier
The variable modifier allows for inline translations:
{* Basic usage *} <{"Hello, world!"|trans}> {* With parameters *} <{"Hello, {name}!"|trans:["name" => "John"]}> {* With domain *} <{"_ADMIN_WELCOME"|trans:[]:'admin'}> {* With domain and locale *} <{"Hello, {name}!"|trans:["name" => "John"]:'messages':'fr'}>
Supported Attributes
Both the block function and variable modifier support the following attributes:
| Attribute | Description | Default Value |
|---|---|---|
| parameters | Key/value pairs to replace placeholders in translated strings | [] |
| domain | Translation domain (usually corresponds to translation file) | system default |
| locale | Specific locale to use for translation | current system locale |
For the variable modifier, the syntax is: trans:PARAMETERS:DOMAIN:LOCALE
Testing
This package includes a comprehensive test suite. To run the tests:
composer test
Documentation
API documentation is automatically generated and available in the project's wiki. For more detailed information about the classes and methods, please refer to the project wiki.
Contributing
Contributions are welcome! Here's how you can contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature-name - Submit a pull request
Please make sure your code follows the PSR-12 coding standard and include tests for any new features or bug fixes.
If you find a bug or have a feature request, please create an issue in the issue tracker.
imponeer/smarty-translate 适用场景与选型建议
imponeer/smarty-translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.88k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「smarty」 「translate」 「smarty-plugins」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 imponeer/smarty-translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imponeer/smarty-translate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 imponeer/smarty-translate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Store your language lines in the database, yaml or other sources
Rewritten Smarty foreach variant that was invented for use in xoops, but nowadays is used in some other PHP based CMS'es
The bundle for easy using json-rpc api on your project
An Astrotomic Translatable extension for Laravel Nova.
腾讯翻译君翻译、百度翻译 、谷歌翻译(Google 翻译)
统计信息
- 总下载量: 20.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-31