1
1
# Light Localziation for PHP
2
2
3
- > A light weight and path-based PHP localization library that translations are loaded up when needed.
3
+ > A light weight, key-value & path-based PHP localization library that translations are loaded up when needed.
4
4
5
5
## 🫡 Usage
6
6
@@ -18,9 +18,13 @@ composer require nabeghe/light-localization
18
18
- In this directory, create new folders, each of these folders actually represent Localization codes.
19
19
They can be language codes or anything else.
20
20
- Inside each code directory, php files will be placed, each of these files has the role of a translator.
21
- These files can return an array or an object that implements the ArrayAccess interface. If it's an array, each key
22
- will represent a value, but if it's an object, each field or method is a translation key.
23
- The priority is with the method & it must return a value. With the method, you can have dynamic localization!
21
+
22
+ - These files can return an array or an object that inheritance from ` Nabeghe\LightLocalization\Translator ` class.
23
+ - If it's an array, each key is a translation key & it will represent a value,
24
+ but if it's an object, each field or method is a translation key.
25
+ The priority is with the method & it must return a value.
26
+ - With the method, you can have dynamic localization!
27
+ - Objects can implement the ` ArrayAccess ` interface in addition to inheriting from the mentioned class.
24
28
25
29
### Examples
26
30
@@ -29,27 +33,48 @@ Check the examples folder in the repositiry.
29
33
``` php
30
34
use Nabeghe\LightLocalization\Localizer;
31
35
32
- $localizer = new Localizer(__DIR__ . '/langs');
36
+ /*
37
+ * Example of Translations Directory:
38
+ | /langs/
39
+ | /en/
40
+ | main.php
41
+ | messages.php
42
+ | /fa/
43
+ | main.php
44
+ | messages.php
45
+ */
46
+
47
+ $defaultLocalizer = new Localizer(__DIR__.'/langs', 'en');
48
+ $localizer = new Localizer(__DIR__.'/langs', 'fa', $defaultLocalizer);
49
+
50
+ // value of `title` key, from `main.php` translation file (default value for second argument).
33
51
echo $localizer->get('title');
52
+
53
+ // value of `hello` key, from `messages.php` translation file.
54
+ echo $localizer->get('hello', 'messages');
34
55
```
35
56
36
57
** Notice:** The localization code can be specified in the constructor method.
37
58
Of course, it's possible to change it later via method ` recode ` .
38
59
60
+ ** Notice:** "For each localizer, a default localizer can be specified in the constructor.
61
+ Additionally, instead of specifying a default localizer, a string can be designated as the default translation."
62
+
39
63
## 🧩 Features
40
64
41
- - Get the value using the key.
65
+ - Get the value (translation) using the key.
42
66
- Localization code (the second parameter of Localizer constructor).
43
- - Default translation value if the key doesn't exists,
67
+ - Default translation that can be a string or another localizer.
44
68
(the third parameter of Localizer constructor).
45
69
- Create different translators in different files.
70
+ - Dynamic translations using methods in the class-based translation files.
46
71
- Reload the translator.
47
72
- Remove the loaded translator.
48
73
- Refreshing translators and reloading them.
49
74
- Changing the localization code.
50
75
51
76
## 📖 License
52
77
53
- Copyright (c) 2023 Hadi Akbarzadeh
78
+ Copyright (c) 2024 Hadi Akbarzadeh
54
79
55
80
Licensed under the MIT license, see [ LICENSE.md] ( LICENSE.md ) for details.
0 commit comments