Skip to content

Commit 363b23b

Browse files
committed
Initial commit
0 parents  commit 363b23b

File tree

10 files changed

+370
-0
lines changed

10 files changed

+370
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
/vendor/

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Hadi Akbarzadeh
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Light Localziation for PHP
2+
3+
> A light weight and path-based PHP localization library that translations are loaded up when needed.
4+
5+
## 🫡 Usage
6+
7+
### 🚀 Installation
8+
9+
You can install the package via composer:
10+
11+
```bash
12+
composer require nabeghe/light-localization
13+
```
14+
15+
### 📁 Localization Directory
16+
17+
- Create a directory for localization.
18+
- In this directory, create new folders, each of these folders actually represent Localization codes.
19+
They can be language codes or anything else.
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!
24+
25+
### Examples
26+
27+
Check the examples folder in the repositiry.
28+
29+
```php
30+
use Nabeghe\LightLocalization\Localizer;
31+
32+
$localizer = new Localizer(__DIR__ . '/langs');
33+
echo $localizer->get('title');
34+
```
35+
36+
**Notice:** The localization code can be specified in the constructor method.
37+
Of course, it's possible to change it later via method `recode`.
38+
39+
## 🧩 Features
40+
41+
- Get the value using the key.
42+
- Localization code (the second parameter of Localizer constructor).
43+
- Default translation value if the key doesn't exists,
44+
(the third parameter of Localizer constructor).
45+
- Create different translators in different files.
46+
- Reload the translator.
47+
- Remove the loaded translator.
48+
- Refreshing translators and reloading them.
49+
- Changing the localization code.
50+
51+
## 📖 License
52+
53+
Copyright (c) 2023 Hadi Akbarzadeh
54+
55+
Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.

composer.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "nabeghe/light-localization",
3+
"description": "A light weight and path-based PHP localization library that translations are loaded up when needed.",
4+
"type": "library",
5+
"version": "0.1.0",
6+
"homepage": "https://github.com/Nabeghe/LightLocalization",
7+
"license": "MIT",
8+
"autoload": {
9+
"psr-4": {
10+
"Nabeghe\\LightLocalization\\": "src/"
11+
}
12+
},
13+
"authors": [
14+
{
15+
"name": "Hadi Akbarzadeh",
16+
"email": "hadicoder@gmail.com",
17+
"homepage": "https://elatel.ir",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {}
22+
}

examples/array/langs/generic/main.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'title' => 'Light Localization',
5+
'message' => 'Hey Honey, are you ready?',
6+
'primary_btn_title' => 'Yes',
7+
'secondary_btn_title' => 'No',
8+
'success' => 'Welcome',
9+
'unsuccess' => 'Bye',
10+
];

examples/array/localizer.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Nabeghe\LightLocalization\Localizer;
4+
5+
require_once __DIR__ . '/../../vendor/autoload.php';
6+
7+
$localizer = new Localizer(__DIR__ . '/langs');
8+
echo $localizer->get('title');
9+
echo PHP_EOL;
10+
echo $localizer->get('message');
11+
echo PHP_EOL;
12+
echo $localizer->get('primary_btn_title') . '|' . $localizer->get('secondary_btn_title');
13+
echo PHP_EOL;
14+
echo $localizer->get('success');
15+
echo PHP_EOL;
16+
echo $localizer->get('unsuccess');

examples/class/langs/generic/main.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Nabeghe\LightLocalization\Translator;
4+
5+
return new class extends Translator {
6+
public string $title = 'Light Localization';
7+
8+
public string $primary_btn_title = 'Yes';
9+
10+
public string $secondary_btn_title = 'No';
11+
12+
public string $success = 'Welcome';
13+
14+
public string $unsuccess = 'Bye';
15+
16+
public function message()
17+
{
18+
$msgs = [
19+
'Hey Honey, are you ready?',
20+
'Hey Babe, Let\'s go?',
21+
'Yeah darling... Come with me.',
22+
'Hello Sweetheart, ready for some fun?',
23+
'Hey Love, what do you think about a little adventure?',
24+
'Sweetie, shall we make this a memorable day?',
25+
];
26+
return $msgs[array_rand($msgs)];
27+
}
28+
};

examples/class/localizer.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Nabeghe\LightLocalization\Localizer;
4+
5+
require_once __DIR__ . '/../../vendor/autoload.php';
6+
7+
$localizer = new Localizer(__DIR__ . '/langs');
8+
var_dump($localizer->getTranslators());
9+
echo $localizer->get('title');
10+
echo PHP_EOL;
11+
echo $localizer->get('message');
12+
echo PHP_EOL;
13+
echo $localizer->get('primary_btn_title') . '|' . $localizer->get('secondary_btn_title');
14+
echo PHP_EOL;
15+
echo $localizer->get('success');
16+
echo PHP_EOL;
17+
echo $localizer->get('unsuccess');

src/Localizer.php

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Nabeghe\LightLocalization;
4+
5+
/**
6+
* Localizer class.
7+
* @package Nabeghe\LightLocalization
8+
*/
9+
class Localizer
10+
{
11+
/**
12+
* Default translator name.
13+
*/
14+
public const DEFAULT_TRANSLATOR = 'main';
15+
16+
/**
17+
* The root path where the directories related to the codes are located.
18+
* @var string
19+
*/
20+
protected string $path;
21+
22+
/**
23+
* Current directory code
24+
* @var string
25+
*/
26+
protected string $code;
27+
28+
/**
29+
* The default translation returned if the requested key (translation) doesn't exist.
30+
* @see self::get()
31+
* @var mixed
32+
*/
33+
protected $defaultTranslation;
34+
35+
/**
36+
* Loaded translators.
37+
* @var array
38+
*/
39+
protected array $translators = [];
40+
41+
/**
42+
* Gets the translators list.
43+
* @return array
44+
*/
45+
public function getTranslators(): array
46+
{
47+
return $this->translators;
48+
}
49+
50+
/**
51+
* Constructor.
52+
* @param string $path The root path where the directories related to the codes are located.
53+
* @param string $code Optional. Localization code. Default generic.
54+
* @param string $default_translation Optional. The default translation returned if the requested key (translation) doesn't exist.. Default empty string.
55+
*/
56+
public function __construct(string $path, string $code = 'generic', $default_translation = '')
57+
{
58+
$this->path = rtrim($path, '/');
59+
$this->code = $code;
60+
$this->defaultTranslation = $default_translation;
61+
}
62+
63+
/**
64+
* Gets the file path of a translator.
65+
* @param string $translator_name Optional. The translator name. Default main.
66+
* @return string
67+
*/
68+
public function getTranslatorPath($translator_name = self::DEFAULT_TRANSLATOR): string
69+
{
70+
return "$this->path/$this->code/$translator_name.php";
71+
}
72+
73+
/**
74+
* Checks if a translator is loaded or not.
75+
* @param $translator_name
76+
* @return bool
77+
*/
78+
public function isLoaded($translator_name): bool
79+
{
80+
return isset($this->translators[$translator_name]);
81+
}
82+
83+
/**
84+
* Loads a translator even if it is already loaded.
85+
* @param string $translator_name
86+
* @return bool
87+
*/
88+
public function load($translator_name = self::DEFAULT_TRANSLATOR): bool
89+
{
90+
$siccess = false;
91+
$branch_path = $this->getTranslatorPath($translator_name);
92+
if (file_exists($branch_path)) {
93+
$new_data = include $branch_path;
94+
if (!$new_data) {
95+
$new_data = [];
96+
} else {
97+
$siccess = true;
98+
}
99+
}
100+
$this->translators[$translator_name] = $new_data ?? [];
101+
return $siccess;
102+
}
103+
104+
/**
105+
* Loads all loaded translators from the beginning.
106+
*/
107+
public function refresh()
108+
{
109+
$translators_names = array_keys($this->translators);
110+
foreach ($translators_names as $translator_name) {
111+
$this->load($translator_name);
112+
}
113+
}
114+
115+
/**
116+
* Removes a translator from loaded state.
117+
* @param string $translator_name
118+
* @return bool
119+
*/
120+
public function unload($translator_name = self::DEFAULT_TRANSLATOR): bool
121+
{
122+
if (isset($this->translators[$translator_name])) {
123+
unset($this->translators[$translator_name]);
124+
return true;
125+
}
126+
return false;
127+
}
128+
129+
/**
130+
* Changes the localization code.
131+
* @param string $code
132+
* @param bool $refresh Optional. After changing the code, should it reload the loaded translators or remove all of them from the loaded state? Default false.
133+
*/
134+
public function recode($code, $refresh = false)
135+
{
136+
$this->code = $code;
137+
if ($refresh) {
138+
$this->refresh();
139+
} else {
140+
$this->translators = [];
141+
}
142+
}
143+
144+
/**
145+
* @param string $key The key is in the translator.
146+
* @param string $translator_name
147+
* @return string|mixed
148+
*/
149+
public function get(string $key, $translator_name = self::DEFAULT_TRANSLATOR)
150+
{
151+
if (!isset($this->translators[$translator_name])) {
152+
$this->load($translator_name);
153+
}
154+
return $this->translators[$translator_name][$key] ?? $this->defaultTranslation;
155+
}
156+
}

src/Translator.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Nabeghe\LightLocalization;
4+
5+
use ArrayAccess;
6+
7+
/**
8+
* Translator class.
9+
* @package Nabeghe\LightLocalization
10+
*/
11+
class Translator implements ArrayAccess
12+
{
13+
public function offsetExists($offset)
14+
{
15+
return method_exists($this, $offset)
16+
|| property_exists($this, $offset);
17+
}
18+
19+
public function offsetGet($offset)
20+
{
21+
if (method_exists($this, $offset)) {
22+
return $this->$offset();
23+
}
24+
25+
if (property_exists($this, $offset)) {
26+
return $this->$offset;
27+
}
28+
29+
return null;
30+
}
31+
32+
public function offsetSet($offset, $value)
33+
{
34+
$this->$offset = $offset;
35+
}
36+
37+
public function offsetUnset($offset)
38+
{
39+
if (property_exists($this, $offset)) {
40+
unset($this->$offset);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)