gettext/

translator

Gettext翻译器函数

v1.2.0 2023-11-06 15:42 UTC

This package is auto-updated.

Last update: 2024-09-06 17:19:10 UTC


README

Latest Version on Packagist Software License Total Downloads

由Oscar Otero创建 http://oscarotero.com oom@oscarotero.com (MIT许可证)

与gettext/gettext配合使用的翻译器函数。如果您没有php的本地gettext扩展或者想避免与它相关的问题,这将非常有用。

安装

composer require gettext/translator

翻译器

use Gettext\Translator;

//Create a new instance of the translator
$t = new Translator();

//Load the translations from php files (generated by Gettext\Extractors\PhpArray)
$t->loadTranslations(
    'locales/gl/domain1.php',
    'locales/gl/domain2.php',
    'locales/gl/domain3.php',
);

//Now you can use it in your templates
echo $t->gettext('apple');

GettextTranslator

Gettext\GettextTranslator使用gettext扩展。它很有用,因为它结合了使用真实gettext函数的性能,但与Translator类的API相同,所以您可以在不更改应用程序代码的情况下切换到任何一个翻译器。

use Gettext\GettextTranslator;

//Create a new instance
$t = new GettextTranslator();

//It detects the environment variables to set the locale, but you can change it:
$t->setLanguage('gl');

//Load the domains:
$t->loadDomain('messages', 'project/Locale');
//this means you have the file "project/Locale/gl/LC_MESSAGES/messages.mo"

//Now you can use it in your templates
echo $t->gettext('apple');

翻译器函数

为了便于在您的php模板中使用翻译,您可以使用提供的函数

use Gettext\TranslatorFunctions;

//Register the translator to use the global functions
TranslatorFunctions::register($t);

echo __('apple'); // it's the same than $t->gettext('apple');

您可以使用PhpCode提取器扫描包含这些函数的php文件并提取值

<!-- index.php -->
<html>
    <body>
        <?= __('Hello world'); ?>
    </body>
</html>

有关最近更改的更多信息,请参阅CHANGELOG,有关贡献详情请参阅CONTRIBUTING

MIT许可证(MIT)。有关更多信息,请参阅LICENSE