delboy1978uk / bone-i18n
I18n package for Bone Framework
Installs: 1 434
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- delboy1978uk/barnacle: ^2.3
- delboy1978uk/bone-http: ^2.3
- delboy1978uk/bone-view: ^v1.7
- delboy1978uk/form: ^2.4
- laminas/laminas-i18n: ^2.26
- laminas/laminas-servicemanager: ^3.3
- psr/http-message: ^2.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- codeception/codeception: ^5.0
- codeception/module-asserts: ^3.0
- delboy1978uk/bone-router: ^1.4
- laminas/laminas-diactoros: ^3.3
- roave/security-advisories: dev-latest
README
I18n package for Bone Framework
installation
bone-i18n is a core dependency of delboy1978uk/bone
, and so it is installed by default.
setup
The skeleton app has a directory for translations (usually data/translations
but you can set this to anything), which
will contain locale folders such as en_US
etc.
Drop in your .mo
and .po
files. Open config/bone-i18n.php
and tweak to suit:
<?php use Laminas\I18n\Translator\Loader\Gettext; return [ 'i18n' => [ 'enabled' => true, 'translations_dir' => 'data/translations', 'type' => Gettext::class, 'default_locale' => 'en_PI', 'supported_locales' => ['en_PI', 'en_GB', 'nl_BE', 'fr_BE'], 'date_format' => 'd/m/Y', ] ];
For any package you have that will contain translation files, edit your package class and make it implement
Bone\I18n\I18nRegistrationInterface
. Create the method public function getTranslationsDirectory(): string
that will
return the translations directory path.
usage
locale view helper
You can have routes prepended with the current locale by calling
<?= $this->locale() ?>
Or more conveniently
<?= $this->l() ?>
in your view files.
A link such as /user
would then become /en_US/user
. Bone Framework uses i18n middleware to fetch the locale and it
strips the locale from the URL and sets it as a Request Attribute, so you do not need to define routes with a locale
parameter.
controllers
In a controller action if you need the locale you can say:
$locale = $request->getAttribute('locale');
To get a translator into your controller, make it implement Bone\I18n\I18nAwareInterface
and use the
Bone\I18n\Traits\HasTranslatorTrait
. If you package's Package class returns the controller without going through the
Bone\Mvc\Controller\Init
class, change it now to this:
return Init::controller(new YourController(), $c);
You can now call $this->getTranslator()
which will return an instance of the translator.
translation view helper
To translate text in your view, call the following:
<?= $this->translate('whatever.key.to.translate') ;?>
Or again, more conveniently
<?= $this->t('whatever.key.to.translate') ;?>
i18n aware forms
Bone Framework uses delboy1978uk/form
for its form functionality. However, instead of extending Del\Form
, you can
create a form extending Bone\I18n\Form
, which takes the translator as a second argument.