impulsephp / translation
Modular translation system for the ImpulsePHP framework with multilingual support, domain based files and automatic fallback.
Requires
- php: >=8.2
Requires (Dev)
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2025-08-03 10:22:44 UTC
README
A modular translation system for the ImpulsePHP framework with multilingual support, domain based files and automatic fallback to English.
Requirements
- PHP 8.2 or higher
Installation
Use Composer to install the package:
composer require impulsephp/translation
After installation the service provider Impulse\Translation\TranslatorProvider
can be registered within your application container if your framework does not handle it automatically.
Usage
Translations are organized in domain files placed under a translations
directory. Each locale has its own subdirectory. For example:
translations/
en/
messages.php
fr/
messages.php
Each file must return an associative array:
<?php return [ 'hello' => 'Hello', 'welcome' => 'Welcome {name}', ];
Create a Translator
instance by providing the locale:
use Impulse\Translation\Translator; $translator = new Translator('fr');
Basic translation
echo $translator->trans('messages.hello'); // Bonjour
Parameter replacement and fallback
echo $translator->trans('messages.welcome', ['name' => 'John']); // Welcome John (falls back to English because the key is missing in French)
Changing locale
$translator->setLocale('en');
Namespaces
You can add additional translation paths using namespaces:
$translator->addNamespace('package', '/path/to/package/translations'); $translator->trans('package::messages.key');
Running Tests
Install development dependencies and run PHPUnit:
composer install
composer test
License
This project is released under the MIT License.