PHPackages                             wufr/php-language-localizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. wufr/php-language-localizer

ActiveLibrary[Localization &amp; i18n](/categories/localization)

wufr/php-language-localizer
===========================

Translate keys into text values based on set language

v1.6.0(11mo ago)111[1 PRs](https://github.com/wUFr/php-language-localizer/pulls)MITPHPPHP &gt;=8.0CI passing

Since Dec 11Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/wUFr/php-language-localizer)[ Packagist](https://packagist.org/packages/wufr/php-language-localizer)[ RSS](/packages/wufr-php-language-localizer/feed)WikiDiscussions release-candidate Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (13)Used By (0)

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.*

[![Latest Version on Packagist](https://camo.githubusercontent.com/f55ff313cedad6339ab5fa82fe52807f0102a54ee29223d13fd53207b0a94cd9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f777566722f7068702d6c616e67756167652d6c6f63616c697a65722e737667)](https://packagist.org/packages/wufr/php-language-localizer)[![PHP Version](https://camo.githubusercontent.com/3ab38f09717dc4431a6a84b957f33962fb535b6a0aa553a9912e14688a4ce06a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e302d383839324246)](https://php.net)[![License](https://camo.githubusercontent.com/9daba9e988346d0b35c3f617ecbd527f240913fc9a730255d0a4741aaee5c210/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f775546722f7068702d6c616e67756167652d6c6f63616c697a6572)](LICENSE.md)

Overview
--------

[](#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
--------

[](#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
------------

[](#installation)

Install the library using Composer:

```
composer require wufr/php-language-localizer
```

**Requirements:**

- PHP 8.0 or higher

Basic Usage
-----------

[](#basic-usage)

### Initialization

[](#initialization)

```
// Import the Translator class
use wUFr\Translator;

// Create a new translator instance
$translator = new Translator(
    dir: "./locales/",  // Directory containing translation files
    lang: "en_US"       // Language code
);

// You can also set/change these values after initialization
$translator->setDirectory("./custom/locales/");
$translator->setLanguage("fr_FR");
```

### Simple String Translation

[](#simple-string-translation)

```
// Translate a simple string
echo $translator->locale("common/general", "welcome");
// Output: "Welcome to our application"
```

Organization
------------

[](#organization)

### Folder Structure

[](#folder-structure)

```
/locales/               # Base directory for all translations
    /en_US/             # English (US) translations
        /common/        # Common translations
            general.php # General terms
            errors.php  # Error messages
        /admin/         # Admin panel translations
            dashboard.php
            users.php
    /fr_FR/             # French translations
        /common/
            general.php
            errors.php
        ...

```

### Translation File Format

[](#translation-file-format)

Each translation file should return an array using the `$l` variable:

```
