koded/i18n

支持 .po 和 .php 翻译文件的简单国际化库

0.9.2 2023-10-17 13:58 UTC

This package is auto-updated.

Last update: 2024-08-23 00:15:46 UTC


README

CI Latest Stable Version Minimum PHP Version: 8.1

这是一个支持 .po 和 .php 翻译文件的简单国际化库。

composer require koded/i18n

要求

  • intl
  • gettext

用法

__(string $message, array $arguments => [], string $locale = '');

配置

I18n 类是静态的,应该在 PHP 代码/脚本的开头部分进行配置。

use Koded\Stdlib\Config;
use Koded\I18n\{ArrayCatalog, DefaultFormatter, I18n, I18nCatalog};

// all conf directives are optional
$config = new Config([
    'translation.catalog' => ArrayCatalog::class,
    'translation.formatter' => DefaultFormatter::class,
    'translation.dir' => '/path/to/your/translation/files',
    'translation.locale' => ['mk_MK', 'de_DE', 'it_IT'],
]);

I18n::register(I18nCatalog::new($config));

翻译文件

.po 文件

poedit 是一个处理翻译文件的非常好用的应用程序。

.php 文件

翻译文件具有简单的结构

return [
    'language' => '',
    'messages' => [
        // your translation strings goes here
        'original string' => 'translated string',
    ]
];

其中 messages 包含翻译字符串的 key => value 对。

代码质量

Code Coverage Scrutinizer Code Quality

vendor/bin/phpunit

许可证

Software license

代码在 3-Clause BSD 许可证 的条款下分发。