bckp / translator-core
简单快速的PHP翻译器
v1.1.1
2020-12-05 14:14 UTC
Requires
- php: ^7.1|^8.0
- nette/neon: >=2.4
- nette/php-generator: >3.3 <4
Requires (Dev)
- nette/tester: ^2.3
- phpstan/phpstan: ^0.12
- squizlabs/php_codesniffer: *
Suggests
- bckp/translator-nette: For NETTE support
README
简单快速的PHP翻译器
用法
对于每种语言,我们创建目录,该目录将编译包含翻译的PHP缓存文件。
$catalogue = new Catalogue(new PluralProvider(), './path/to/cache', 'cs'); $catalogue->addFile('./path/to/locales/errors.cs.neon'); $catalogue->addFile('./path/to/locales/messages.cs.neon'); // Enable debug mode, disabled by default $catalogue->setDebugMode(true); $compiledCatalogue = $catalogue->compile(); $translator = new Translator($compiledCatalogue); $translator->translate('errors.error.notFound'); // Will output "Soubor nenalezen" $translator->translate(['messages.plural', 4]); // Will output "4 lidé" $translator->translate('messages.withArgs', 'Honza', 'poledne'); // Will output "Ahoj, já jsem Honza, přeji krásné poledne" $translator->translate('messages.withArgsRev', 'Honza', 'poledne'); // Will output "Krásné poledne, já jsem Honza"
您可以通过回调添加自己的文本源
$catalogue = new Catalogue(new PluralProvider(), './path/to/cache', 'cs'); $catalogue->addDynamic('errors', function(array &$messages, string $resource, string $locale){ $messages['common'] = 'Common error translation'; $messages['critical'] = $this->database->fetchAll('translations')->where('resource = ? and locale = ?', $resource, $locale); }); $catalogue->addFile('./path/to/locales/messages.cs.neon'); // if you add new file errors.cs.neon, it will be overwritten by dynamic, as they is processed later // Enable debug mode, disabled by default $catalogue->setDebugMode(true); // You can even add events for onCheck // $timestamp contains timestamp of last file generation // but remember, this will called only on debug mode! $catalogue->addCheckCallback(function(int $timestamp){ if ($timestamp < $this->database->fetchSingle('select last_update from settings where caption = ?', 'translations')){ throw new BuilderException('rebuild required'); } }); // And events for onCompile // this will occur when app have prepared all translations into single // big array, and you want to modify it $catalogue->addCompileCallback(function(array &$messages, string $locale){ // in messages, we have all the translations $messages['errors.common'] = 'Modify common error translation'; }); $compiledCatalogue = $catalogue->compile(); $translator = new Translator($compiledCatalogue); $translator->translate('errors.common'); // Will output "Modify common error translation"
调试模式会减慢翻译器的速度,每次调用compile()时,它都会检查某些语言文件是否已更改,如果已更改,则自动重新编译缓存,这在开发中是好的,但在生产模式中是糟糕的。
或者您可以使用TranslatorProvider
$catalogue = new Catalogue(new PluralProvider(), './path/to/cache', 'cs'); $catalogue->addFile('./path/to/locales/errors.cs.neon'); $catalogue->addFile('./path/to/locales/messages.cs.neon'); $provider = new TranslatorProvider(['cs','en']); $provider->addCatalogue('cs', $catalogue); $translator = $provider->getTranslator('cs');
这种方法的优点是,如果您使用多种语言并在它们之间切换,翻译提供程序仅在第一次使用时编译目录,然后使用编译后的目录。
翻译文件格式
翻译文件使用NEON格式编写。复数字符串在数组中,否则为字符串。
welcome: 'Vítejte' withArgs: 'Ahoj, já jsem %s, přeji krásné %s' withArgsRev: 'Krásné %2$s, já jsem %1$s' plural: zero: 'žádný člověk' one: 'jeden člověk' few: '%d lidé' other: '%d lidí' next: 'This is next translation'
如果您想有更多的结构,您可以使用点作为字符,例如,如果我们想创建3个错误消息,我们将创建如下结构
error.notFound: 'Soubor nenalezen' error.notMatch: 'Soubor neodpovídá zadání' error.typeMismatch: 'Neplatný typ souboru'
诊断
您可以使用简单的诊断(或实现一个),如果您想了解您的翻译有什么问题,这主要用于框架集成(如Nette框架)。