Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nabeghe/light-localization
A light weight and path-based PHP localization library that translations are loaded up when needed.
https://github.com/nabeghe/light-localization
library localization localizer php php-library php-localization php-translation
Last synced: 13 days ago
JSON representation
A light weight and path-based PHP localization library that translations are loaded up when needed.
- Host: GitHub
- URL: https://github.com/nabeghe/light-localization
- Owner: nabeghe
- License: mit
- Created: 2024-01-19T00:17:14.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-09-19T16:43:53.000Z (2 months ago)
- Last Synced: 2024-11-03T05:22:59.481Z (17 days ago)
- Topics: library, localization, localizer, php, php-library, php-localization, php-translation
- Language: PHP
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Light Localziation for PHP
> A light weight, key-value & path-based PHP localization library that translations are loaded up when needed.
## 🫡 Usage
### 🚀 Installation
You can install the package via composer:
```bash
composer require nabeghe/light-localization
```### 📁 Localization Directory
- Create a directory for localization.
- In this directory, create new folders, each of these folders actually represent Localization codes.
They can be language codes or anything else.
- Inside each code directory, php files will be placed, each of these files has the role of a translator.- These files can return an array or an object that inheritance from `Nabeghe\LightLocalization\Translator` class.
- If it's an array, each key is a translation key & it will represent a value,
but if it's an object, each field or method is a translation key.
The priority is with the method & it must return a value.
- With the method, you can have dynamic localization!
- Objects can implement the `ArrayAccess` interface in addition to inheriting from the mentioned class.### Examples
Check the examples folder in the repositiry.
```php
use Nabeghe\LightLocalization\Localizer;/*
* Example of Translations Directory:
| /langs/
| /en/
| main.php
| messages.php
| /fa/
| main.php
| messages.php
*/$defaultLocalizer = new Localizer(__DIR__.'/langs', 'en');
$localizer = new Localizer(__DIR__.'/langs', 'fa', $defaultLocalizer);// value of `title` key, from `main.php` translation file (default value for second argument).
echo $localizer->get('title');// value of `hello` key, from `messages.php` translation file.
echo $localizer->get('hello', 'messages');
```**Notice:** The localization code can be specified in the constructor method.
Of course, it's possible to change it later via method `recode`.**Notice:** "For each localizer, a default localizer can be specified in the constructor.
Additionally, instead of specifying a default localizer, a string can be designated as the default translation."## 🧩 Features
- Get the value (translation) using the key.
- Localization code (the second parameter of Localizer constructor).
- Default translation that can be a string or another localizer.
(the third parameter of Localizer constructor).
- Create different translators in different files.
- Dynamic translations using methods in the class-based translation files.
- Reload the translator.
- Remove the loaded translator.
- Refreshing translators and reloading them.
- Changing the localization code.## 📖 License
Copyright (c) 2024 Hadi Akbarzadeh
Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.