TYPO3 CMS  TYPO3_8-7
LocalizationUtilityTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
22 class LocalizationUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
30 
36  protected $LOCAL_LANG = [
37  'extensionKey' => [
38  'default' => [
39  'key1' => [
40  [
41  'source' => 'English label for key1',
42  'target' => 'English label for key1',
43  ]
44  ],
45  'key2' => [
46  [
47  'source' => 'English label for key2',
48  'target' => 'English label for key2',
49  ]
50  ],
51  'key3' => [
52  [
53  'source' => 'English label for key3',
54  'target' => 'English label for key3',
55  ]
56  ],
57  'key4' => [
58  [
59  'source' => 'English label for key4',
60  'target' => 'English label for key4',
61  ]
62  ],
63  'keyWithPlaceholder' => [
64  [
65  'source' => 'English label with number %d',
66  'target' => 'English label with number %d',
67  ]
68  ],
69  ],
70  'dk' => [
71  'key1' => [
72  [
73  'source' => 'English label for key1',
74  'target' => 'Dansk label for key1',
75  ]
76  ],
77  // not translated in dk => no target (llxml)
78  'key2' => [
79  [
80  'source' => 'English label for key2',
81  ]
82  ],
83  'key3' => [
84  [
85  'source' => 'English label for key3',
86  ]
87  ],
88  // not translated in dk => empty target (xliff)
89  'key4' => [
90  [
91  'source' => 'English label for key4',
92  'target' => '',
93  ]
94  ],
95  // not translated in dk => empty target (xliff)
96  'key5' => [
97  [
98  'source' => 'English label for key5',
99  'target' => '',
100  ]
101  ],
102  'keyWithPlaceholder' => [
103  [
104  'source' => 'English label with number %d',
105  ]
106  ],
107  ],
108  // fallback language for labels which are not translated in dk
109  'dk_alt' => [
110  'key1' => [
111  [
112  'source' => 'English label for key1',
113  ]
114  ],
115  'key2' => [
116  [
117  'source' => 'English label for key2',
118  'target' => 'Dansk alternative label for key2',
119  ]
120  ],
121  'key3' => [
122  [
123  'source' => 'English label for key3',
124  ]
125  ],
126  // not translated in dk_alt => empty target (xliff)
127  'key4' => [
128  [
129  'source' => 'English label for key4',
130  'target' => '',
131  ]
132  ],
133  'key5' => [
134  [
135  'source' => 'English label for key5',
136  'target' => 'Dansk alternative label for key5',
137  ]
138  ],
139  'keyWithPlaceholder' => [
140  [
141  'source' => 'English label with number %d',
142  ]
143  ],
144  ],
145 
146  ],
147  ];
148 
152  protected function setUp()
153  {
154  $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
155 
156  $this->configurationManager = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class, ['getConfiguration']);
157  $property = $reflectionClass->getProperty('configurationManager');
158  $property->setAccessible(true);
159  $property->setValue($this->configurationManager);
160  }
161 
165  protected function tearDown()
166  {
167  $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
168 
169  $property = $reflectionClass->getProperty('configurationManager');
170  $property->setAccessible(true);
171  $property->setValue(null);
172 
173  $property = $reflectionClass->getProperty('LOCAL_LANG');
174  $property->setAccessible(true);
175  $property->setValue([]);
176 
177  $property = $reflectionClass->getProperty('languageKey');
178  $property->setAccessible(true);
179  $property->setValue('default');
180 
181  $property = $reflectionClass->getProperty('alternativeLanguageKeys');
182  $property->setAccessible(true);
183  $property->setValue([]);
184 
185  parent::tearDown();
186  }
187 
192  {
193  $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
194  $method = $reflectionClass->getMethod('flattenTypoScriptLabelArray');
195  $method->setAccessible(true);
196 
197  $expected = [
198  'key1' => 'value1',
199  'key2' => 'value2',
200  'key3' => 'value3',
201  'key3.subkey1' => 'subvalue1',
202  'key3.subkey2.subsubkey' => 'val'
203  ];
204  $input = [
205  'key1' => 'value1',
206  'key2' => 'value2',
207  'key3' => [
208  '_typoScriptNodeValue' => 'value3',
209  'subkey1' => 'subvalue1',
210  'subkey2' => [
211  'subsubkey' => 'val'
212  ]
213  ]
214  ];
215  $result = $method->invoke(null, $input);
216  $this->assertEquals($expected, $result);
217  }
218 
223  {
224  $this->assertNull(LocalizationUtility::translate('', 'extbase'));
225  }
226 
231  {
232  $this->assertNull(LocalizationUtility::translate('', 'extbase', ['argument']));
233  }
234 
238  public function translateDataProvider()
239  {
240  return [
241  'get translated key' =>
242  ['key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1'],
243 
244  'fallback to English when translation is missing for key' =>
245  ['key2', $this->LOCAL_LANG, 'dk', 'English label for key2'],
246 
247  'fallback to English for non existing language' =>
248  ['key2', $this->LOCAL_LANG, 'xx', 'English label for key2'],
249 
250  'replace placeholder with argument' =>
251  ['keyWithPlaceholder', $this->LOCAL_LANG, 'en', 'English label with number 100', [], [100]],
252 
253  'get translated key from primary language' =>
254  ['key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1', ['dk_alt']],
255 
256  'fallback to alternative language if translation is missing(llxml)' =>
257  ['key2', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key2', ['dk_alt']],
258 
259  'fallback to alternative language if translation is missing(xlif)' =>
260  ['key5', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key5', ['dk_alt']],
261 
262  'fallback to English for label not translated in dk and dk_alt(llxml)' =>
263  ['key3', $this->LOCAL_LANG, 'dk', 'English label for key3', ['dk_alt']],
264 
265  'fallback to English for label not translated in dk and dk_alt(xlif)' =>
266  ['key4', $this->LOCAL_LANG, 'dk', 'English label for key4', ['dk_alt']],
267  ];
268  }
269 
280  public function translateTest($key, array $LOCAL_LANG, $languageKey, $expected, array $altLanguageKeys = [], array $arguments = null)
281  {
282  $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
283 
284  $property = $reflectionClass->getProperty('LOCAL_LANG');
285  $property->setAccessible(true);
286  $property->setValue($LOCAL_LANG);
287 
288  $property = $reflectionClass->getProperty('languageKey');
289  $property->setAccessible(true);
290  $property->setValue($languageKey);
291 
292  $property = $reflectionClass->getProperty('alternativeLanguageKeys');
293  $property->setAccessible(true);
294  $property->setValue($altLanguageKeys);
295 
296  $this->assertEquals($expected, LocalizationUtility::translate($key, 'extensionKey', $arguments));
297  }
298 
303  {
304  return [
305  'override labels with typoscript' => [
306  'LOCAL_LANG' => [
307  'extensionKey' => [
308  'dk' => [
309  'key1' => [
310  [
311  'source' => 'English label for key1',
312  'target' => 'Dansk label for key1 extensionKey',
313  ]
314  ],
315  'key2' => [
316  [
317  'source' => 'English label for key2',
318  ]
319  ],
320  'key3.subkey1' => [
321  [
322  'source' => 'English label for key3',
323  ]
324  ],
325  ],
326  ],
327  'extensionKey1' => [
328  'dk' => [
329  'key1' => [
330  [
331  'source' => 'English label for key1',
332  'target' => 'Dansk label for key1 extensionKey1',
333  ]
334  ],
335  'key2' => [
336  [
337  'source' => 'English label for key2',
338  ]
339  ],
340  'key3.subkey1' => [
341  [
342  'source' => 'English label for key3',
343  ]
344  ],
345  ],
346  ],
347  ],
348  'typoscript LOCAL_LANG' => [
349  '_LOCAL_LANG' => [
350  'dk' => [
351  'key1' => 'key1 value from TS extensionKey',
352  'key3' => [
353  'subkey1' => 'key3.subkey1 value from TS extensionKey',
354  // this key doesn't exist in xml files
355  'subkey2' => [
356  'subsubkey' => 'key3.subkey2.subsubkey value from TS extensionKey'
357  ]
358  ]
359  ]
360  ]
361  ],
362  'language key' => 'dk',
363  'expected' => [
364  'key1' => [
365  [
366  'source' => 'English label for key1',
367  'target' => 'key1 value from TS extensionKey',
368  ]
369  ],
370  'key2' => [
371  [
372  'source' => 'English label for key2',
373  ]
374  ],
375  'key3.subkey1' => [
376  [
377  'source' => 'English label for key3',
378  'target' => 'key3.subkey1 value from TS extensionKey',
379  ]
380  ],
381  'key3.subkey2.subsubkey' => [
382  [
383  'target' => 'key3.subkey2.subsubkey value from TS extensionKey',
384  ]
385  ],
386  ],
387  ]
388  ];
389  }
390 
401  public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoScriptLocalLang, $languageKey, array $expected)
402  {
403  $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
404 
405  $property = $reflectionClass->getProperty('LOCAL_LANG');
406  $property->setAccessible(true);
407  $property->setValue($LOCAL_LANG);
408 
409  $property = $reflectionClass->getProperty('languageKey');
410  $property->setAccessible(true);
411  $property->setValue($languageKey);
412 
414  $this->configurationManager->expects($this->at(0))->method('getConfiguration')->with($configurationType, 'extensionKey', null)->will($this->returnValue($typoScriptLocalLang));
415 
416  $method = $reflectionClass->getMethod('loadTypoScriptLabels');
417  $method->setAccessible(true);
418  $method->invoke(null, 'extensionKey');
419 
420  $property = $reflectionClass->getProperty('LOCAL_LANG');
421  $property->setAccessible(true);
422  $result = $property->getValue();
423 
424  $this->assertEquals($expected, $result['extensionKey'][$languageKey]);
425  }
426 
430  public function clearLabelWithTypoScript()
431  {
432  $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
433 
434  $property = $reflectionClass->getProperty('LOCAL_LANG');
435  $property->setAccessible(true);
436  $property->setValue($this->LOCAL_LANG);
437 
438  $property = $reflectionClass->getProperty('languageKey');
439  $property->setAccessible(true);
440  $property->setValue('dk');
441 
442  $typoScriptLocalLang = [
443  '_LOCAL_LANG' => [
444  'dk' => [
445  'key1' => '',
446  ]
447  ]
448  ];
449 
451  $this->configurationManager->expects($this->at(0))->method('getConfiguration')->with($configurationType, 'extensionKey', null)->will($this->returnValue($typoScriptLocalLang));
452 
453  $method = $reflectionClass->getMethod('loadTypoScriptLabels');
454  $method->setAccessible(true);
455  $method->invoke(null, 'extensionKey');
456 
457  $result = LocalizationUtility::translate('key1', 'extensionKey');
458  $this->assertNotNull($result);
459  $this->assertEquals('', $result);
460  }
461 
468  {
469  $result = LocalizationUtility::translate('LLL:EXT:extbase/Resources/Private/Language/locallang_db.xlf:fe_users.tx_extbase_type');
470  $this->assertNotEmpty($result);
471  }
472 
477  {
478  $this->expectException(\InvalidArgumentException::class);
479  $this->expectExceptionCode(1498144052);
480  LocalizationUtility::translate('foo/bar', '');
481  }
482 }
translateTest($key, array $LOCAL_LANG, $languageKey, $expected, array $altLanguageKeys=[], array $arguments=null)
static translate($key, $extensionName=null, $arguments=null)
loadTypoScriptLabels(array $LOCAL_LANG, array $typoScriptLocalLang, $languageKey, array $expected)