bckp/translator-core
Composer 安装命令:
composer require bckp/translator-core
包简介
Simple and fast PHP translator
关键字:
README 文档
README
Simple and fast PHP translator
Usage
For each language, we create Catalogue, that will compile PHP cache file with translations.
$catalogue = new CatalogueBuilder(new PluralProvider(), './path/to/cache', 'cs'); $catalogue->addFile('./path/to/locales/errors.cs.neon'); $catalogue->addFile('./path/to/locales/messages.cs.neon'); // Enable debug mode, disabled by default $catalogue->setDebugMode(true); $compiledCatalogue = $catalogue->compile(); $translator = new Translator($compiledCatalogue); $translator->translate('errors.error.notFound'); // Will output "Soubor nenalezen" $translator->translate('messages.plural', 4); // Will output "4 lidé" $translator->translate('messages.withArgs', 'Honza', 'poledne'); // Will output "Ahoj, já jsem Honza, přeji krásné poledne" $translator->translate('messages.withArgsRev', 'Honza', 'poledne'); // Will output "Krásné poledne, já jsem Honza"
You can add your own source of text by callback
$catalogue = new CatalogueBuilder(new PluralProvider(), './path/to/cache', 'cs'); $catalogue->addDynamic('errors', function(array &$messages, string $resource, string $locale){ $messages['common'] = 'Common error translation'; $messages['critical'] = $this->database->fetchAll('translations')->where('resource = ? and locale = ?', $resource, $locale); }); $catalogue->addFile('./path/to/locales/messages.cs.neon'); // if you add new file errors.cs.neon, it will be overwritten by dynamic, as they is processed later // Enable debug mode, disabled by default $catalogue->setDebugMode(true); // You can even add events for onCheck // $timestamp contains timestamp of last file generation // but remember, this will called only on debug mode! $catalogue->addCheckCallback(function(int $timestamp){ if ($timestamp < $this->database->fetchSingle('select last_update from settings where caption = ?', 'translations')){ throw new BuilderException('rebuild required'); } }); // And events for onCompile // this will occur when app have prepared all translations into single // big array, and you want to modify it $catalogue->addCompileCallback(function(array &$messages, string $locale){ // in messages, we have all the translations $messages['errors.common'] = 'Modify common error translation'; }); $compiledCatalogue = $catalogue->compile(); $translator = new Translator($compiledCatalogue); $translator->translate('errors.common'); // Will output "Modify common error translation"
Debug mode will made translator slower, it will check every time you call compile() if some of language files did change or not, and if they do, automaticly recompile cache, this is Good for development, but BAD on production mode.
Or you can use TranslatorProvider
$catalogue = new CatalogueBuilder(new PluralProvider(), './path/to/cache', 'cs'); $catalogue->addFile('./path/to/locales/errors.cs.neon'); $catalogue->addFile('./path/to/locales/messages.cs.neon'); $provider = new TranslatorProvider(['cs','en']); $provider->addCatalogue('cs', $catalogue); $translator = $provider->getTranslator('cs');
Great about this approach is, if you use more then one language, and switch between them, translator provider will compile catalogue only on first use, then use the compiled one.
Translation file format
Translation files are written in NEON format. Plural strings are in ARRAY, otherwise STRING.
welcome: 'Vítejte' withArgs: 'Ahoj, já jsem %s, přeji krásné %s' withArgsRev: 'Krásné %2$s, já jsem %1$s' plural: zero: 'žádný člověk' one: 'jeden člověk' few: '%d lidé' other: '%d lidí' next: 'This is next translation'
If you want more structure, you can use DOT as character, so lets say we want 3 error messages, we will create structure like this
error.notFound: 'Soubor nenalezen' error.notMatch: 'Soubor neodpovídá zadání' error.typeMismatch: 'Neplatný typ souboru'
Diagnostics
You can use simple diagnostics (or implement one), if you want to know, what is wrong with your translations, this is primary for framework integration (like Nette one).
bckp/translator-core 适用场景与选型建议
bckp/translator-core 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.06k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「translator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bckp/translator-core 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bckp/translator-core 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bckp/translator-core 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Olympus Hermes is a bundle used to translate all messages from your WordPress packages and Olympus bundles.
This is a simple light weight language parser to help with language key translation built for speed using in-memory caching optimization technique.
Simple and fast translator and tools to internationalize your PHP application.
Taiwan Address to English version
Free Baidu Translate API PHP Package
Translator class using Nette Database layer
统计信息
- 总下载量: 4.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-05-15