philasearch/i18n
Composer 安装命令:
composer require philasearch/i18n
包简介
An i18n library for PHP
README 文档
README
PHP I18n
PHP I18n is a simple library for storing localized strings outside of your code.
Installing
Add the following require to your composer.json file.
{
"require": {
"philasearch/i18n": "1.1.*"
}
}
Setup
Language Folder
In order to use i18n, you will need a language folder. By convention this folder is config/locales, however, you can choose whichever you want. In this folder, each supported language requires its own folder (config/locales/en, config/locales/de, ...).
Use in PHP
You will most likely be using the I18n class in your own implementation, so you are just given a class which you can write a wrapper class for in your own code. All you need to do to create this class is give it the path to the language folder you decided on before.
<?php use Philasearch\I18n\I18n as Lang; $lang = new Lang( dirname(__FILE__) . '/config/locales' );
Example Usage
Notice: Currently, only yaml files are supported.
For this example, the language folder is at config/locales.
Create a file config/locales/en/example.yml
foo: bar nested: foo: bar var: foo :var plural: one: A :color apple other: :count :color apples
And a dialect file config/locales/en_US/example.yml
foo: murica
Note: Take note of the yml file name as it will be the first part of your key (example.yml => example).
Now we can load the translation class and get our translated string.
<?php use Philasearch\I18n\I18n as Lang; $lang = new Lang( dirname(__FILE__) . 'config/locales' ); /** * normal translated string * * @param string $locale * @param string $key */ $lang->get( 'en', 'example.foo' ); // returns 'bar' /** * nested translated strings * * @param string $locale * @param string $key */ $lang->get( 'en', 'example.nested.foo' ); // returns 'bar' /** * Variable translated string * * @param string $locale * @param string $key */ $lang->get( 'en', 'example.var', ['var' => 'bar']); // returns 'foo bar' /** * Plural tranlated strings * * @param string $locale * @param string $key * @param int $count */ $lang->get( 'en', 'example.plural', ['color' => 'red'], 1); // returns 'A red apple' $lang->get( 'en', 'example.plural', ['color' => 'red'], 2); // returns '2 red apples' /** * Dialect translated strings */ $lang->get( 'en_US', 'example.foo' ); // returns 'murica' $lang->get( 'en_US', 'example.nested.foo' ); // returns 'bar' as it falls back to en
philasearch/i18n 适用场景与选型建议
philasearch/i18n 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.53k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 philasearch/i18n 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philasearch/i18n 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2014-09-09