承接 imponeer/smarty-translate 相关项目开发

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

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

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

License GitHub release PHP Packagist Smarty version requirement

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:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin feature-name
  5. 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 20.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-31