jrmajor/laravel-fluent

Laravel的Fluent翻译

v1.1.0 2024-03-13 17:12 UTC

README

Latest Stable Version Required PHP Version

使用Project Fluent(由Mozilla设计的一种本地化系统)释放Laravel应用程序中自然语言的表述能力,以Project Fluent的方式。

阅读Fluent语法指南或尝试在Fluent游乐场中试用以了解更多关于语法的知识。

shared-photos =
    { $userName } { $photoCount ->
        [one] added a new photo
       *[other] added { $photoCount } new photos
    } to { $userGender ->
        [male] his stream
        [female] her stream
       *[other] their stream
    }.
__('stream.shared-photos', [
    'userName' => 'jrmajor',
    'photoCount' => 2,
    'userGender' => 'male',
]); // jrmajor added 2 new photos to his stream.

此包是jrmajor/fluent-php的Laravel包装器。

您可以通过Composer安装它:composer require jrmajor/laravel-fluent。包将自动注册自己。

用法

此包使用Major\Fluent\Laravel\FluentTranslator替换了默认的Laravel翻译器。

app('translator') instanceof Major\Fluent\Laravel\FluentTranslator; // true

Fluent翻译存储在.ftl文件中。将它们放置在您的Laravel应用程序中的.php翻译文件之间。

/resources
  /lang
    /en
      menu.ftl
      validation.php
    /pl
      menu.ftl
      validation.php

如果没有给定键的Fluent消息,翻译器将回退到.php文件,这允许您逐步引入Fluent翻译格式。

Laravel验证器使用用于替换:attribute变量的自定义逻辑,并需要深层嵌套的键,这些键在Fluent中不受支持,因此您应保持validation.php文件在默认的Laravel格式。

trans_choice()辅助函数始终回退到默认翻译器,因为Fluent格式消除了对此函数的需求。

您可以使用FluentTranslator::addFunction()方法注册Fluent函数

配置

可选地,您可以使用以下命令发布配置文件

php artisan vendor:publish --tag fluent-config

这将在config/fluent.php中发布以下文件

return [

    /*
     * In strict mode, exceptions will be thrown for syntax errors
     * in .ftl files, unknown variables in messages etc.
     * It's recommended to enable this setting in development
     * to make it easy to spot mistakes.
     */
    'strict' => env('APP_ENV', 'production') !== 'production',

    /*
     * Determines if it should use Unicode isolation marks (FSI, PDI)
     * for bidirectional interpolations. You may want to enable this
     * behaviour if your application uses right-to-left script.
     */
    'use_isolating' => false,

];

测试

vendor/bin/phpunit           # Tests
vendor/bin/phpstan analyze   # Static analysis
vendor/bin/php-cs-fixer fix  # Formatting