tacoberu/nette-fluent-translator

Nette适配器,用于PHP实现的Fluent项目。

v1.1.4 2023-03-21 02:24 UTC

This package is auto-updated.

Last update: 2024-09-21 05:30:04 UTC


README

这是一个Nette适配器,用于PHP实现的Fluent项目。Fluent是一个本地化框架,旨在释放自然语言翻译的全部表达力。

Fluent项目保持简单事物的简单性,同时使复杂事物成为可能。描述翻译所使用的语法易于阅读和理解。同时,它还允许在必要时表示自然语言中的复杂概念,如性别、复数、动词变位等。

学习FTL语法

FTL是一种用于描述翻译资源的本地化文件格式。FTL代表Fluent Translation List

FTL设计得易于阅读,同时允许表示自然语言中的复杂概念,如性别、复数、动词变位等。

hello-user = Hello, { $username }!

为了了解语法,请阅读Fluent语法指南。如果您是工具作者,您可能对EBNF语法感兴趣。

安装

推荐使用Composer进行安装

    composer require tacoberu/nette-fluent-translator

使用方法

示例 FLT 本地化文件

-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
greet-by-name = Hello, { $name }!
form-title = Title

neon 配置

extensions:
    translation: Taco\NetteFluentTranslator\Extension(%appDir%/locales)

translation:
	defaultLocale: cs_CZ
	supportedLocales:
		- cs_CZ
		- fr_FR
		- en_GB

在PHP代码中

$translator = $container->getService('translator');

dump($translator->translate('-brand-name'));
// "Foo 3000"

dump($translator->translate('welcome', ['name' => 'Anne']));
// "Welcome, Anne, to Foo 3000!"

dump($translator->translate('greet-by-name', ['name' => 'Anne']));
// "Hello, Anne!"

This means, for example:

```php
function beforeRender()
{
	$this->template->setTranslator($this->context->getByType(Nette\Localization\ITranslator::class));
}

并且

$form = new UI\Form($this, $name);
$form->setTranslator($this->context->getByType(Localization\ITranslator::class));
$form->addText('title', 'form-title');

在Latte模板中

{_"-brand-name"}
{_"welcome", ["name" => "Anne"]}
{_"greet-by-name", ["name" => "Anne"]}