ali-translator/text-template

v1.0.5 2024-06-06 09:58 UTC

This package is auto-updated.

Last update: 2024-09-06 10:30:51 UTC


README

安装

此安装需要 php ^7.4

$ composer require ali-translator/text-template:^1

使用

use \ALI\TextTemplate\MessageFormat\MessageFormatsEnum;
use \ALI\TextTemplate\TextTemplateFactory;

$textTemplateFactory = new TextTemplateFactory(new TemplateMessageResolverFactory('en'));

# Simple variable
$textTemplate = $textTemplateFactory->create('Tom has {appleNumbers} apples', [
    'appleNumbers' => 5,
]);
echo $textTemplate->resolve();
// Result: "Tom has 5 apples"

# For better results, you can add plural form selection
$textTemplate = $textTemplateFactory->create('Tom has {plural(appleNumbers, "=0[no one apple] =1[one apple] other[many apples]")}', [
    'appleNumbers' => 1,
]);
echo $textTemplate->resolve();
// Result: "Tom has one apple"

# It is also possible to create multi-nested templates
$textTemplate = $textTemplateFactory->create('Tom has {appleNumbers}', [
    'appleNumbers' => [
        'content' => '{plural(appleNumbers,"=0[no one apple] =1[one apple] other[many apples]")}',
        'parameters' => [
            'appleNumbers' => 1,
        ],
        // Custom values, if required (mostly for add-on libraries)
        'options' => ['some_notes' => 123]
    ],
]);
echo $textTemplate->resolve();
// Result: "Tom has one apple"

# The same effect will occur when using php objects
$insideTextTemplate = $textTemplateFactory->create(
    '{plural(appleNumbers,"=0[no one apple] =1[one apple] other[many apples]")}', 
    ['appleNumbers' => 1],
    MessageFormatsEnum::TEXT_TEMPLATE,
    ['some_notes' => 123]
);
$textTemplate = $textTemplateFactory->create('Tom has {appleNumbers}', [
    'appleNumbers' => $insideTextTemplate,
]);
// Result: "Tom has one apple"

函数语法

在我们的系统中,函数提供了一种动态的方式来操纵和格式化文本。它们使用一种与基于Unix系统的管道功能相似的语法,允许多个函数的链式或顺序应用。

基本语法

{functionName(some_variable_name, 'some static text')|anotherFunctionWithoutArguments}

关于语法的更多细节

处理函数的处理器

处理器是函数语法的核心功能。它们提供了以各种方式操纵文本和数据的能力。

开箱即用的处理器

  • PrintHandler : 打印 "静态"/"普通变量" 的值。可以用作另一个处理器函数的输入。 {print('Hello World')}

  • HideHandler : 此处理器旨在确认变量,而不在文本中显示它们。这在需要确保所有注册的变量都在文本中使用的情况下很有用,即使它们在视觉上不出现。
    {hide(variable1, variable2, ...)}

  • PluralHandler : 根据给定的参数和区域设置处理复数。
    {plural(appleNumbers,"=0[no one apple] =1[one apple] other[many apples]")}

  • FirstCharacterInLowercaseHandler : 将输入字符串的第一个字符转换为小写。
    {print('HELLO')|makeFirstCharacterInLowercase()}

  • FirstCharacterInUppercaseHandler : 将给定文本的第一个字符转换为大写。 {print('hello')|makeFirstCharacterInUppercase()}

  • ChoosePrepositionBySonorityHandler (Russian) : 确定给定单词在俄语中的正确介词。
    Поездка {ru_choosePreposition('во/в', 'Львов')} Львов

  • AddLocativeSuffixHandler (Turkish) : 为给定单词在土耳其语中添加正确的定位后缀。
    {tr_addLocativeSuffix('İstanbul')} -> İstanbul'da

  • AddDirectionalSuffixHandler (Turkish) : 根据元音和谐向给定单词添加适当的方位后缀 ("'a", "'e", "'ya", "'ye")。
    {tr_addDirectionalSuffix('İstanbul')} -> İstanbul'a
    : 无撇号
    {tr_addDirectionalSuffix('Ev', '')} -> Eve

  • ChooseQuestionSuffixHandler (Turkish) : 根据元音和谐为给定单词选择适当的问题后缀 ("mı", "mi", "mu", "mü")。仅适用于土耳其语。
    Şehriniz {city_name} {tr_chooseQuestionSuffix(city_name)}? 对于 "İstanbul" 的结果将是 Şehriniz İstanbul mu?

  • ChoosePrepositionBySonorityHandler (Ukrainian) : 确定给定单词在乌克兰语中的正确介词。
    Поїздка {uk_choosePreposition('Поїздка', 'в/у', 'Львів')} Львів

测试

php composer install
./vendor/bin/phpunit