|
2 anni fa | |
---|---|---|
.. | ||
src | 2 anni fa | |
CHANGELOG.md | 2 anni fa | |
LICENSE | 2 anni fa | |
README.md | 2 anni fa | |
composer.json | 2 anni fa |
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
// You can use null to not load a file and the use a setter to set the translations
$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);
// 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
// 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.