1 | <?php |
||
27 | class FileTranslator implements TranslatorInterface, EditTranslationInterface { |
||
28 | use EditTranslationHelperTrait; |
||
29 | |||
30 | /** |
||
31 | * Message list |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $msg = null; |
||
36 | |||
37 | /** |
||
38 | * The path to the directory storing the translations. |
||
39 | * <p>The directory path should end with a "/".</p> |
||
40 | * <p>If the path start with / or c:/ is the real path of file, otherwise, this must be start without / to root path of application.</p> |
||
41 | * <p>Each file in this directory is a PHP file containing an array variable named $msg. The key is the code or message id, the value is translation.<br/> |
||
42 | * Example : |
||
43 | * </p> |
||
44 | * <pre class="brush:php">$msg["home.title"] = "Hello world";<br /> |
||
45 | * $msg["home.text"] = "News 1, news 2 and news 3";</pre> |
||
46 | * |
||
47 | * |
||
48 | * @Property |
||
49 | * @var string |
||
50 | */ |
||
51 | private $i18nMessagePath = "resources/"; |
||
52 | |||
53 | /** |
||
54 | * Set the language detection |
||
55 | * |
||
56 | * @Property |
||
57 | * @var LanguageDetectionInterface |
||
58 | */ |
||
59 | private $languageDetection; |
||
60 | |||
61 | /** |
||
62 | * Store the instance of language |
||
63 | * @var array<MessageFileLanguage |
||
64 | */ |
||
65 | private $messageFile = []; |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * @param string $i18nMessagePath The path to the directory storing the translations. <p>The directory path should end with a "/".</p><p>If the path start with / or c:/ is the real path of file, otherwise, this must be start without / to root path of application.</p><p>By default this is resources/</p> |
||
70 | * @param LanguageDetectionInterface $languageDetection LanguageDetectionInterface |
||
71 | */ |
||
72 | public function __construct($i18nMessagePath = "resources/", LanguageDetectionInterface $languageDetection) { |
||
76 | |||
77 | /** |
||
78 | * This function return the real path set in parameter. |
||
79 | * @return string |
||
80 | */ |
||
81 | private function getPath() { |
||
87 | |||
88 | /** |
||
89 | * Return the instance of the MessageFileLanguage |
||
90 | * Each MessageFileLanguage is link to one language |
||
91 | * |
||
92 | * @param string $language |
||
93 | * @return MessageFileLanguage |
||
94 | */ |
||
95 | private function getMessageFile($language) { |
||
101 | |||
102 | /** |
||
103 | * Retrieve the translation of code or message. |
||
104 | * Check in the $msg variable if the key exist to return the value. This function check all the custom file if the translation is not in the main message_[language].php |
||
105 | * If this message doesn't exist, it return a link to edit it. |
||
106 | * |
||
107 | */ |
||
108 | public function getTranslation($message, array $parameters = [], LanguageDetectionInterface $languageDetectionInterface = null) { |
||
132 | |||
133 | /** |
||
134 | * Retrieve array variable store in the language file. |
||
135 | * This function include the message resource by default and the language file if the language code is set. |
||
136 | * The file contain an array with translation, we retrieve it in a private array msg. |
||
137 | * |
||
138 | * @param string $language Language code |
||
139 | * @return boolean |
||
140 | */ |
||
141 | private function retrieveMessages($language) { |
||
149 | |||
150 | |||
151 | /***************************/ |
||
152 | /****** Edition mode *******/ |
||
153 | /***************************/ |
||
154 | |||
155 | /** |
||
156 | * The list of all messages in all languages |
||
157 | * @var array<string, FineMessageLanguage> |
||
158 | */ |
||
159 | private $messages = array(); |
||
160 | |||
161 | public function getAllTranslationByLanguage() { |
||
168 | |||
169 | /** |
||
170 | * Return a list of all message for a language. |
||
171 | * |
||
172 | * @param string $language Language |
||
173 | * @return array<string, string> List with key value of translation |
||
|
|||
174 | */ |
||
175 | public function getTranslationsForLanguage($language) { |
||
182 | |||
183 | /** |
||
184 | * Return a list of all message for a key, by language. |
||
185 | * |
||
186 | * @param string $key Key of translation |
||
187 | * @return array<string, string> List with key value of translation |
||
188 | */ |
||
189 | public function getTranslationsForKey($key) { |
||
201 | |||
202 | /** |
||
203 | * Delete a translation for a language. If the language is not set or null, this function deletes the translation for all language. |
||
204 | * |
||
205 | * @param string $key Key to remove |
||
206 | * @param string|null $language Language to remove key or null for all |
||
207 | */ |
||
208 | public function deleteTranslation($key, $language = null) { |
||
223 | |||
224 | /** |
||
225 | * Add or change a translation |
||
226 | * |
||
227 | * @param string $key Key of translation |
||
228 | * @param string $value Message of translation |
||
229 | * @param string $language Language to add translation |
||
230 | */ |
||
231 | public function setTranslation($key, $value, $language) { |
||
238 | |||
239 | /** |
||
240 | * Add or change many translations in one time |
||
241 | * |
||
242 | * @param array<string, string> $messages List with key value of translation |
||
243 | * @param string $language Language to add translation |
||
244 | */ |
||
245 | public function setTranslationsForLanguage(array $messages, $language) { |
||
252 | |||
253 | /** |
||
254 | * Add or change many translations in one time |
||
255 | * |
||
256 | * @param array<string, string> $messages List with key value of translation |
||
257 | * @param string $key Key to add translation |
||
258 | */ |
||
259 | public function setTranslationsForKey(array $messages, $key) { |
||
266 | |||
267 | /** |
||
268 | * Liste of all language supported |
||
269 | * |
||
270 | * @return array<string> |
||
271 | */ |
||
272 | public function getLanguageList() { |
||
283 | } |
||
284 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.