chipslays/phrase

PHP 国际化库。

1.0.2 2021-08-30 20:20 UTC

This package is auto-updated.

Last update: 2024-09-25 16:09:16 UTC


README

GitHub Workflow Status GitHub

PHP 国际化库。

功能

  • 基于 JSONYAML 文件进行翻译;
  • 轻松添加新的 源文件
  • 插值翻译;
  • 复数;

安装

composer require chipslays/phrase

设置

区域文件: locales/yaml/en_US.yml

# interpolated message
hello: Hello {name}!

# pluralization
plural: I have {count} {{eng:{count},melon}} and {money} {{eng:{money},dollar}}.

# message can be a array
array:
  text: This is my flag.
  image: images/en-flag.jpg
  myKey: myValue

区域文件: locales/json/en_US.json

{
    "hello": "Hello {name}!",
    "plural": "I have {count} {{eng:{count},melon}} and {money} {{eng:{money},dollar}}.",
    "array": {
        "text": "This is my flag.",
        "image": "images/en-flag.jpg"
    }
}

用法

创建 Phrase 实例

// Engine constructor
__construct(string $root, string $locale, ?string $fallback);
use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\JsonEngine;

$engine = new JsonEngine(__DIR__ . '/locales/json', 'en_US');
$phrase = new Phrase($engine);
$phrase->get(...);
use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\YamlEngine;

$engine = new YamlEngine(__DIR__ . '/locales/yaml', 'en_US');
$phrase = new Phrase($engine);
$phrase->get(...);
use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\JsonEngine;

$engine = new JsonEngine(__DIR__ . '/locales/json', 'en_US');
Phrase::setEngine($engine);
Phrase::get(...);

获取消息

$phrase->get(string $key, ?array $replace = null, ?string $locale = null);

# key - locale message key
# replace - array with interpolation
# locale - force locale (useful when using multiple locales at the same time)
$phrase->get('hello');
Phrase::get('hello');
__('hello');

传递语言代码以强制使用。

$phrase->get('hello', null, 'ru_RU');
Phrase::get('hello', null, 'ru_RU');
__('hello', null, 'ru_RU');

插值

传递命名参数以插值翻译。

$phrase->get('hello', ['{name}' => 'John Doe']);
Phrase::get('hello', ['{name}' => 'John Doe']);
__('hello', ['{name}' => 'John Doe']);

// Hello John Doe!

复数

英语复数短语

{{eng:{count},melon}}

俄语复数短语

{{rus:{count},арбуз,арбуза,арбузов}}
# english locale file
# for english plural word have 1 form
...
plural: I have {count} {{eng:{count},melon}} and {money} {{eng:{money},dollar}}.
....
# russian locale file
# for russian plural word have 3 forms
...
plural: У меня есть {count} {{rus:{count},арбуз,арбуза,арбузов}} и {money} {{rus:{money},рубль,рубля,рублей}}
....
$phrase->get('plural', ['{count}' => 1, '{money}' => 100])
Phrase::get('plural', ['{count}' => 1, '{money}' => 100])
__('plural', ['{count}' => 1, '{money}' => 100])

// I have 1 melon and 100 dollars.

合并区域消息(loadpatch

如果需要向已加载的文件添加消息,请使用 patch 方法。

如果此区域之前未加载,则将简单地加载。

使用 load 方法删除并覆盖所有之前按区域加载的消息。

use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\YamlEngine;

$engine = new YamlEngine(__DIR__ . '/locales/yaml', 'en_US');
$phrase = new Phrase($engine);

// this method loaded en_US.yml file from MyPlugin dir and merge with previously loaded locale en_US
$phrase->patch(__DIR__ . '/locales/plugins/MyPlugin/', 'en_US');

// this method delete and overwrite all previous messages
$phrase->load(__DIR__ . '/locales/yaml', 'en_US');
use Chipslays\Phrase\Plural;

echo Plural::eng(10, 'melon'); // melons
echo Plural::rus(10, ['арбуз', 'арбуза', 'арбузов']); // арбузов

助手

__(string $key, ?array $replace = null, ?string $locale = null): string|array
__('hello', ['{name}' => 'John Doe'], 'en_US');

自定义区域文件(引擎)

YamlEngine 的示例

use Chipslays\Phrase\Engine\AbstractEngine;
use Chipslays\Phrase\Exceptions\PhraseException;

class YamlEngine extends AbstractEngine
{
    /**
     * @param string $locale
     * @return void
     *
     * @throws PhraseException
     */
    protected function load(string $locale): void
    {
        $path = $this->root . '/' . $locale . '.yml';

        if (!file_exists($path)) {
            throw new PhraseException("Locale file not found in path {$path}", 1);
        }

        $this->locales[$locale] = yaml_parse_file($path);
    }
}

许可

MIT