elcheco / translator
Translation system using neon files, supports plurals and string replacements, fallback language. Extension to Nette Framework ^3.0
Installs: 1 506
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
- ext-intl: *
- ext-tokenizer: *
- nette/di: ^3.1
- nette/neon: ^3.0
- nette/safe-stream: ^2.4|^3.0
- nette/utils: ^3.0|^4.0
- psr/log: ^1.0
Requires (Dev)
- mockery/mockery: ^1.5
- nette/tester: ^2.0
- phpstan/phpstan: ^1.10
README
Lightweight and powerful translation system for PHP 8.0+, build as component not only for Nette framework
Note: Inspired by rostenkowski/translate, but I needed support for Nette Framework ^3.2|^4.0 and fallback translation possibility. I also refactored a bit the plurals to be naturally understandable.
Install
composer require elcheco/translator
Translations
Translations are stored in *.neon files in this format:
# simple message Hi!: Ahoj! # supporting placeholder Hi %s!: Ahoj %s! # supporting also plurals in multiple forms # in English it's easy to use You have %s points.: 0: You have no points 1: You have %s point. 2: You have %s points. # but for example in Czech plurals are a bit more complicated You have %s points.: 0: Nemáte žádné body. 1: Máte %s bod. "2-4": Máte %s body. 5: Máte %s bodů.
Usage with Nette Framework
Put your translations to %appDir%/translations
directory as cs_CZ.neon
etc.
# register extension extensions: translate: ElCheco\Translator\Extension # configuration translator: default: en_US fallback: cs_CZ
Usage with plain PHP
<?php namespace App; require __DIR__ . '/vendor/autoload.php'; use ElCheco\Translator\Translator; use ElCheco\Translator\NeonDictionary\NeonDictionaryFactory; // both translations and cache are in the same directory $translator = new Translator(new NeonDictionaryFactory(__DIR__, __DIR__)); $translator->setLocale('cs_CZ'); $translator->translate('Welcome!');
Requirements
- PHP 8.0+
- nette/di
- nette/neon
- nette/safe-stream
- nette/utils
- nette/tester