drago-ex/translator
最新稳定版本:v1.0.12
Composer 安装命令:
composer require drago-ex/translator
包简介
Drago Extension for Nette Framework providing localization support with a custom Translator service and DI integration.
README 文档
README
Simple and lightweight translator for Nette Framework, providing localization support using NEON translation files.
Requirements
- PHP >= 8.3
- Nette Framework
- Composer
Installation
composer require drago-ex/translator Extension registration
Register the Drago\Localization\DI\TranslatorExtension in your Nette project by adding the following configuration to your neon file:
extensions: - Drago\Localization\DI\TranslatorExtension(translateDir: %appDir%/locale)
Use in the presenter
To use the translator in your presenter, add the TranslatorAdapter trait:
use Drago\Localization\TranslatorAdapter
Accessing the Current Language
You can access the currently set language using the following property:
$this->lang;
Get Translator Instance
To get the translator instance, use the getTranslator method:
$this->getTranslator();
Translation File Format
Translation files should be written in the NEON format. For example:
"Hello, world!": "Hello, world!"
Using Translations in Templates
You can translate strings directly in your Latte templates using the following syntax:
{_"Hello, world!"} {* Using a filter for translation *} {$var|translate}
Translating Forms
To use translations in forms, simply set the translator for the form:
$form->setTranslator($this->getTranslator());
Route Configuration for Language Switching
Set up your routes to support language prefixes. For example, you can define routes with language codes:
$router->addRoute('[<lang=en cs|en>/]<presenter>/<action>', 'Presenter:action');
Switching Languages in Templates
To switch between languages in your templates, you can use n:href to pass the selected language:
<a n:href="this, 'lang' => 'cs'">Czech</a> <a n:href="this, 'lang' => 'en'">English</a>
统计信息
- 总下载量: 2.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04