dever b1cb82ec76 edit | 6 vuotta sitten | |
---|---|---|
.. | ||
.github | 6 vuotta sitten | |
src | 6 vuotta sitten | |
CHANGES.md | 6 vuotta sitten | |
CONTRIBUTING.md | 6 vuotta sitten | |
LICENSE | 6 vuotta sitten | |
PERFORMANCE.md | 6 vuotta sitten | |
README.md | 6 vuotta sitten | |
codecov.yml | 6 vuotta sitten | |
composer.json | 6 vuotta sitten | |
phpunit.xml | 6 vuotta sitten |
Translation API for PHP using Gettext MO files.
eval()
for plural equationPlease use Composer to install:
composer require phpmyadmin/motranslator
The API documentation is available at https://develdocs.phpmyadmin.net/motranslator/.
// 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)
// Directly load the mo file
$translator = new PhpMyAdmin\MoTranslator\Translator('./path/to/file.mo');
// Now you can use Translator API (see below)
// 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);
// 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);
This library is based on php-gettext. It adds some performance improvements and ability to install using Composer.
Motivation for this library includes:
eval()
function for plural equations what can have severe security implications, see CVE-2016-6175We've tried that, but it's not a viable solution:
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.