68publishers / translation-bridge
Bridges between integrations of translation component and '68publishers bundles.
Requires
- php: ^7.3
- nette/di: ^3.0.3
- nette/utils: ^3.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.4
- friendsofphp/php-cs-fixer: ^2.0
- mockery/mockery: ^1.4
- nette/bootstrap: ^3.0
- nette/tester: ^2.3.4
- roave/security-advisories: dev-master
Suggests
- contributte/translation: For integration with contributte/translation
- kdyby/translation: For integration with kdyby/translation
This package is auto-updated.
Last update: 2022-07-13 08:44:35 UTC
README
A package contains bridges for the most used integrations of symfomy/translation into Nette Framework:
Why? Because we want to keep our bundles independent from specific integrations so applications can use any of the integrations mentioned above and will be still compatible with our bundles.
Installation
The best way to install 68publishers/translation-bridge is using Composer:
$ composer require 68publishers/translation-bridge
Configuration
extensions: # If you are using kdyby/translation: translation_bridge: SixtyEightPublishers\TranslationBridge\Bridge\Kdyby\DI\TranslationBridgeExtension # Or if you are using contributte/translation: translation_bridge: SixtyEightPublishers\TranslationBridge\Bridge\Contributte\DI\TranslationBridgeExtension
Usage
Translation Resources Provider
Extensions can provide paths with translation resources.
<?php use Nette\DI\CompilerExtension; use SixtyEightPublishers\TranslationBridge\DI\TranslationProviderInterface; final class FooBundleExtension extends CompilerExtension implements TranslationProviderInterface { public function getTranslationResources(): array { return [ __DIR__ . '/../translations', ]; } }
Translator Aware
All services that implement an interface TranslatorAwareInterface
will automatically receive the Translator instance.
<?php use SixtyEightPublishers\TranslationBridge\TranslatorAwareTrait; use SixtyEightPublishers\TranslationBridge\TranslatorAwareInterface; final class FooService implements TranslatorAwareInterface { use TranslatorAwareTrait; public function doSomething(): void { $this->translator->translate('....'); } }
Prefixed Translator Factory
The Container contains an autowired service of type PrefixedTranslatorFactoryInterface
for creating prefixed translators.
<?php use SixtyEightPublishers\TranslationBridge\PrefixedTranslatorFactoryInterface; final class FooService { private $translator; public function __construct(PrefixedTranslatorFactoryInterface $prefixedTranslatorFactory) { $this->translator = $prefixedTranslatorFactory->create('FooService'); } }
Translator Localizer
The Container contains an service of type TranslatorLocalizerInterface
for manipulating with the Translator locale.
use SixtyEightPublishers\TranslationBridge\Localization\TranslatorLocalizerInterface; final class FooService { private $localizer; public function __construct(TranslatorLocalizerInterface $localizer) { $this->localizer = $localizer; } public function doSomething(): void { # Get the current locale $locale = $this->localizer->getLocale(); # Set the new locale $this->localizer->setLocale('cs_CZ'); } }
Translator Locale Resolver
The Translator's locale can be resolved with own resolvers like this:
<?php use Nette\Localization\ITranslator; use SixtyEightPublishers\TranslationBridge\Localization\TranslatorLocaleResolverInterface; final class MyLocaleResolver implements TranslatorLocaleResolverInterface { public function resolveLocale(ITranslator $translator) : ?string { # return a valid locale or NULL } }
Resolvers can be registered manually with a tag 68publishers.translation_bridge.translator_locale_resolver
or through a CompilerExtension
and each resolver can have priority.
They are sorted by priority in descending order so a Resolver with the highest priority will be called first. A default priority is 0.
Resolvers defined in Kdyby
and Contributte
integrations are automatically wrapped and provided into the main Resolver. Their priority is always 10.
services: - type: MyLocaleResolver tags: 68publishers.translation_bridge.translator_locale_resolver: 15
Or
<?php use Nette\DI\CompilerExtension; use SixtyEightPublishers\TranslationBridge\DI\TranslatorLocaleResolver; use SixtyEightPublishers\TranslationBridge\DI\TranslatorLocaleResolverProviderInterface; final class FooBundleExtension extends CompilerExtension implements TranslatorLocaleResolverProviderInterface { public function getTranslatorLocaleResolvers(): array { return [ new TranslatorLocaleResolver(MyLocaleResolver::class, 15), ]; } }
Contributing
Before committing any changes, don't forget to run
$ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run
and
$ composer run tests