定制 drago-ex/translator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

drago-ex/translator

Composer 安装命令:

composer require drago-ex/translator

包简介

Lightweight translator for Nette Framework using NEON files, supporting global and module-specific translations.

README 文档

README

Lightweight translator for Nette Framework using NEON files, supporting global and module-specific translations.

License: MIT PHP version Tests Coding Style

Requirements

  • PHP >= 8.3
  • Nette Framework
  • Composer

Installation

composer require drago-ex/translator

Extension Registration

Register the DI extension in your NEON configuration.

extensions:
	translator: Drago\Localization\DI\TranslatorExtension(%appDir%, %tempDir%)

Optional configuration

translator:
	autoFinder: false
	translateDirs:
		- %appDir%/First/Translate
		- %appDir%/Second/Translate
	exclude:
		- %appDir%/Temp
		- %appDir%/Legacy

Translator Behavior

  • All directories listed in translateDirs are loaded in order.
  • Later directories override translations from earlier ones.
  • If autoFinder is enabled, the entire application directory is scanned for NEON files.
  • Directories listed in exclude are skipped during automatic scanning.

Translation files must be named by language code:

cs.neon
en.neon

Translation File Format

"Hello, world!": "Hello, world!"

Using Translator in Presenters

Add the TranslatorAdapter trait to your presenter:

use Drago\Localization\TranslatorAdapter;

The trait provides:

  • persistent language parameter ($lang)
  • automatic translator initialization
  • template integration

Accessing the Current Language

You can access the currently set language using the following property:

$this->lang;

Getting Translator Instance

To get the initialized translator for the current language:

$this->getTranslator()

Using Translations in Templates

The translator is automatically registered in templates. Example usage in Latte:

{_"Hello, world!"}
{$label|translate}

Using Translator in Forms

To enable translations in forms, set the translator explicitly:

$form->setTranslator($this->getTranslator());

Routing for Language Switching

To support language prefixes, configure your routes accordingly:

$router->addRoute('[<lang=en cs|en>/]<presenter>/<action>', 'Presenter:action');

Switching Languages in Templates

You can switch languages by passing the lang parameter:

<a n:href="this, lang => cs">Czech</a>
<a n:href="this, lang => en">English</a>

Language Switch Widget

The package provides a reusable Latte widget for language switching. When project file copying is handled by drago-ex/project-tools, the widget is copied to:

app/Presentation/Accessory/Widget/@lang-switch.latte

Import the widget in your layout:

{import 'path/to/@lang-switch.latte'}

Render language links:

{include lang-switch, lang: 'cs', name: 'Czech'}
<span class="small ps-1 pe-1 text-secondary">|</span>
{include lang-switch, lang: 'en', name: 'English'}

The current language link automatically receives the current class.

Available options:

  • lang - target language code.
  • name - visible translated label.
  • class - optional class added to the link.
  • tag - optional wrapper tag: li, div, or span.
  • tagClass - optional class added to the wrapper tag.

Use class when the link needs a custom class:

{include lang-switch, lang: 'cs', name: 'Czech', class: 'nav-link'}

Use tag when the link must be wrapped, for example in a dropdown menu:

{include lang-switch, lang: 'cs', name: 'Czech', tag: 'li'}
{include lang-switch, lang: 'en', name: 'English', tag: 'li'}

Use tagClass when the wrapper needs styling:

{include lang-switch, lang: 'cs', name: 'Czech', tag: 'li', tagClass: 'item-wrapper'}

Notes

  • Translator loads translations lazily on first use
  • Translations are loaded once per request
  • Missing keys return the original message

drago-ex/translator 适用场景与选型建议

drago-ex/translator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.16k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 drago-ex/translator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-09