locomotivemtl/charcoal-translation

此包已被废弃且不再维护。作者建议使用 locomotivemtl/charcoal-translator 包代替。

Charcoal 翻译与语言

0.1 2017-02-03 03:32 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:55:42 UTC


README

Charcoal 为多语言 Charcoal 项目处理本地化、国际化和翻译提供了一个全面的解决方案。

使用方法

设置默认语言 / 设置当前语言

use \Charcoal\Translation\TranslationConfig;

$translation_config = TranslationConfig::instance();

// Set the list of available languages
$translation_config->set_available_langs([
    'en' => [],
    'fr' => []
]);

// Set the default language to "English" ("en")
$translation_config->set_default_lang('en');

// Set the current language to French
$translation_config->set_lang('fr');

从目录加载(已翻译)的字符串

use \Charcoal\Translation\Catalog;

$catalog = Catalog::instance();
$catalog->add_resource('myproject.csv');
echo $catalog->tr('my string');

// Add a custom string..
$catalog->add_translation('custom string', [
    'en' => 'Custom string',
    'fr' => 'Chaîne aléatoire'
]);
ech $catalog->tr('custom string');

直接使用 TranslationString 对象

// Let's assume the default language has been set to 'en'...

use \Charcoal\Translation\TranslationString;

$str = new TranslationString([
    'fr' => 'foo',
    'en' => 'bar'
]);

// All the following examples output "bar"
echo $str;
echo $str->en();
echo $str->val('en');

// All the following examples output "foo"
echo $str->fr();
echo $str->set_lang('fr')->val();
echo $str->val('fr');
//

与 Mustache 模板一起使用

默认情况下,所有对 Charcoal 模板中 {{# _t }} Mustache 助手的调用都尝试使用默认 Charcoal 目录进行翻译。

例如

  • 假设 "my string" 已经被添加到主目录,如上一个示例所示

依赖项

  • Charcoal\ConfigTranslationConfig 所需的