efabrica/translatte

Translator for Nette framework

1.0.0 2019-04-26 13:32 UTC

README

Build Status Scrutinizer Code Quality Code Coverage

Translator for Nette framework.

Usage

use Efabrica\Translatte\Translator;
use Efabrica\Translatte\Resource\NeonDirectoryResource;

// Create new translator with default language
$translator = new Translator('sk_SK');

// Register new resources where translations are stored
$translator->addResource(new NeonDirectoryResource([__DIR__ . '/lang', __DIR__ . '/another/lang']));
$translator->addResource(new NeonResource(__DIR__ . '/dictionary.sk_SK.neon', 'sk_SK'));

// Translate basic string
$translator->translate('dictionary.forms.error']);

// Translate with pluralization
$translator->translate('key', 10);

// Translate with parameters
$translator->translate('key', ['name' => 'Peter']);

// Translate with pluralization and parameters
$translator->translate('key', 10, ['page' => 'login']);

// Select translation language on the fly
$translator->translate('key', 1, [], 'en_US');

Nette extension

extensions:
	translation: Efabrica\Translatte\DI\TranslationExtension
	
# Minimal configuration
translation:
    default: 'sk_SK' # mandatory

# Full configuration
translation:
    default: 'sk_SK' # mandatory
    fallback: # optional
        - 'en_US'
        - 'en_UK'
    dirs:
        - %appDir%/lang
    cache: Efabrica\Translatte\Cache\NullCache() # optional
    resolvers: # optional
        - Efabrica\Translatte\Resolver\StaticResolver('sk_Sk')
    resources: #optional
        - Efabrica\Translatte\Resource\NeonDirectoryResource(%appDir%/localize)

Syntactic sugar

dictionary.sk_SK.neon:

cart:
    products_in_cart: 'V košíku je jeden produkt|V košíku sú %count% produktov|V košíku je %count% produktov'

index.php:

// Translator setup
$translator = ...

// To params array is set count variable
$translator->translate('dictionary.cart.products_in_cart', 2); // V košíku sú 2 produkty

// Param count from params array is used to select right plural form
$translator->translate('dictionary.cart.products_in_cart', ['count' => 2]); // V košíku sú 2 produkty

// If we set both params nothing is override
$translator->translate('dictionary.cart.products_in_cart', 10, ['count' => 2]); // V košíku je 2 produktov

Main classes

Resolver

Resolves which language has translator to use.
Available resolvers:

  • StaticResolver - Resolves to given static lang.
  • ChainResolver - Multiple resolvers can be registered to this resolver. First resolver which returns non empty string is used.

Resource

Represents "storage" with translation strings. It can be anything - directory with translation files, database, redis or external api call.
Available resources:

  • NeonResource - One neon file.
  • NeonDirectoryResource - Multiple directories in which resource search neon files in format "{prefix}.{lang}.neon".

Cache

Used for cache generated directory.