‪TYPO3CMS  11.5
LanguageServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
23 
24 class ‪LanguageServiceTest extends FunctionalTestCase
25 {
27  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_localization',
28  ];
29 
33  protected ‪$initializeDatabase = false;
34 
36  'SYS' => [
37  'caching' => [
38  'cacheConfigurations' => [
39  'l10n' => [
40  'backend' => NullBackend::class,
41  ],
42  ],
43  ],
44  'locallangXMLOverride' => [],
45  ],
46  ];
47 
48  // Constants to access the various language files
49  private const ‪LANGUAGE_FILE = 'EXT:test_localization/Resources/Private/Language/locallang.xlf';
50  private const ‪LANGUAGE_FILE_OVERRIDE = 'EXT:test_localization/Resources/Private/Language/locallang_override.xlf';
51  private const ‪LANGUAGE_FILE_OVERRIDE_DE = 'EXT:test_localization/Resources/Private/Language/de.locallang_override.xlf';
52  private const ‪LANGUAGE_FILE_OVERRIDE_FR = 'EXT:test_localization/Resources/Private/Language/fr.locallang_override.xlf';
53  private const ‪LANGUAGE_FILE_CORE = 'EXT:core/Resources/Private/Language/locallang_common.xlf';
54  private const ‪LANGUAGE_FILE_CORE_OVERRIDE = 'EXT:test_localization/Resources/Private/Language/locallang_common_override.xlf';
55  private const ‪LANGUAGE_FILE_CORE_OVERRIDE_FR = 'EXT:test_localization/Resources/Private/Language/fr.locallang_common_override.xlf';
56 
61  public function ‪splitLabelTest(string $input, string $expected): void
62  {
63  $subject = $this->get(LanguageServiceFactory::class)->create('default');
64  self::assertEquals($expected, $subject->sL($input));
65  }
66 
67  public function ‪splitLabelTestDataProvider(): \Generator
68  {
69  yield 'String without whitespace' => [
70  'Edit content',
71  'Edit content',
72  ];
73  yield 'String with leading whitespace' => [
74  ' Edit content',
75  ' Edit content',
76  ];
77  yield 'String with trailing whitespace' => [
78  'Edit content ',
79  'Edit content ',
80  ];
81  yield 'String with outer whitespace' => [
82  ' Edit content ',
83  ' Edit content ',
84  ];
85  yield 'String with inner whitespace' => [
86  'Edit content',
87  'Edit content',
88  ];
89  yield 'String with inner and outer whitespace' => [
90  ' Edit content ',
91  ' Edit content ',
92  ];
93  yield 'String containing the LLL: key' => [
94  'You can use LLL: to ...',
95  'You can use LLL: to ...',
96  ];
97  yield 'String starting with the LLL: key' => [
98  'LLL: can be used to ...',
99  '', // @todo Should this special case be handled to return the input string?
100  ];
101  yield 'Locallang label without whitespace' => [
102  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editcontent',
103  'Edit content',
104  ];
105  yield 'Locallang label with leading whitespace' => [
106  ' LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editcontent',
107  'Edit content',
108  ];
109  yield 'Locallang label with trailing whitespace' => [
110  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editcontent ',
111  'Edit content',
112  ];
113  yield 'Locallang label with outer whitespace' => [
114  ' LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editcontent ',
115  'Edit content',
116  ];
117  yield 'Locallang label with inner whitespace' => [
118  'LLL: EXT: core/Resources/Private/Language/locallang_core.xlf:cm.editcontent',
119  'Edit content',
120  ];
121  yield 'Locallang label with inner and outer whitespace' => [
122  ' LLL: EXT: core/Resources/Private/Language/locallang_core.xlf:cm.editcontent ',
123  'Edit content',
124  ];
125  }
126 
127  private function ‪ensureLocalizationScenarioWorks(string $locale, string $languageFile, array $expectedLabels): void
128  {
129  $subject = $this->get(LanguageServiceFactory::class)->create($locale);
130 
131  foreach ($expectedLabels as $label => $expectedLabel) {
132  self::assertEquals($expectedLabel, $subject->sL(sprintf('LLL:%s:%s', $languageFile, $label)));
133  }
134  }
135 
140  public function ‪ensureVariousLocalizationScenariosWork(string $locale, array $expectedLabels): void
141  {
142  $this->‪ensureLocalizationScenarioWorks($locale, self::LANGUAGE_FILE, $expectedLabels);
143  }
144 
145  public function ‪ensureVariousLocalizationScenariosWorkDataProvider(): \Generator
146  {
147  yield 'Can handle localization for native language' => [
148  'locale' => 'default',
149  'expectedLabels' => [
150  'label1' => 'This is label #1',
151  'label2' => 'This is label #2',
152  'label3' => 'This is label #3',
153  ],
154  ];
155  yield 'Can handle localization for available translation' => [
156  'locale' => 'fr',
157  'expectedLabels' => [
158  'label1' => 'Ceci est le libellé no. 1',
159  'label2' => 'Ceci est le libellé no. 2',
160  'label3' => 'Ceci est le libellé no. 3',
161  ],
162  ];
163  yield 'Can handle localization for missing translation' => [
164  'locale' => 'de',
165  'expectedLabels' => [
166  'label1' => 'This is label #1',
167  'label2' => 'This is label #2',
168  'label3' => 'This is label #3',
169  ],
170  ];
171  }
172 
177  public function ‪ensureVariousLocalizationOverrideScenariosWork(string $locale, array $expectedLabels): void
178  {
179  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][‪self::LANGUAGE_FILE][] = ‪self::LANGUAGE_FILE_OVERRIDE;
180  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['de'][‪self::LANGUAGE_FILE][] = ‪self::LANGUAGE_FILE_OVERRIDE_DE;
181  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][‪self::LANGUAGE_FILE][] = ‪self::LANGUAGE_FILE_OVERRIDE_FR;
182 
183  $this->‪ensureLocalizationScenarioWorks($locale, self::LANGUAGE_FILE, $expectedLabels);
184  }
185 
187  {
188  yield 'Can override localization for native translation' => [
189  'locale' => 'default',
190  'expectedLabels' => [
191  'label1' => 'This is my 1st label',
192  'label2' => 'This is my 2nd label',
193  'label3' => 'This is label #3',
194  ],
195  ];
196  yield 'Can override localization for existing translation' => [
197  'locale' => 'fr',
198  'expectedLabels' => [
199  'label1' => 'Ceci est mon 1er libellé',
200  'label2' => 'Ceci est le libellé no. 2',
201  'label3' => 'Ceci est mon 3e libellé',
202  ],
203  ];
204  yield 'Can override localization for missing translation' => [
205  'locale' => 'de',
206  'expectedLabels' => [
207  'label1' => 'Das ist Beschriftung 1',
208  'label2' => 'Das ist Beschriftung 2',
209  'label3' => 'Das ist Beschriftung 3',
210  ],
211  ];
212  }
213 
218  public function ‪ensureVariousLocalizationOverrideScenariosForCoreExtensionWork(string $locale, array $expectedLabels): void
219  {
220  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][‪self::LANGUAGE_FILE_CORE][] = ‪self::LANGUAGE_FILE_CORE_OVERRIDE;
221  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][‪self::LANGUAGE_FILE_CORE][] = ‪self::LANGUAGE_FILE_CORE_OVERRIDE_FR;
222 
223  $this->‪ensureLocalizationScenarioWorks($locale, self::LANGUAGE_FILE_CORE, $expectedLabels);
224  }
225 
227  {
228  yield 'Can override localization of core for native locale' => [
229  'locale' => 'default',
230  'expectedLabels' => [
231  'about' => 'Overriden About',
232  'help' => 'Overriden Help',
233  'ok' => 'Overriden OK',
234  ],
235  ];
236  yield 'Can override localization of core for foreign locale' => [
237  'locale' => 'fr',
238  'expectedLabels' => [
239  'about' => 'A propos',
240  'help' => 'Aide',
241  'ok' => 'OK',
242  ],
243  ];
244  }
245 
250  public function ‪ensureMultiLanguageTranslationInSameContextWork(array $expectedLabelSet): void
251  {
252  foreach ($expectedLabelSet as $locale => $expectedLabels) {
253  $this->‪ensureLocalizationScenarioWorks($locale, self::LANGUAGE_FILE, $expectedLabels);
254  }
255  }
256 
258  {
259  yield 'Multi language translation in same context works with default first' => [
260  'expectedLabelSet' => [
261  'default' => [
262  'label1' => 'This is label #1',
263  'label2' => 'This is label #2',
264  'label3' => 'This is label #3',
265  ],
266  'fr' => [
267  'label1' => 'Ceci est le libellé no. 1',
268  'label2' => 'Ceci est le libellé no. 2',
269  'label3' => 'Ceci est le libellé no. 3',
270  ],
271  'de' => [
272  'label1' => 'This is label #1',
273  'label2' => 'This is label #2',
274  'label3' => 'This is label #3',
275  ],
276  ],
277  ];
278  yield 'Multi language translation in same context works with default last' => [
279  'expectedLabelSet' => [
280  'fr' => [
281  'label1' => 'Ceci est le libellé no. 1',
282  'label2' => 'Ceci est le libellé no. 2',
283  'label3' => 'Ceci est le libellé no. 3',
284  ],
285  'de' => [
286  'label1' => 'This is label #1',
287  'label2' => 'This is label #2',
288  'label3' => 'This is label #3',
289  ],
290  'default' => [
291  'label1' => 'This is label #1',
292  'label2' => 'This is label #2',
293  'label3' => 'This is label #3',
294  ],
295  ],
296  ];
297  }
298 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\splitLabelTestDataProvider
‪splitLabelTestDataProvider()
Definition: LanguageServiceTest.php:66
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\LANGUAGE_FILE_CORE_OVERRIDE_FR
‪const LANGUAGE_FILE_CORE_OVERRIDE_FR
Definition: LanguageServiceTest.php:54
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\$configurationToUseInTestInstance
‪$configurationToUseInTestInstance
Definition: LanguageServiceTest.php:34
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\$initializeDatabase
‪bool $initializeDatabase
Definition: LanguageServiceTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest
Definition: LanguageServiceTest.php:25
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\LANGUAGE_FILE_OVERRIDE
‪const LANGUAGE_FILE_OVERRIDE
Definition: LanguageServiceTest.php:49
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureVariousLocalizationOverrideScenariosForCoreExtensionWork
‪ensureVariousLocalizationOverrideScenariosForCoreExtensionWork(string $locale, array $expectedLabels)
Definition: LanguageServiceTest.php:217
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\LANGUAGE_FILE_CORE
‪const LANGUAGE_FILE_CORE
Definition: LanguageServiceTest.php:52
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\$testExtensionsToLoad
‪$testExtensionsToLoad
Definition: LanguageServiceTest.php:26
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureMultiLanguageTranslationInSameContextWork
‪ensureMultiLanguageTranslationInSameContextWork(array $expectedLabelSet)
Definition: LanguageServiceTest.php:249
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureVariousLocalizationOverrideScenariosWorkDataProvider
‪ensureVariousLocalizationOverrideScenariosWorkDataProvider()
Definition: LanguageServiceTest.php:185
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureVariousLocalizationScenariosWorkDataProvider
‪ensureVariousLocalizationScenariosWorkDataProvider()
Definition: LanguageServiceTest.php:144
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureVariousLocalizationOverrideScenariosWork
‪ensureVariousLocalizationOverrideScenariosWork(string $locale, array $expectedLabels)
Definition: LanguageServiceTest.php:176
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\LANGUAGE_FILE_CORE_OVERRIDE
‪const LANGUAGE_FILE_CORE_OVERRIDE
Definition: LanguageServiceTest.php:53
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureLocalizationScenarioWorks
‪ensureLocalizationScenarioWorks(string $locale, string $languageFile, array $expectedLabels)
Definition: LanguageServiceTest.php:126
‪TYPO3\CMS\Core\Cache\Backend\NullBackend
Definition: NullBackend.php:22
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureVariousLocalizationScenariosWork
‪ensureVariousLocalizationScenariosWork(string $locale, array $expectedLabels)
Definition: LanguageServiceTest.php:139
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\LANGUAGE_FILE
‪const LANGUAGE_FILE
Definition: LanguageServiceTest.php:48
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\splitLabelTest
‪splitLabelTest(string $input, string $expected)
Definition: LanguageServiceTest.php:60
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Functional\Localization
Definition: LanguageServiceTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\LANGUAGE_FILE_OVERRIDE_DE
‪const LANGUAGE_FILE_OVERRIDE_DE
Definition: LanguageServiceTest.php:50
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\LANGUAGE_FILE_OVERRIDE_FR
‪const LANGUAGE_FILE_OVERRIDE_FR
Definition: LanguageServiceTest.php:51
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureMultiLanguageTranslationInSameContextWorkDataProvider
‪ensureMultiLanguageTranslationInSameContextWorkDataProvider()
Definition: LanguageServiceTest.php:256
‪TYPO3\CMS\Core\Tests\Functional\Localization\LanguageServiceTest\ensureVariousLocalizationOverrideScenariosForCoreExtensionWorkDataProvider
‪ensureVariousLocalizationOverrideScenariosForCoreExtensionWorkDataProvider()
Definition: LanguageServiceTest.php:225