Download the PHP package wufr/php-language-localizer without Composer
On this page you can find all versions of the php package wufr/php-language-localizer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wufr/php-language-localizer
More information about wufr/php-language-localizer
Files in wufr/php-language-localizer
Package php-language-localizer
Short Description Translate keys into text values based on set language
License MIT
Informations about the package php-language-localizer
PHP Language Localizer
A powerful, flexible PHP library for translating and localizing applications with support for gender-based translations, pluralization, and parameter replacement.
Just a weekend project so far, also to experiment with Github Copilot to rewrite and extend this library.
Overview
PHP Language Localizer provides a simple yet comprehensive solution for implementing multi-language support in your PHP applications. The library allows you to structure translations in a clean, maintainable folder hierarchy while providing advanced features like gender-based translations, pluralization, and parameter replacement.
Features
- Simple Key-Based Translation: Convert translation keys to localized strings
- Flexible Folder Structure: Organize translations in a logical folder hierarchy
- Parameter Replacement: Insert dynamic values into translated strings
- Pluralization Support: Handle different translations based on quantity
- Gender-Based Translation: Support for male, female, neutral, and entity (object/animal) forms
- Combined Rules: Mix gender and quantity rules for complex language scenarios
- Type Safety: Built with PHP 8.x features including enums and match expressions
- Modern Implementation: Utilizes readonly properties and proper type hints
Installation
Install the library using Composer:
Requirements:
- PHP 8.0 or higher
Basic Usage
Initialization
Simple String Translation
Organization
Folder Structure
Translation File Format
Each translation file should return an array using the $l
variable:
Advanced Features
Parameter Replacement
Insert dynamic values into translations:
Pluralization with Counters
Handle different forms based on quantity:
When using the _counter
parameter, the library selects the appropriate translation by finding the highest key that is less than or equal to the counter value.
Gender-Based Translations
Handle gender-specific language forms:
Combined Gender and Counter Translations
For complex language rules that need both gender and quantity:
Using with Parameters and Counters
Combine counters with parameter replacement:
Note: The _counter
parameter is only used to determine which string to return, not as a variable inside the string. Add another parameter (like count
in this example) to use the number within the text.
Using with PHP 8.1+ Enums
For type-safe gender-based translations, use the provided Gender
enum:
Helpful Enum Methods
The Gender
enum provides utility methods for working with pronouns:
Error Handling
The library provides clear error messages when translations are missing:
API Reference
Translator Class
Gender Enum
Contributing
To contribute, please create a new branch from the release-candidate
branch and submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.