someSon/phalcon-i18n

多语言支持(扩展版)

v1.3.3 2022-11-13 15:25 UTC

This package is auto-updated.

Last update: 2024-09-29 06:03:21 UTC


README

MIT License CircleCI codecov Packagist Version (including pre-releases) Made in Ukraine Russian Warship Go Fuck Yourself

扩展 Phalcon 框架 v5 翻译模块

安装

$ composer require someson/phalcon-i18n

示例

例如 login.json 文件

{
    "form": {
        "label": {
            "identity": "Benutzername",
            "password": "Passwort",
            "rememberMe": "Ich möchte angemeldet bleiben"
        },
        "placeholder": {
            "identity": "Bitte geben Sie ihren Benutzernamen ein",
            "password": "Bitte geben Sie ihr Passwort ein"
        },
        "button": "Anmelden"
    },
    "title": {
        "h1": "Main Title",
        "h2": "some subtitle"
    }
}

翻译路径如 login:form.label.identity 返回 Benutzername

login: 开头表示文件名,其余部分是纯 JSON 路径。

用法

1. 简单用法

// component using a singleton pattern, so we can instantiate it before the framework itself
// or wrap it into some global function
$t = \Phalcon\I18n\Translator::instance();

// using the "de" directory, "en" by default
$t->setLang('de');

// equal to "global:a", hence "global" is a default scope
echo $t->_('a');

// placeholder "key" replaced through "value"
echo $t->_('b', ['key' => 'value']);

// nested key
echo $t->_('c.d.e', ['key' => 'value']);

// nested key from the "api" scope (filename === scope, if files used)
echo $t->_('api:c.d.e', ['key' => 'value']);

2. 高级用法

在任何引导文件中(例如 index.php)定义

use \Phalcon\I18n\Translator;

if (! function_exists('__')) {
    function __(string $key, array $params = [], bool $pluralize = true): string {
        return $key ? Translator::instance()->_($key, $params, $pluralize) : '[TRANSLATION ERROR]';
    }
}

在您的代码内部

$translation = __('a.b.c');

或在任何视图中

<h1><?= __('a.b.c') ?></h1>
<h1>{{ __('a.b.c') }}</h1>

配置

默认配置 \Phalcon\I18n\Config\Default.php

return [
    'defaultLang' => 'en',
    'defaultScope' => 'global',

    // loads data from chosen source (e.g. Json) by chosen loader (e.g. Files)
    // can be e.g. "Mysql" by "Database" (feel free to implement)
    'loader' => [
        'className' => \Phalcon\I18n\Loader\Files::class,
        'arguments' => ['path' => $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'locale'],
    ],

    // reads the source and translates it into chosen type of handler (@see key "handler")
    'adapter' => [
        'className' => \Phalcon\I18n\Adapter\Json::class,
    ],

    // implements \Phalcon\Translate\AdapterInterface
    // returns an object of all translations of the specific language
    // provides functionality for placeholder replacing
    'handler' => [
        'options' => [
            'flatten' => ['shift' => 1],
        ],
    ],

    // replaces user-defined (or '%' by default) placeholders
    'interpolator' => [
        'className' => \Phalcon\I18n\Interpolator\AssocArray::class,
        'arguments' => ['{{', '}}'],
    ],

    // bool only
    'collectMissingTranslations' => true,

    // - false
    // - sprintf pattern e.g. [# %s #]
    // - \Phalcon\I18n\Interfaces\DecoratorInterface object
    'decorateMissingTranslations' => new \Phalcon\I18n\Decorator\HtmlCode,
];

您可能希望用自己的配置覆盖它(默认用于具有 i18n 范围的 config 容器)

return [
    // ...

    'i18n' => [
        'loader' => [
            'arguments' => ['path' => '/my/own/path/to/locale/'],
        ],
        'interpolator' => [
            'arguments' => ['[[', ']]'],
        ],
        'collectMissingTranslations' => false,
        'decorateMissingTranslations' => '[# %s #]',
    ],

    // ...
];

运行测试

Codeception 使用

$ docker-compose exec i18n ./vendor/bin/codecept build

要运行测试,请运行以下命令

$ docker-compose exec i18n ./vendor/bin/codecept run [-vv]

要运行代码覆盖率信息,运行

$ docker-compose exec i18n ./vendor/bin/codecept run --coverage --coverage-html

然后在浏览器中打开 tests/_output/coverage/index.html

静态分析器

$ docker-compose exec i18n ./vendor/bin/phpstan analyse src --level max