Library for making multilingual applications in Opulence easy., (*1)
, (*2)
Setup
Install the library via composer:, (*3)
composer install peteraba/foo-translate
Add the bootstrapper to you application:, (*4)
# config/bootstrappers.php
return [
// ...
Foo\Translate\Bootstrapper\TranslatorBootstrapper::class,
];
Add your translations in resources/lang
as it already exist for validation., (*5)
Add your default language to your .env.app.php
:, (*6)
Environment::setVar('DEAFULT_LANGUAGE', "hu");
Usage
Files under resources/lang/${DEFAULT_LANG}
will be loaded automatically. Values defined in the language files are
namespaced by a :
character, so the value mainPageTitle defined in application.php
can be referenced as application:mainPageTitle, (*7)
User classes can access the translator via loading from the IOC Container as ITranslator., (*8)
In Fortune you can call the helper tr to retrieve your translations., (*9)
Example
resources/lang/en/form.php, (*10)
<?php
return [
'createNewLabel' => 'Create new %s',
];
src/Project/Form/Login.php, (*11)
class Login
{
/**
* @param ITranslator $translator
* @param string $entityName
*/
public function __construct(ITranslator $translator, string $entityName)
{
$this->translator = $translator;
$this->entityName = $entityName;
}
/**
* @return Button
*/
public function createSaveButton(): Button
{
return new Button($this->translator->translate('form:createNewLabel', $this->entityName));
}
}
resources/views/forms/login.fortune.php, (*12)
<button>{{ tr("form:createNewLabel", $entityName) }}</button>
Notes
- The library will default to use English (en) as default language if one is not provided.
- The bootrapper is not Lazy loaded, because international application usually need translations throughout the application.
- At the moment translations are not cached, but that's a planned feature.