wdes / php-i18n-l10n
PHP库/API,提供国际化与本地化功能
v4.0.0
2021-03-31 13:51 UTC
Requires
- php: ^7.2.9 || ^8.0
- phpmyadmin/twig-i18n-extension: ^4.0
- twig/twig: ^3
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^7 || ^8 || ^9
- wdes/coding-standard: ^3
README
PHP库/API,提供国际化与本地化功能
许可证
关于
我们使用phpmyadmin/twig-i18n-extension
作为Twig扩展。
使用方法
composer require wdes/php-i18n-l10n
请参阅示例文件example/simple.php
没有MO文件的示例
<?php declare(strict_types = 1); // Can be removed :) require_once __DIR__ . '/../vendor/autoload.php'; use Wdes\phpI18nL10n\plugins\MoReader; use Wdes\phpI18nL10n\Launcher; use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n; use Twig\Environment as TwigEnvironment; use Twig\Loader\ArrayLoader as TwigLoader; $moReader = new MoReader(); $moReader->setTranslations( [ 'Homepage' => 'Page d\'accueil', ] ); // Load the translation plugin Launcher::setPlugin($moReader); $twig = new TwigEnvironment(new TwigLoader()); $twig->addExtension(new ExtensionI18n()); // You can use a file instead, see the example using a mo file $templateContents = <<<HTML <html> <title>{% trans %}Homepage{% endtrans %}</title> <body> {% trans %}Homepage{% endtrans %} </body> </html> HTML; echo $twig->createTemplate($templateContents)->render([]);
有MO文件的示例
<?php declare(strict_types = 1); // Can be removed :) require_once __DIR__ . '/../vendor/autoload.php'; use Wdes\phpI18nL10n\plugins\MoReader; use Wdes\phpI18nL10n\Launcher; use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n; use Twig\Environment as TwigEnvironment; use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem; $dataDir = __DIR__ . '/locale/'; $moReader = new MoReader(); $moReader->readFile($dataDir . 'fr.mo'); // Load the file you want (a specific language for example) // Load the translation plugin Launcher::setPlugin($moReader); $loader = new TwigLoaderFilesystem([ __DIR__ . '/templates/' ]); // Load all templates from the dir $twig = new TwigEnvironment($loader); $twig->addExtension(new ExtensionI18n()); echo $twig->render( 'homepage.twig', // Can be found in the templates directory [ 'keyForTwig' => 'theValue', // Just an example line ;) 'say' => 'Hello world' ] );
脚本
此包包含一些有用的脚本scripts/tools
以下是如何使用它们的示例:scripts/update-example.sh