Skip to content

Commit 3e06557

Browse files
authored
Merge pull request #9109 from kenjis/docs-localization.rst
docs: improve Localization
2 parents a151d6a + 2835cc4 commit 3e06557

File tree

6 files changed

+166
-67
lines changed

6 files changed

+166
-67
lines changed

phpstan-baseline.php

-24
Original file line numberDiff line numberDiff line change
@@ -15727,30 +15727,6 @@
1572715727
'count' => 2,
1572815728
'path' => __DIR__ . '/tests/system/Images/ImageMagickHandlerTest.php',
1572915729
];
15730-
$ignoreErrors[] = [
15731-
// identifier: method.notFound
15732-
'message' => '#^Call to an undefined method CodeIgniter\\\\Language\\\\Language\\:\\:disableIntlSupport\\(\\)\\.$#',
15733-
'count' => 1,
15734-
'path' => __DIR__ . '/tests/system/Language/LanguageTest.php',
15735-
];
15736-
$ignoreErrors[] = [
15737-
// identifier: method.notFound
15738-
'message' => '#^Call to an undefined method CodeIgniter\\\\Language\\\\Language\\:\\:loaded\\(\\)\\.$#',
15739-
'count' => 3,
15740-
'path' => __DIR__ . '/tests/system/Language/LanguageTest.php',
15741-
];
15742-
$ignoreErrors[] = [
15743-
// identifier: method.notFound
15744-
'message' => '#^Call to an undefined method CodeIgniter\\\\Language\\\\Language\\:\\:loadem\\(\\)\\.$#',
15745-
'count' => 2,
15746-
'path' => __DIR__ . '/tests/system/Language/LanguageTest.php',
15747-
];
15748-
$ignoreErrors[] = [
15749-
// identifier: method.notFound
15750-
'message' => '#^Call to an undefined method CodeIgniter\\\\Language\\\\Language\\:\\:setData\\(\\)\\.$#',
15751-
'count' => 9,
15752-
'path' => __DIR__ . '/tests/system/Language/LanguageTest.php',
15753-
];
1575415730
$ignoreErrors[] = [
1575515731
// identifier: missingType.iterableValue
1575615732
'message' => '#^Method CodeIgniter\\\\Language\\\\LanguageTest\\:\\:provideBundleUniqueKeys\\(\\) return type has no value type specified in iterable type iterable\\.$#',

system/Common.php

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use CodeIgniter\HTTP\RedirectResponse;
2929
use CodeIgniter\HTTP\RequestInterface;
3030
use CodeIgniter\HTTP\ResponseInterface;
31+
use CodeIgniter\Language\Language;
3132
use CodeIgniter\Model;
3233
use CodeIgniter\Session\Session;
3334
use CodeIgniter\Test\TestLogger;
@@ -732,6 +733,7 @@ function is_windows(?bool $mock = null): bool
732733
*/
733734
function lang(string $line, array $args = [], ?string $locale = null)
734735
{
736+
/** @var Language $language */
735737
$language = service('language');
736738

737739
// Get active locale

tests/system/Language/LanguageTest.php

+89-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class LanguageTest extends CIUnitTestCase
3131

3232
protected function setUp(): void
3333
{
34-
$this->lang = new MockLanguage('en');
34+
$this->lang = new Language('en');
3535
}
3636

3737
public function testReturnsStringWithNoFileInMessage(): void
@@ -54,6 +54,8 @@ public function testReturnParsedStringWithNoFileInMessage(): void
5454

5555
public function testGetLineReturnsLine(): void
5656
{
57+
$this->lang = new MockLanguage('en');
58+
5759
$this->lang->setData('books', [
5860
'bookSaved' => 'We kept the book free from the boogeyman',
5961
'booksSaved' => 'We saved some more',
@@ -62,8 +64,67 @@ public function testGetLineReturnsLine(): void
6264
$this->assertSame('We saved some more', $this->lang->getLine('books.booksSaved'));
6365
}
6466

67+
public function testGetLineReturnsLineWithKeyWithDots(): void
68+
{
69+
$this->lang = new MockLanguage('en');
70+
71+
$this->lang->setData('books', [
72+
'bookSaved.foo' => 'We kept the book free from the boogeyman',
73+
'booksSaved.bar.baz' => 'We saved some more',
74+
]);
75+
76+
$this->assertSame(
77+
'We kept the book free from the boogeyman',
78+
$this->lang->getLine('books.bookSaved.foo')
79+
);
80+
$this->assertSame(
81+
'We saved some more',
82+
$this->lang->getLine('books.booksSaved.bar.baz')
83+
);
84+
}
85+
86+
public function testGetLineCannotUseKeysWithLeadingDot(): void
87+
{
88+
$this->lang = new MockLanguage('en');
89+
90+
$this->lang->setData('books', [
91+
'.bookSaved.foo.' => 'We kept the book free from the boogeyman',
92+
'.booksSaved.bar.baz.' => 'We saved some more',
93+
]);
94+
95+
$this->assertSame(
96+
'books.bookSaved.foo', // Can't get the message.
97+
$this->lang->getLine('books.bookSaved.foo')
98+
);
99+
$this->assertSame(
100+
'books.booksSaved.bar.baz', // Can't get the message.
101+
$this->lang->getLine('books.booksSaved.bar.baz')
102+
);
103+
}
104+
105+
public function testGetLineCannotUseKeysWithTrailingDot(): void
106+
{
107+
$this->lang = new MockLanguage('en');
108+
109+
$this->lang->setData('books', [
110+
'bookSaved.foo.' => 'We kept the book free from the boogeyman',
111+
'booksSaved.bar.baz.' => 'We saved some more',
112+
]);
113+
114+
$this->assertSame(
115+
'books.bookSaved.foo', // Can't get the message.
116+
$this->lang->getLine('books.bookSaved.foo')
117+
);
118+
$this->assertSame(
119+
'books.booksSaved.bar.baz', // Can't get the message.
120+
$this->lang->getLine('books.booksSaved.bar.baz')
121+
);
122+
}
123+
65124
public function testGetLineReturnsFallbackLine(): void
66125
{
126+
$this->lang = new MockLanguage('en');
127+
67128
$this->lang
68129
->setLocale('en-US')
69130
->setData('equivalent', [
@@ -86,6 +147,8 @@ public function testGetLineReturnsFallbackLine(): void
86147

87148
public function testGetLineArrayReturnsLineArray(): void
88149
{
150+
$this->lang = new MockLanguage('en');
151+
89152
$this->lang->setData('books', [
90153
'booksList' => [
91154
'The Boogeyman',
@@ -106,6 +169,8 @@ public function testGetLineFormatsMessage(): void
106169
$this->markTestSkipped('No intl support.');
107170
}
108171

172+
$this->lang = new MockLanguage('en');
173+
109174
$this->lang->setData('books', [
110175
'bookCount' => '{0, number, integer} books have been saved.',
111176
]);
@@ -120,6 +185,8 @@ public function testGetLineArrayFormatsMessages(): void
120185
$this->markTestSkipped('No intl support.');
121186
}
122187

188+
$this->lang = new MockLanguage('en');
189+
123190
$this->lang->setData('books', [
124191
'bookList' => [
125192
'{0, number, integer} related books.',
@@ -139,6 +206,8 @@ public function testGetLineInvalidFormatMessage(): void
139206
$this->markTestSkipped('No intl support.');
140207
}
141208

209+
$this->lang = new MockLanguage('en');
210+
142211
$this->lang->setLocale('ar');
143212

144213
$line = 'تم الكشف عن كلمة المرور {0} بسبب اختراق البيانات وشوهدت {1 ، عدد} مرة في {2} في كلمات المرور المخترقة.';
@@ -163,6 +232,8 @@ public function testLangAllowsOtherLocales(): void
163232

164233
public function testLangDoesntFormat(): void
165234
{
235+
$this->lang = new MockLanguage('en');
236+
166237
$this->lang->disableIntlSupport();
167238

168239
$this->lang->setData('books', [
@@ -185,40 +256,42 @@ public function testLanguageDuplicateKey(): void
185256

186257
public function testLanguageFileLoading(): void
187258
{
188-
$this->lang = new SecondMockLanguage('en');
259+
$lang = new SecondMockLanguage('en');
189260

190-
$this->lang->loadem('More', 'en');
191-
$this->assertContains('More', $this->lang->loaded());
261+
$lang->loadem('More', 'en');
262+
$this->assertContains('More', $lang->loaded());
192263

193-
$this->lang->loadem('More', 'en');
194-
$this->assertCount(1, $this->lang->loaded()); // should only be there once
264+
$lang->loadem('More', 'en');
265+
$this->assertCount(1, $lang->loaded()); // should only be there once
195266
}
196267

197268
public function testLanguageFileLoadingReturns(): void
198269
{
199-
$this->lang = new SecondMockLanguage('en');
270+
$lang = new SecondMockLanguage('en');
200271

201-
$result = $this->lang->loadem('More', 'en', true);
202-
$this->assertNotContains('More', $this->lang->loaded());
272+
$result = $lang->loadem('More', 'en', true);
273+
$this->assertNotContains('More', $lang->loaded());
203274
$this->assertCount(3, $result);
204275

205-
$this->lang->loadem('More', 'en');
206-
$this->assertContains('More', $this->lang->loaded());
207-
$this->assertCount(1, $this->lang->loaded());
276+
$lang->loadem('More', 'en');
277+
$this->assertContains('More', $lang->loaded());
278+
$this->assertCount(1, $lang->loaded());
208279
}
209280

210281
public function testLanguageSameKeyAndFileName(): void
211282
{
283+
$lang = new MockLanguage('en');
284+
212285
// first file data | example.message
213-
$this->lang->setData('example', ['message' => 'This is an example message']);
286+
$lang->setData('example', ['message' => 'This is an example message']);
214287

215288
// force loading data into file Example
216-
$this->assertSame('This is an example message', $this->lang->getLine('example.message'));
289+
$this->assertSame('This is an example message', $lang->getLine('example.message'));
217290

218291
// second file data | another.example
219-
$this->lang->setData('another', ['example' => 'Another example']);
292+
$lang->setData('another', ['example' => 'Another example']);
220293

221-
$this->assertSame('Another example', $this->lang->getLine('another.example'));
294+
$this->assertSame('Another example', $lang->getLine('another.example'));
222295
}
223296

224297
public function testGetLocale(): void

0 commit comments

Comments
 (0)