Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeghe committed Apr 26, 2024
1 parent dd7ff1b commit 5385114
Show file tree
Hide file tree
Showing 20 changed files with 1,988 additions and 161 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"ArrayBasedTranslators::testForDefaultTranslation":3,"ArrayBasedTranslators::testForDifferentTranslator":3,"ClassBasedTranslators::testForDefaultTranslation":3,"ClassBasedTranslators::testForDifferentTranslator":3},"times":{"ArrayBasedTranslators::testForGet":0.007,"ArrayBasedTranslators::testForDefaultTranslation":0.001,"ArrayBasedTranslators::testForDifferentTranslator":0,"ClassBasedTranslators::testDynamicMessage":0.156,"ClassBasedTranslators::testForGet":0.005,"ClassBasedTranslators::testForDefaultTranslation":0,"ClassBasedTranslators::testForDifferentTranslator":0}}
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Light Localziation for PHP

> A light weight and path-based PHP localization library that translations are loaded up when needed.
> A light weight, key-value & path-based PHP localization library that translations are loaded up when needed.
## 🫡 Usage

Expand All @@ -18,9 +18,13 @@ composer require nabeghe/light-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 implements the ArrayAccess interface. If it's an array, each key
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!

- 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

Expand All @@ -29,27 +33,48 @@ Check the examples folder in the repositiry.
```php
use Nabeghe\LightLocalization\Localizer;

$localizer = new Localizer(__DIR__ . '/langs');
/*
* 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 using the key.
- Get the value (translation) using the key.
- Localization code (the second parameter of Localizer constructor).
- Default translation value if the key doesn't exists,
- 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) 2023 Hadi Akbarzadeh
Copyright (c) 2024 Hadi Akbarzadeh

Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "nabeghe/light-localization",
"description": "A light weight and path-based PHP localization library that translations are loaded up when needed.",
"description": "A light weight, key-value & path-based PHP localization library that translations are loaded up when needed.",
"type": "library",
"version": "0.1.3",
"version": "1.0.0",
"homepage": "https://github.com/nabeghe/light-localization",
"license": "MIT",
"autoload": {
Expand All @@ -18,7 +18,13 @@
"role": "Developer"
}
],
"scripts": {
"test": "vendor/bin/phpunit tests"
},
"require": {
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
}
}
Loading

0 comments on commit 5385114

Please sign in to comment.