imponeer/symfony-translations-constants-loader

symfony/translation extension to load PHP files that are defined all translations as constants

v2.1.0 2025-06-05 22:07 UTC

This package is auto-updated.

Last update: 2025-06-05 22:08:38 UTC


README

Packagist License Packagist Version Packagist Dependency Version

Symfony Translations Constants Loader

Extension for symfony/translation package to load translations defined as constants list in PHP file.

Installation

To install and use this package, we recommend to use Composer:

composer require imponeer/symfony-translations-constants-loader

Otherwise you need to include manualy files from src/ directory.

Usage Example

1. Create a file with define() constants:

// translations/en/messages.php

define('HELLO', 'Hello!');
define('GOODBYE', 'Goodbye!');

💡 You can create separate files for other locales, such as translations/fr/messages.php.

2. Load the constants using the PHPFileLoader:

use Symfony\Component\Translation\Translator;
use Imponeer\SymfonyTranslationsConstantsLoader\PHPFileLoader;

$translator = new Translator('en');

// Register the loader for the 'php_consts' format
$translator->addLoader('php_consts', new PHPFileLoader());

// Add your translation resource
$translator->addResource('php_consts', __DIR__ . '/translations/en/messages.php', 'en');

// Use translations
echo $translator->trans('HELLO');   // Outputs: Hello!
echo $translator->trans('GOODBYE'); // Outputs: Goodbye!

Development

Code Style

This project follows the PSR-12 coding standard. To check your code for style issues:

composer phpcs

To automatically fix most coding standards issues:

composer phpcbf

Testing

To run the test suite, execute:

vendor/bin/phpunit

This will run all tests in the tests/ directory.

Static Analysis

This project uses PHPStan for static code analysis at level 5. PHPStan helps detect bugs and potential issues without running the code.

To run the static analysis:

composer phpstan

The analysis checks both the src/ and tests/ directories. Configuration can be found in the phpstan.neon file.

How to contribute?

If you want to add some functionality or fix bugs, you can fork, change and create pull request. If you not sure how this works, try read GitHub documentation about git.

Please make sure your code follows the PSR-12 coding standard by running the code style checks before submitting a pull request.

If you found any bug or have some questions, use issues tab and write there your questions.