peteraba/foo-translate

Library for making international applications in Opulence easy.

dev-scrutinizer-patch-2 2017-04-19 21:08 UTC

This package is auto-updated.

Last update: 2024-04-09 20:59:04 UTC


README

Library for making multilingual applications in Opulence easy.

Build Status License composer.lock Scrutinizer Code Quality Code Coverage Build Status

Setup

Install the library via composer:

composer install peteraba/foo-translate

Add the bootstrapper to you application:

# config/bootstrappers.php

return [
    // ...
    Foo\Translate\Bootstrapper\TranslatorBootstrapper::class,
];

Add your translations in resources/lang as it already exist for validation.

Add your default language to your .env.app.php:

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

User classes can access the translator via loading from the IOC Container as ITranslator.

In Fortune you can call the helper tr to retrieve your translations.

Example

resources/lang/en/form.php

<?php

return [
    'createNewLabel' => 'Create new %s',
];

src/Project/Form/Login.php

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

<button>{{ tr("form:createNewLabel", $entityName) }}</button>

Notes

  1. The library will default to use English (en) as default language if one is not provided.
  2. The bootrapper is not Lazy loaded, because international application usually need translations throughout the application.
  3. At the moment translations are not cached, but that's a planned feature.