17 use Prophecy\Argument;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
53 protected function setUp()
57 $this->languageFilePath => [
61 'source' =>
'English label for key1',
62 'target' =>
'English label for key1',
67 'source' =>
'English label for key2',
68 'target' =>
'English label for key2',
73 'source' =>
'English label for key3',
74 'target' =>
'English label for key3',
79 'source' =>
'English label for key4',
80 'target' =>
'English label for key4',
83 'keyWithPlaceholder' => [
85 'source' =>
'English label with number %d',
86 'target' =>
'English label with number %d',
93 'source' =>
'English label for key1',
94 'target' =>
'Dansk label for key1',
100 'source' =>
'English label for key2',
105 'source' =>
'English label for key3',
111 'source' =>
'English label for key4',
118 'source' =>
'English label for key5',
122 'keyWithPlaceholder' => [
124 'source' =>
'English label with number %d',
132 'source' =>
'English label for key1',
137 'source' =>
'English label for key2',
138 'target' =>
'Dansk alternative label for key2',
143 'source' =>
'English label for key3',
149 'source' =>
'English label for key4',
155 'source' =>
'English label for key5',
156 'target' =>
'Dansk alternative label for key5',
159 'keyWithPlaceholder' => [
161 'source' =>
'English label with number %d',
169 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
171 $this->configurationManagerInterfaceProphecy = $this->prophesize(ConfigurationManagerInterface::class);
172 $property = $reflectionClass->getProperty(
'configurationManager');
173 $property->setAccessible(
true);
174 $property->setValue($this->configurationManagerInterfaceProphecy->reveal());
176 $localizationFactoryProphecy = $this->prophesize(LocalizationFactory::class);
177 GeneralUtility::setSingletonInstance(LocalizationFactory::class, $localizationFactoryProphecy->reveal());
178 $localizationFactoryProphecy->getParsedData(Argument::cetera(),
'foo')->willReturn([]);
186 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
188 $property = $reflectionClass->getProperty(
'configurationManager');
189 $property->setAccessible(
true);
190 $property->setValue(
null);
192 $property = $reflectionClass->getProperty(
'LOCAL_LANG');
193 $property->setAccessible(
true);
194 $property->setValue([]);
196 GeneralUtility::purgeInstances();
207 return 'EXT:' . $extensionName .
'/Resources/Private/Language/locallang.xlf';
215 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
216 $method = $reflectionClass->getMethod(
'flattenTypoScriptLabelArray');
217 $method->setAccessible(
true);
223 'key3.subkey1' =>
'subvalue1',
224 'key3.subkey2.subsubkey' =>
'val'
230 '_typoScriptNodeValue' =>
'value3',
231 'subkey1' =>
'subvalue1',
237 $result = $method->invoke(
null, $input);
238 $this->assertEquals($expected, $result);
263 'get translated key' =>
264 [
'key1',
'dk',
'Dansk label for key1'],
266 'fallback to English when translation is missing for key' =>
267 [
'key2',
'dk',
'English label for key2'],
269 'fallback to English for non existing language' =>
270 [
'key2',
'xx',
'English label for key2'],
272 'replace placeholder with argument' =>
273 [
'keyWithPlaceholder',
'default',
'English label with number 100', [], [100]],
275 'get translated key from primary language' =>
276 [
'key1',
'dk',
'Dansk label for key1', [
'dk_alt']],
278 'fallback to alternative language if translation is missing(llxml)' =>
279 [
'key2',
'dk',
'Dansk alternative label for key2', [
'dk_alt']],
281 'fallback to alternative language if translation is missing(xlif)' =>
282 [
'key5',
'dk',
'Dansk alternative label for key5', [
'dk_alt']],
284 'fallback to English for label not translated in dk and dk_alt(llxml)' =>
285 [
'key3',
'dk',
'English label for key3', [
'dk_alt']],
287 'fallback to English for label not translated in dk and dk_alt(xlif)' =>
288 [
'key4',
'dk',
'English label for key4', [
'dk_alt']],
303 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
305 $property = $reflectionClass->getProperty(
'LOCAL_LANG');
306 $property->setAccessible(
true);
307 $property->setValue($this->LOCAL_LANG);
309 $backendUserAuthenticationProphecy = $this->prophesize(BackendUserAuthentication::class);
310 $GLOBALS[
'BE_USER'] = $backendUserAuthenticationProphecy->reveal();
311 $backendUserAuthenticationProphecy->uc = [
312 'lang' => $languageKey,
330 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
332 $property = $reflectionClass->getProperty(
'LOCAL_LANG');
333 $property->setAccessible(
true);
334 $property->setValue($this->LOCAL_LANG);
345 'override labels with typoscript' => [
351 'source' =>
'English label for key1',
352 'target' =>
'Dansk label for key1 core',
357 'source' =>
'English label for key2',
362 'source' =>
'English label for key3',
371 'source' =>
'English label for key1',
372 'target' =>
'Dansk label for key1 backend',
377 'source' =>
'English label for key2',
382 'source' =>
'English label for key3',
388 'typoscript LOCAL_LANG' => [
391 'key1' =>
'key1 value from TS core',
393 'subkey1' =>
'key3.subkey1 value from TS core',
396 'subsubkey' =>
'key3.subkey2.subsubkey value from TS core'
402 'language key' =>
'dk',
406 'source' =>
'English label for key1',
407 'target' =>
'key1 value from TS core',
412 'source' =>
'English label for key2',
417 'source' =>
'English label for key3',
418 'target' =>
'key3.subkey1 value from TS core',
421 'key3.subkey2.subsubkey' => [
423 'target' =>
'key3.subkey2.subsubkey value from TS core',
443 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
445 $property = $reflectionClass->getProperty(
'LOCAL_LANG');
446 $property->setAccessible(
true);
450 $this->configurationManagerInterfaceProphecy
451 ->getConfiguration($configurationType,
'core',
null)
453 ->willReturn($typoScriptLocalLang);
455 $method = $reflectionClass->getMethod(
'loadTypoScriptLabels');
456 $method->setAccessible(
true);
457 $method->invoke(
null,
'core', $this->languageFilePath);
459 $property = $reflectionClass->getProperty(
'LOCAL_LANG');
460 $property->setAccessible(
true);
461 $result = $property->getValue();
463 $this->assertEquals($expected, $result[$this->languageFilePath][$languageKey]);
471 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
473 $property = $reflectionClass->getProperty(
'LOCAL_LANG');
474 $property->setAccessible(
true);
475 $property->setValue($this->LOCAL_LANG);
477 $typoScriptLocalLang = [
486 $this->configurationManagerInterfaceProphecy
487 ->getConfiguration($configurationType,
'core',
null)
489 ->willReturn($typoScriptLocalLang);
491 $method = $reflectionClass->getMethod(
'loadTypoScriptLabels');
492 $method->setAccessible(
true);
493 $method->invoke(
null,
'core', $this->languageFilePath);
498 $this->assertNotNull($result);
499 $this->assertEquals(
'', $result);
507 $this->expectException(\InvalidArgumentException::class);
508 $this->expectExceptionCode(1498144052);
517 $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
519 $typoScriptLocalLang = [
522 'key1' =>
'I am a new key and there is no xlf file',
528 $this->configurationManagerInterfaceProphecy
529 ->getConfiguration($configurationType,
'core',
null)
531 ->willReturn($typoScriptLocalLang);
533 $method = $reflectionClass->getMethod(
'loadTypoScriptLabels');
534 $method->setAccessible(
true);
535 $method->invoke(
null,
'core',
'');
540 $this->assertNotNull($result);
541 $this->assertEquals(
'I am a new key and there is no xlf file', $result);