承接 bckp/translator-core 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bckp/translator-core

Composer 安装命令:

composer require bckp/translator-core

包简介

Simple and fast PHP translator

关键字:

README 文档

README

Downloads this Month Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version License

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 bckp/translator-core 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 4
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-05-15