phpmyadmin/motranslator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

phpmyadmin/motranslator

Composer 安装命令:

composer require phpmyadmin/motranslator

包简介

Translation API for PHP using Gettext MO files

README 文档

README

Translation API for PHP using Gettext MO files.

Test-suite codecov.io Scrutinizer Code Quality Packagist

Features

  • All strings are stored in memory for fast lookup
  • Fast loading of MO files
  • Low level API for reading MO files
  • Emulation of Gettext API
  • No use of eval() for plural equation

Limitations

  • Default InMemoryCache not suitable for huge MO files which you don't want to store in memory
  • Input and output encoding has to match (preferably UTF-8)

Installation

Please use Composer to install:

composer require phpmyadmin/motranslator

Documentation

The API documentation is available at https://develdocs.phpmyadmin.net/motranslator/.

Object API usage

// Create loader object
$loader = new PhpMyAdmin\MoTranslator\Loader();

// Set locale
$loader->setlocale('cs');

// Set default text domain
$loader->textdomain('domain');

// Set path where to look for a domain
$loader->bindtextdomain('domain', __DIR__ . '/data/locale/');

// Get translator
$translator = $loader->getTranslator();

// Now you can use Translator API (see below)

Low level API usage

// Directly load the mo file
// You can use null to not load a file and the use a setter to set the translations
$cache = new PhpMyAdmin\MoTranslator\Cache\InMemoryCache(new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo'));
$translator = new PhpMyAdmin\MoTranslator\Translator($cache);

// Now you can use Translator API (see below)

Translator API usage

// Translate string
echo $translator->gettext('String');

// Translate plural string
echo $translator->ngettext('String', 'Plural string', $count);

// Translate string with context
echo $translator->pgettext('Context', 'String');

// Translate plural string with context
echo $translator->npgettext('Context', 'String', 'Plural string', $count);

// Get the translations
echo $translator->getTranslations();

// All getters and setters below are more to be used if you are using a manual loading mode
// Example: $translator = new PhpMyAdmin\MoTranslator\Translator(null);

// Set a translation
echo $translator->setTranslation('Test', 'Translation for "Test" key');

// Set translations
echo $translator->setTranslations([
  'Test' => 'Translation for "Test" key',
  'Test 2' => 'Translation for "Test 2" key',
]);

// Use the translation
echo $translator->gettext('Test 2'); // -> Translation for "Test 2" key

Gettext compatibility usage

// Load compatibility layer
PhpMyAdmin\MoTranslator\Loader::loadFunctions();

// Configure
_setlocale(LC_MESSAGES, 'cs');
_textdomain('phpmyadmin');
_bindtextdomain('phpmyadmin', __DIR__ . '/data/locale/');
_bind_textdomain_codeset('phpmyadmin', 'UTF-8');

// Use functions
echo _gettext('Type');
echo __('Type');

// It also support other Gettext functions
_dnpgettext($domain, $msgctxt, $msgid, $msgidPlural, $number);
_dngettext($domain, $msgid, $msgidPlural, $number);
_npgettext($msgctxt, $msgid, $msgidPlural, $number);
_ngettext($msgid, $msgidPlural, $number);
_dpgettext($domain, $msgctxt, $msgid);
_dgettext($domain, $msgid);
_pgettext($msgctxt, $msgid);

Using APCu-backed cache

If you have the APCu extension installed you can use it for storing the translation cache. The .mo file will then only be loaded once and all processes will share the same cache, reducing memory usage and resulting in performance comparable to the native gettext extension.

If you are using Loader, pass it an ApcuCacheFactory before getting the translator instance:

PhpMyAdmin\MoTranslator\Loader::setCacheFactory(
    new PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory()
);
$loader = new PhpMyAdmin\MoTranslator\Loader();

// Proceed as before 

If you are using the low level API, instantiate the ApcuCache directly:

$cache = new PhpMyAdmin\MoTranslator\Cache\ApcuCache(
    new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo'),
    'de_DE',     // the locale
    'phpmyadmin' // the domain
);
$translator = new PhpMyAdmin\MoTranslator\Translator($cache);

// Proceed as before

By default, APCu will cache the translations until next server restart and prefix the cache entries with mo_ to avoid clashes with other cache entries. You can control this behaviour by passing $ttl and $prefix arguments, either to the ApcuCacheFactory or when instantiating ApcuCache:

PhpMyAdmin\MoTranslator\Loader::setCacheFactory(
    new PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory(
        3600,     // cache for 1 hour
        true,     // reload on cache miss
        'custom_' // custom prefix for cache entries
    )
);
$loader = new PhpMyAdmin\MoTranslator\Loader();

// or...

$cache = new PhpMyAdmin\MoTranslator\Cache\ApcuCache(
    new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo'),
    'de_DE',
    'phpmyadmin',
    3600,     // cache for 1 hour
    true,     // reload on cache miss
    'custom_' // custom prefix for cache entries
);
$translator = new PhpMyAdmin\MoTranslator\Translator($cache);

If you receive updated translation files you can load them without restarting the server using the low-level API:

$parser = new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo');
$cache = new PhpMyAdmin\MoTranslator\Cache\ApcuCache($parser, 'de_DE', 'phpmyadmin');
$parser->parseIntoCache($cache);

You should ensure APCu has enough memory to store all your translations, along with any other entries you use it for. If an entry is evicted from cache, the .mo file will be re-parsed, impacting performance. See the apc.shm_size and apc.shm_segments documentation and monitor cache usage when first rolling out.

If your .mo files are missing lots of translations, the first time a missing entry is requested the .mo file will be re-parsed. Again, this will impact performance until all the missing entries are hit once. You can turn off this behaviour by setting the $reloadOnMiss argument to false. If you do this it is critical that APCu has enough memory, or users will see untranslated text when entries are evicted.

History

This library is based on php-gettext. It adds some performance improvements and ability to install using Composer.

Motivation

Motivation for this library includes:

  • The php-gettext library is not maintained anymore
  • It doesn't work with recent PHP version (phpMyAdmin has patched version)
  • It relies on eval() function for plural equations what can have severe security implications, see CVE-2016-6175
  • It's not possible to install it using Composer
  • There was place for performance improvements in the library

Why not to use native gettext in PHP?

We've tried that, but it's not a viable solution:

  • You can not use locales not known to system, what is something you can not control from web application. This gets even more tricky with minimalist virtualisation containers.
  • Changing the MO file usually leads to PHP segmentation fault. It (or rather Gettext library) caches headers of MO file and if it's content is changed (for example new version is uploaded to server) it tries to access new data with old references. This is bug known for ages: https://bugs.php.net/bug.php?id=45943

Why use Gettext and not JSON, YAML or whatever?

We want translators to be able to use their favorite tools and we want us to be able to use wide range of tools available with Gettext as well such as web based translation using Weblate. Using custom format usually adds another barrier for translators and we want to make it easy for them to contribute.

phpmyadmin/motranslator 适用场景与选型建议

phpmyadmin/motranslator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.88M 次下载、GitHub Stars 达 59, 最近一次更新时间为 2016 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「i18n」 「gettext」 「translator」 「mo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phpmyadmin/motranslator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phpmyadmin/motranslator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.88M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 62
  • 点击次数: 32
  • 依赖项目数: 9
  • 推荐数: 4

GitHub 信息

  • Stars: 59
  • Watchers: 9
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2016-02-23