bckp/translator-core

Simple and fast PHP translator

v1.0.2 2019-11-29 15:59 UTC

This package is auto-updated.

Last update: 2019-12-02 11:50:32 UTC


README

Downloads this Month Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version License

Simple and fast PHP translator

Usage

For each language, we create Catalogue, that will compile PHP cache file with translations.

$catalogue = new Catalogue(new PluralProvider(), './path/to/cache', 'cs');
$catalogue->addFile('./path/to/locales/errors.cs.neon');
$catalogue->addFile('./path/to/locales/messages.cs.neon');

// Enable debug mode, disabled by default
$catalogue->setDebugMode(true);

$compiledCatalogue = $catalogue->compile();
$translator = new Translator($compiledCatalogue);

$translator->translate('errors.error.notFound'); // Will output "Soubor nenalezen"
$translator->translate(['messages.plural', 4]); // Will output "4 lidé"
$translator->translate('messages.withArgs', 'Honza', 'poledne'); // Will output "Ahoj, já jsem Honza, přeji krásné poledne"
$translator->translate('messages.withArgsRev', 'Honza', 'poledne'); // Will output "Krásné poledne, já jsem Honza"

Debug mode will made translator slower, it will check every time you call compile() if some of language files did change or not, and if they do, automaticly recompile cache, this is Good for development, but BAD on production mode.

Or you can use TranslatorProvider

$catalogue = new Catalogue(new PluralProvider(), './path/to/cache', 'cs');
$catalogue->addFile('./path/to/locales/errors.cs.neon');
$catalogue->addFile('./path/to/locales/messages.cs.neon');

$provider = new TranslatorProvider(['cs','en']);
$provider->addCatalogue('cs', $catalogue);

$translator = $provider->getTranslator('cs');

Great about this approach is, if you use more then one language, and switch between them, translator provider will compile catalogue only on first use, then use the compiled one.

Translation file format

Translation files are written in NEON format. Plural strings are in ARRAY, otherwise STRING.

welcome: 'Vítejte'
withArgs: 'Ahoj, já jsem %s, přeji krásné %s'
withArgsRev: 'Krásné %2$s, já jsem %1$s'
plural:
	zero: 'žádný člověk'
	one: 'jeden člověk'
	few: '%d lidé'
	other: '%d lidí'
next: 'This is next translation'

If you want more structure, you can use DOT as character, so lets say we want 3 error messages, we will create structure like this

error.notFound: 'Soubor nenalezen'
error.notMatch: 'Soubor neodpovídá zadání'
error.typeMismatch: 'Neplatný typ souboru'

Diagnostics

You can use simple diagnostics (or implement one), if you want to know, what is wrong with your translations, this is primary for framework integration (like Nette one).