Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
Translation | |
100.00% |
25 / 25 |
|
100.00% |
7 / 7 |
12 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
add | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
allLocales | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasMultiLocales | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
locale | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setLocale | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Dynart\Micro; |
4 | |
5 | /** |
6 | * Handles static text translations |
7 | * |
8 | * You can set the current locale, load the translation for the current locale and get text |
9 | * for the current locale with the help of this class. |
10 | * |
11 | * Related configuration values: |
12 | * * app.root_path - for the ~ symbol in the translations' folder path |
13 | * * translation.all - the all known translation locales seperated with commas, for example: "hu, en" |
14 | * * translation.default - if no locale was set this will be the default, for example: "en" |
15 | * |
16 | * @see Config |
17 | * @package Dynart\Micro |
18 | */ |
19 | class Translation { |
20 | |
21 | /** |
22 | * The configuration name of all of the known translation |
23 | */ |
24 | const CONFIG_ALL = 'translation.all'; |
25 | |
26 | /** |
27 | * The configuration name of the default translation |
28 | */ |
29 | const CONFIG_DEFAULT = 'translation.default'; |
30 | |
31 | /** |
32 | * The default locale |
33 | */ |
34 | const DEFAULT_LOCALE = 'en'; |
35 | |
36 | /** |
37 | * The folders for all of the translations in [namespace => path] format |
38 | * @var array |
39 | */ |
40 | protected $folders = []; |
41 | |
42 | /** |
43 | * The loaded translations in [namespace => [id => text]] format |
44 | * @var array |
45 | */ |
46 | protected $data = []; |
47 | |
48 | /** |
49 | * All of the known translations |
50 | * @var array |
51 | */ |
52 | protected $allLocales = []; |
53 | |
54 | /** |
55 | * Is it has a multi locale config? |
56 | * @var bool |
57 | */ |
58 | protected $hasMultiLocales = false; |
59 | |
60 | /** |
61 | * The current locale |
62 | * @var string |
63 | */ |
64 | protected $locale = 'en'; |
65 | |
66 | /** |
67 | * @var Config |
68 | */ |
69 | protected $config; |
70 | |
71 | /** |
72 | * Sets the `$locale`, the `$allLocales` and `$hasMultiLocales` members via the `$config` |
73 | * @param Config $config |
74 | */ |
75 | public function __construct(Config $config) { |
76 | $this->config = $config; |
77 | $this->locale = $config->get(self::CONFIG_DEFAULT, self::DEFAULT_LOCALE); |
78 | $this->allLocales = $config->getCommaSeparatedValues(self::CONFIG_ALL); |
79 | $this->hasMultiLocales = count($this->allLocales) > 1; |
80 | } |
81 | |
82 | /** |
83 | * Adds a folder path for a namespace |
84 | * @param string $namespace The name of the namespace |
85 | * @param string $folder The folder path for the namespace, it can contain a ~ symbol for the `app.root_path` |
86 | */ |
87 | public function add(string $namespace, string $folder): void { |
88 | $this->data[$namespace] = null; |
89 | $this->folders[$namespace] = $folder; |
90 | } |
91 | |
92 | /** |
93 | * Returns with all of the known locales |
94 | * @return array |
95 | */ |
96 | public function allLocales(): array { |
97 | return $this->allLocales; |
98 | } |
99 | |
100 | /** |
101 | * Does the application has multi locales? |
102 | * @return bool True if the application has multiple known locales |
103 | */ |
104 | public function hasMultiLocales(): bool { |
105 | return $this->hasMultiLocales; |
106 | } |
107 | |
108 | /** |
109 | * Returns with the current locale |
110 | * @return string The current locale |
111 | */ |
112 | public function locale(): string { |
113 | return $this->locale; |
114 | } |
115 | |
116 | /** |
117 | * Sets the current locale |
118 | * @param string $locale The current locale |
119 | */ |
120 | public function setLocale(string $locale): void { |
121 | $this->locale = $locale; |
122 | } |
123 | |
124 | /** |
125 | * Returns with the text by namespace and text id for the current locale |
126 | * |
127 | * For translation you have to have multi locale config within your config.ini.php, for example: |
128 | * |
129 | * <pre> |
130 | * translation.all = en, hu |
131 | * translation.default = en |
132 | * </pre> |
133 | * |
134 | * then you have to add at least one namespace with a folder path for example in your App::init() method: |
135 | * |
136 | * <pre> |
137 | * class MyApp extends App { |
138 | * // ... |
139 | * public function init() { |
140 | * $translation = $this->get(Translation::class); |
141 | * $translation->addFolder('test', '~/folder/within/the/app/root/folder'); |
142 | * } |
143 | * // ... |
144 | * } |
145 | * </pre> |
146 | * |
147 | * In the given folder you have to have the files `en.ini` and `hu.ini`. Both of the files have to have the |
148 | * text IDs and the translations. The `en.ini` could look like: |
149 | * |
150 | * <pre> |
151 | * welcome = "Hello {name}!" |
152 | * </pre> |
153 | * |
154 | * and then you can use it in your code: |
155 | * |
156 | * <pre> |
157 | * echo $translation->get('test:welcome', ['name' => 'Joe']); |
158 | * </pre> |
159 | * |
160 | * or in your view with the `tr` helper function: |
161 | * |
162 | * <pre> |
163 | * <?= tr('test:welcome', ['name' => 'Joe']); ?> |
164 | * </pre> |
165 | * |
166 | * the result will be with 'en' current locale: |
167 | * |
168 | * <pre> |
169 | * Hello Joe! |
170 | * </pre> |
171 | * |
172 | * If the translation doesn't exists, the result will be the `$id` between # symbols: |
173 | * |
174 | * <pre> |
175 | * #test:welcome# |
176 | * </pre> |
177 | * |
178 | * @param string $id The id of the translated text in 'namespace:text_id' format |
179 | * @param array $params The parameters for the variables in the text in ['name' => 'value'] format |
180 | * @return string The translated text with replaced variables |
181 | */ |
182 | public function get(string $id, array $params = []): string { |
183 | $dotPos = strpos($id, ':'); |
184 | $namespace = substr($id, 0, $dotPos); |
185 | $name = substr($id, $dotPos + 1); |
186 | $result = '#'.$id.'#'; |
187 | if (!isset($this->folders[$namespace])) { |
188 | return $result; |
189 | } |
190 | if (!isset($this->data[$namespace])) { |
191 | $path = $this->config->getFullPath($this->folders[$namespace].'/'.$this->locale.'.ini'); |
192 | $iniData = file_exists($path) ? parse_ini_file($path) : []; |
193 | $this->data[$namespace] = $iniData; |
194 | } |
195 | if (isset($this->data[$namespace][$name])) { |
196 | $result = $this->data[$namespace][$name]; |
197 | } |
198 | foreach ($params as $name => $value) { |
199 | $result = str_replace('{' . $name . '}', $value, $result); |
200 | } |
201 | return $result; |
202 | } |
203 | |
204 | } |