Skip to content

Commit 97cf139

Browse files
committed
- fixes the properties and methods scope
- adds more tests - CS chore
1 parent 6f004d8 commit 97cf139

7 files changed

Lines changed: 116 additions & 56 deletions

File tree

I18n.php

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use Koded\Stdlib\Config;
66
use Throwable;
7-
use function error_log;
87
use function ini_set;
98
use function strtr;
10-
use function substr_count;
119
use function vsprintf;
1210

1311
interface I18nFormatter
@@ -65,24 +63,24 @@ public static function translate(
6563
string $locale = null): string
6664
{
6765
try {
68-
return static::$catalogs[$locale]->translate('messages', $string, $arguments);
66+
return self::$catalogs[$locale]->translate('messages', $string, $arguments);
6967
} catch (Throwable) {
70-
static::registerCatalog($locale ??= static::locale());
71-
return static::$catalogs[$locale]->translate('messages', $string, $arguments);
68+
self::registerCatalog($locale ??= self::locale());
69+
return self::$catalogs[$locale]->translate('messages', $string, $arguments);
7270
}
7371
}
7472

7573
public static function locale(): string
7674
{
77-
return static::$locale ??= static::normalizeLocale(\Locale::getDefault());
75+
return self::$locale ??= I18nCatalog::normalizeLocale(\Locale::getDefault());
7876
}
7977

8078
/**
8179
* @return array{string, I18nCatalog}
8280
*/
8381
public static function catalogs(): array
8482
{
85-
return static::$catalogs;
83+
return self::$catalogs;
8684
}
8785

8886
/**
@@ -91,7 +89,7 @@ public static function catalogs(): array
9189
public static function info(): array
9290
{
9391
$catalogs = [];
94-
foreach (static::$catalogs as $locale => $instance) {
92+
foreach (self::$catalogs as $locale => $instance) {
9593
$catalogs[$locale] = [
9694
'class' => $instance::class,
9795
'formatter' => $instance->formatter()::class,
@@ -100,7 +98,7 @@ public static function info(): array
10098
];
10199
}
102100
return [
103-
'locale' => static::$locale,
101+
'locale' => self::$locale,
104102
'catalogs' => $catalogs,
105103
];
106104
}
@@ -110,52 +108,43 @@ public static function register(
110108
bool $asDefault = false): void
111109
{
112110
$locale = $catalog->locale();
113-
if ($asDefault || empty(static::$catalogs)) {
114-
static::setDefaultLocale($locale);
115-
static::$directory = $catalog->directory();
116-
static::$formatter = $catalog->formatter()::class;
117-
static::$catalog = $catalog::class;
111+
if ($asDefault || empty(self::$catalogs)) {
112+
self::setDefaultLocale($locale);
113+
self::$directory = $catalog->directory();
114+
self::$formatter = $catalog->formatter()::class;
115+
self::$catalog = $catalog::class;
118116
}
119-
static::$catalogs[$locale] = $catalog;
117+
self::$catalogs[$locale] = $catalog;
120118
}
121119

122120
public static function flush(): void
123121
{
124-
static::$catalogs = [];
125-
static::$directory = null;
126-
static::$formatter = null;
127-
static::$catalog = null;
128-
static::$locale = null;
122+
self::$catalogs = [];
123+
self::$directory = null;
124+
self::$formatter = null;
125+
self::$catalog = null;
126+
self::$locale = null;
129127
ini_set('intl.default_locale', '');
130128
\Locale::setDefault('');
131129
}
132130

133131
private static function registerCatalog(string $locale): void
134132
{
135-
if (isset(static::$catalogs[$locale])) {
133+
if (isset(self::$catalogs[$locale])) {
136134
return;
137135
}
138-
static::$catalogs[$locale] = I18nCatalog::new((new Config)
136+
self::$catalogs[$locale] = I18nCatalog::new((new Config)
139137
->set('translation.locale', $locale)
140-
->set('translation.dir', static::$directory)
141-
->set('translation.formatter', static::$formatter)
142-
->set('translation.catalog', static::$catalog)
138+
->set('translation.dir', self::$directory)
139+
->set('translation.formatter', self::$formatter)
140+
->set('translation.catalog', self::$catalog)
143141
);
144142
}
145143

146144
private static function setDefaultLocale(string $locale): void
147145
{
148-
static::$locale = $locale;
146+
self::$locale = $locale;
149147
ini_set('intl.default_locale', $locale);
150148
\Locale::setDefault($locale);
151149
}
152-
153-
private static function normalizeLocale(string $locale): string
154-
{
155-
if (substr_count($locale, '_') > 1) {
156-
$locale = explode('_', $locale);
157-
$locale = "$locale[0]_$locale[1]";
158-
}
159-
return $locale;
160-
}
161150
}

I18nCatalog.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,30 @@ public static function new(Configuration $conf): I18nCatalog
3131
$instance = new $catalog(
3232
new $formatter,
3333
$directory = $conf->get('translation.dir', getcwd() . '/locales'),
34-
$locale = $conf->get('translation.locale', I18n::DEFAULT_LOCALE)
34+
$locale = self::normalizeLocale($conf->get('translation.locale', I18n::DEFAULT_LOCALE))
3535
);
3636
if ($instance->supports($locale)) {
3737
return $instance;
3838
}
3939
if ($catalog !== ArrayCatalog::class) {
40-
error_log(" > ($locale) gettext not supported, try ArrayCatalog ...");
40+
error_log(" > ($locale) gettext not supported, trying ArrayCatalog ...");
4141
$conf->set('translation.catalog', ArrayCatalog::class);
4242
return static::new($conf);
4343
}
4444
// Last resort, passthru
4545
return new NoCatalog(new $formatter, $directory, $locale);
4646
}
4747

48+
public static function normalizeLocale(string $locale): string
49+
{
50+
$locale = str_replace('.', '_', $locale);
51+
if (substr_count($locale, '_') > 1) {
52+
$locale = explode('_', $locale);
53+
$locale = "$locale[0]_$locale[1]";
54+
}
55+
return $locale;
56+
}
57+
4858
public function translate(
4959
string $domain,
5060
string $key,
@@ -114,6 +124,7 @@ protected function supports(string $locale): bool
114124
{
115125
return true;
116126
}
127+
117128
// @codeCoverageIgnoreEnd
118129

119130
protected function initialize(string $locale): string|false
@@ -141,7 +152,7 @@ protected function initialize(string $locale): string|false
141152
try {
142153
$this->data = require($catalog = "$this->directory/$locale.php");
143154
if (false === array_key_exists('messages', $this->data)) {
144-
error_log("i18n catalog $catalog is missing the messages array");
155+
error_log("ERROR : i18n catalog $catalog is missing the messages array");
145156
return false;
146157
}
147158
return $locale;

functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
function __(
66
string $string,
7-
array $arguments = [],
7+
array $arguments = [],
88
string $locale = null): string
99
{
1010
return I18n::translate($string, $arguments, $locale);

phpunit.xml.dist

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
verbose="true"
77
colors="true"
88
>
9-
<coverage>
10-
<include>
11-
<directory suffix=".php">./</directory>
12-
</include>
13-
<exclude>
14-
<directory>vendor</directory>
15-
<directory>tests</directory>
16-
</exclude>
17-
</coverage>
18-
<testsuites>
19-
<testsuite name="Koded I18n Test Suite">
20-
<directory>./tests</directory>
21-
</testsuite>
22-
</testsuites>
23-
<php>
24-
<ini name="date.timezone" value="UTC"/>
25-
</php>
9+
<coverage>
10+
<include>
11+
<directory suffix=".php">./</directory>
12+
</include>
13+
<exclude>
14+
<directory>vendor</directory>
15+
<directory>tests</directory>
16+
</exclude>
17+
</coverage>
18+
<testsuites>
19+
<testsuite name="Koded I18n Test Suite">
20+
<directory>./tests</directory>
21+
</testsuite>
22+
</testsuites>
23+
<php>
24+
<ini name="date.timezone" value="UTC"/>
25+
</php>
2626
</phpunit>

tests/Fixtures/xx_XX.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
5+
];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tests\Koded\I18n\Unit;
4+
5+
use Koded\I18n\ArrayCatalog;
6+
use Koded\I18n\I18n;
7+
use Koded\I18n\I18nCatalog;
8+
use Koded\I18n\NoCatalog;
9+
use Koded\Stdlib\Config;
10+
11+
class InvalidArrayCatalogTest extends I18nTestCase
12+
{
13+
public function test_invalid_array()
14+
{
15+
I18n::register(I18nCatalog::new((new Config)
16+
->set('translation.locale', 'xx_XX')
17+
->set('translation.dir', __DIR__ . '/../Fixtures')
18+
->set('translation.catalog', ArrayCatalog::class)
19+
));
20+
21+
$this->assertInstanceOf(NoCatalog::class, I18n::catalogs()['xx_XX'],
22+
'Expected ArrayCatalog, but it fallback to NoCatalog (invalid array)');
23+
}
24+
}

tests/Unit/NormalizeLocaleTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests\Koded\I18n\Unit;
4+
5+
use Koded\I18n\I18n;
6+
use Koded\I18n\I18nCatalog;
7+
use Koded\I18n\NoCatalog;
8+
use Koded\Stdlib\Config;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class NormalizeLocaleTest extends TestCase
12+
{
13+
public function test_should_normalize_locale()
14+
{
15+
$this->assertSame('mk_MK', I18n::catalogs()['mk_MK']->locale(),
16+
'The locale is normalized');
17+
}
18+
19+
protected function setUp(): void
20+
{
21+
I18n::register(I18nCatalog::new((new Config)
22+
->set('translation.catalog', NoCatalog::class)
23+
->set('translation.locale', 'mk_MK_UTF8')
24+
));
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
I18n::flush();
30+
}
31+
}

0 commit comments

Comments
 (0)