tobento / app-translation
应用翻译支持。
1.0.2
2024-09-07 12:19 UTC
Requires
- php: >=8.0
- tobento/app: ^1.0
- tobento/service-translation: ^1.0.3
Requires (Dev)
- phpunit/phpunit: ^9.5
- tobento/app-migration: ^1.0.2
- tobento/service-filesystem: ^1.0.5
- tobento/service-language: ^1.0.4
- vimeo/psalm: ^4.0
README
应用翻译支持。
目录
入门
使用以下命令安装应用翻译项目的最新版本。
composer require tobento/app-translation
要求
- PHP 8.0 或更高版本
文档
应用
如果您使用的是骨架,请查看 App Skeleton。
您还可以查看 App 以了解更多关于应用的一般信息。
翻译启动
翻译启动执行以下操作
- 实现 TranslatorInterface
- 添加
trans
应用宏
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // You might add trans directory: $app->dirs() ->dir($app->dir('app').'trans', 'trans', group: 'trans', priority: 100); // if not set, the same dir is specified by the boot as default! // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); // Run the app $app->run();
默认情况下,翻译器使用 文件资源。
翻译消息
您可以通过多种方式翻译消息
使用应用
use Tobento\App\AppFactory; use Tobento\Service\Translation\TranslatorInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); $app->booting(); $translator = $app->get(TranslatorInterface::class); $translated = $translator->trans( message: 'Hi :name', parameters: [':name' => 'John'], locale: 'de' ); // or using the app macro: $translated = $app->trans( message: 'Hi :name', parameters: [':name' => 'John'], locale: 'de' ); // Run the app $app->run();
使用自动装配
您还可以在任何由应用解析的类中请求 TranslatorInterface::class
。
use Tobento\Service\Translation\TranslatorInterface; class SomeService { public function __construct( protected TranslatorInterface $translator, ) {} }
使用翻译启动
use Tobento\App\Boot; use Tobento\App\Translation\Boot\Translation; class AnyServiceBoot extends Boot { public const BOOT = [ // you may ensure the view boot. Translation::class, ]; public function boot(Translation $translation) { $translated = $translation->trans( message: 'Hi :name', parameters: [':name' => 'John'], locale: 'de' ); } }
查看 翻译服务 了解更多。
使用 trans 函数
use function Tobento\App\Translation\{trans}; $translated = trans( message: 'Hi :name', parameters: [':name' => 'John'], locale: 'de' );
配置翻译器
使用应用语言配置区域设置
首先安装 app-language 包
composer require tobento/app-language
然后,只需启动 Language::class
。就是这样。区域设置将根据 Tobento\Service\Language\LanguagesInterface::class
设置。
// ... $app->boot(\Tobento\App\Language\Boot\Language::class); $app->boot(\Tobento\App\Translation\Boot\Translation::class); // ...
手动配置区域设置
use Tobento\App\AppFactory; use Tobento\Service\Translation\TranslatorInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); $app->booting(); $translator = $app->get(TranslatorInterface::class); // set the default locale: $translator->setLocale('de'); // set the locale fallbacks: $translator->setLocaleFallbacks(['de' => 'en']); // set the locale mapping: $translator->setLocaleMapping(['de' => 'de-CH']); // or using the app on method: $app->on(TranslatorInterface::class, function(TranslatorInterface $translator) { $translator->setLocale('de'); $translator->setLocaleFallbacks(['de' => 'en']); $translator->setLocaleMapping(['de' => 'de-CH']); }); // Run the app $app->run();
配置缺失的翻译
如果您可能想记录缺失的翻译,可以将 MissingTranslationHandlerInterface::class
的实现设置为满足您的需求。
use Tobento\App\AppFactory; use Tobento\Service\Translation\MissingTranslationHandlerInterface; use Tobento\Service\Translation\MissingTranslationHandler; use Psr\Log\LoggerInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); $app->booting(); $app->set(MissingTranslationHandlerInterface::class, function()) { return new MissingTranslationHandler($logger); // LoggerInterface, any PSR-3 logger }); // Run the app $app->run();
迁移翻译
默认情况下,翻译器使用 文件资源。因此,您可能需要安装并使用 App Migration 包
编写迁移
use Tobento\Service\Migration\MigrationInterface; use Tobento\Service\Migration\ActionsInterface; use Tobento\Service\Migration\Actions; use Tobento\Service\Migration\Action\FilesCopy; use Tobento\Service\Migration\Action\FilesDelete; use Tobento\Service\Dir\DirsInterface; class TranslationFiles implements MigrationInterface { protected array $files; /** * Create a new TranslationFiles. * * @param DirsInterface $dirs */ public function __construct( protected DirsInterface $dirs, ) { $transDir = realpath(__DIR__.'/../../').'/resources/trans/'; $this->files = [ $this->dirs->get('trans').'en/' => [ $transDir.'en/en.json', $transDir.'en/shop.json', ], $this->dirs->get('trans').'de/' => [ $transDir.'de/de.json', $transDir.'de/shop.json', ], ]; } /** * Return a description of the migration. * * @return string */ public function description(): string { return 'Translation files.'; } /** * Return the actions to be processed on install. * * @return ActionsInterface */ public function install(): ActionsInterface { return new Actions( new FilesCopy($this->files), ); } /** * Return the actions to be processed on uninstall. * * @return ActionsInterface */ public function uninstall(): ActionsInterface { return new Actions( new FilesDelete($this->files), ); } }
迁移启动
use Tobento\App\Boot; use Tobento\App\Migration\Boot\Migration; class TranslationFilesMigration extends Boot { public const BOOT = [ // you may ensure the migration boot. Migration::class, ]; public function boot(Migration $migration) { // Install migrations $migration->install(TranslationFiles::class); } }
应用示例
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); $app->boot(TranslationFilesMigration::class); //... // Run the app $app->run();
添加翻译
您可以通过以下方式简单添加更多翻译
use Tobento\App\AppFactory; use Tobento\Service\Translation\TranslatorInterface; use Tobento\Service\Translation\Resource; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); // using the app on method: $app->on(TranslatorInterface::class, function(TranslatorInterface $translator) { $translator->resources()->add(new Resource('*', 'de', [ 'Hello World' => 'Hallo Welt', ])); $translator->resources()->add(new Resource('shop', 'de', [ 'Cart' => 'Warenkorb', ])); }); // Run the app $app->run();
您可以通过查看 添加资源 部分来了解更多。
自定义翻译
您可以通过以下方式自定义/覆盖翻译
通过添加具有更高优先级的资源
use Tobento\App\AppFactory; use Tobento\Service\Translation\TranslatorInterface; use Tobento\Service\Translation\Resource; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); // using the app on method: $app->on(TranslatorInterface::class, function(TranslatorInterface $translator) { $translator->resources()->add(new Resource( name: 'shop', locale: 'de', translations: ['Hello World' => 'Hallo Welt'], priority: 200, // set a higher the default added )); }); // Run the app $app->run();
您可能只需要指定要覆盖的翻译,因为同名的资源将被合并。
您可以通过查看 添加资源 部分来了解更多。
通过添加具有更高优先级的新 trans 目录
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs()->dir(realpath(__DIR__.'/../app/'), 'app'); // Add trans directory with higher priority: $this->app->dirs()->dir( dir: $this->app->dir('app').'trans-custom/', // do not use 'trans' as name for migration purposes name: 'trans.custom', group: 'trans', // add higher priority as default trans dir: priority: 300, ); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); // Run the app $app->run();
然后,只需在定义的目录中添加您希望覆盖的翻译文件。如果文件不存在,则使用默认 trans 目录中的文件。
使用翻译管理器
进行中...