gettext / translator
Gettext翻译器函数
v1.2.0
2023-11-06 15:42 UTC
Requires
- php: ^7.2|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- gettext/gettext: ^5.0.0
- oscarotero/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.0
Suggests
- gettext/gettext: Is necessary to load and generate array files used by the translator
README
由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。