Skip to content

Commit

Permalink
- fixes the properties and methods scope
Browse files Browse the repository at this point in the history
- adds more tests
- CS chore
  • Loading branch information
kodeart committed Jan 25, 2022
1 parent 6f004d8 commit 97cf139
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 56 deletions.
59 changes: 24 additions & 35 deletions I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use Koded\Stdlib\Config;
use Throwable;
use function error_log;
use function ini_set;
use function strtr;
use function substr_count;
use function vsprintf;

interface I18nFormatter
Expand Down Expand Up @@ -65,24 +63,24 @@ public static function translate(
string $locale = null): string
{
try {
return static::$catalogs[$locale]->translate('messages', $string, $arguments);
return self::$catalogs[$locale]->translate('messages', $string, $arguments);
} catch (Throwable) {
static::registerCatalog($locale ??= static::locale());
return static::$catalogs[$locale]->translate('messages', $string, $arguments);
self::registerCatalog($locale ??= self::locale());
return self::$catalogs[$locale]->translate('messages', $string, $arguments);
}
}

public static function locale(): string
{
return static::$locale ??= static::normalizeLocale(\Locale::getDefault());
return self::$locale ??= I18nCatalog::normalizeLocale(\Locale::getDefault());
}

/**
* @return array{string, I18nCatalog}
*/
public static function catalogs(): array
{
return static::$catalogs;
return self::$catalogs;
}

/**
Expand All @@ -91,7 +89,7 @@ public static function catalogs(): array
public static function info(): array
{
$catalogs = [];
foreach (static::$catalogs as $locale => $instance) {
foreach (self::$catalogs as $locale => $instance) {
$catalogs[$locale] = [
'class' => $instance::class,
'formatter' => $instance->formatter()::class,
Expand All @@ -100,7 +98,7 @@ public static function info(): array
];
}
return [
'locale' => static::$locale,
'locale' => self::$locale,
'catalogs' => $catalogs,
];
}
Expand All @@ -110,52 +108,43 @@ public static function register(
bool $asDefault = false): void
{
$locale = $catalog->locale();
if ($asDefault || empty(static::$catalogs)) {
static::setDefaultLocale($locale);
static::$directory = $catalog->directory();
static::$formatter = $catalog->formatter()::class;
static::$catalog = $catalog::class;
if ($asDefault || empty(self::$catalogs)) {
self::setDefaultLocale($locale);
self::$directory = $catalog->directory();
self::$formatter = $catalog->formatter()::class;
self::$catalog = $catalog::class;
}
static::$catalogs[$locale] = $catalog;
self::$catalogs[$locale] = $catalog;
}

public static function flush(): void
{
static::$catalogs = [];
static::$directory = null;
static::$formatter = null;
static::$catalog = null;
static::$locale = null;
self::$catalogs = [];
self::$directory = null;
self::$formatter = null;
self::$catalog = null;
self::$locale = null;
ini_set('intl.default_locale', '');
\Locale::setDefault('');
}

private static function registerCatalog(string $locale): void
{
if (isset(static::$catalogs[$locale])) {
if (isset(self::$catalogs[$locale])) {
return;
}
static::$catalogs[$locale] = I18nCatalog::new((new Config)
self::$catalogs[$locale] = I18nCatalog::new((new Config)
->set('translation.locale', $locale)
->set('translation.dir', static::$directory)
->set('translation.formatter', static::$formatter)
->set('translation.catalog', static::$catalog)
->set('translation.dir', self::$directory)
->set('translation.formatter', self::$formatter)
->set('translation.catalog', self::$catalog)
);
}

private static function setDefaultLocale(string $locale): void
{
static::$locale = $locale;
self::$locale = $locale;
ini_set('intl.default_locale', $locale);
\Locale::setDefault($locale);
}

private static function normalizeLocale(string $locale): string
{
if (substr_count($locale, '_') > 1) {
$locale = explode('_', $locale);
$locale = "$locale[0]_$locale[1]";
}
return $locale;
}
}
17 changes: 14 additions & 3 deletions I18nCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,30 @@ public static function new(Configuration $conf): I18nCatalog
$instance = new $catalog(
new $formatter,
$directory = $conf->get('translation.dir', getcwd() . '/locales'),
$locale = $conf->get('translation.locale', I18n::DEFAULT_LOCALE)
$locale = self::normalizeLocale($conf->get('translation.locale', I18n::DEFAULT_LOCALE))
);
if ($instance->supports($locale)) {
return $instance;
}
if ($catalog !== ArrayCatalog::class) {
error_log(" > ($locale) gettext not supported, try ArrayCatalog ...");
error_log(" > ($locale) gettext not supported, trying ArrayCatalog ...");
$conf->set('translation.catalog', ArrayCatalog::class);
return static::new($conf);
}
// Last resort, passthru
return new NoCatalog(new $formatter, $directory, $locale);
}

public static function normalizeLocale(string $locale): string
{
$locale = str_replace('.', '_', $locale);
if (substr_count($locale, '_') > 1) {
$locale = explode('_', $locale);
$locale = "$locale[0]_$locale[1]";
}
return $locale;
}

public function translate(
string $domain,
string $key,
Expand Down Expand Up @@ -114,6 +124,7 @@ protected function supports(string $locale): bool
{
return true;
}

// @codeCoverageIgnoreEnd

protected function initialize(string $locale): string|false
Expand Down Expand Up @@ -141,7 +152,7 @@ protected function initialize(string $locale): string|false
try {
$this->data = require($catalog = "$this->directory/$locale.php");
if (false === array_key_exists('messages', $this->data)) {
error_log("i18n catalog $catalog is missing the messages array");
error_log("ERROR : i18n catalog $catalog is missing the messages array");
return false;
}
return $locale;
Expand Down
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function __(
string $string,
array $arguments = [],
array $arguments = [],
string $locale = null): string
{
return I18n::translate($string, $arguments, $locale);
Expand Down
34 changes: 17 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
verbose="true"
colors="true"
>
<coverage>
<include>
<directory suffix=".php">./</directory>
</include>
<exclude>
<directory>vendor</directory>
<directory>tests</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Koded I18n Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="date.timezone" value="UTC"/>
</php>
<coverage>
<include>
<directory suffix=".php">./</directory>
</include>
<exclude>
<directory>vendor</directory>
<directory>tests</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Koded I18n Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
5 changes: 5 additions & 0 deletions tests/Fixtures/xx_XX.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [

];
24 changes: 24 additions & 0 deletions tests/Unit/InvalidArrayCatalogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Tests\Koded\I18n\Unit;

use Koded\I18n\ArrayCatalog;
use Koded\I18n\I18n;
use Koded\I18n\I18nCatalog;
use Koded\I18n\NoCatalog;
use Koded\Stdlib\Config;

class InvalidArrayCatalogTest extends I18nTestCase
{
public function test_invalid_array()
{
I18n::register(I18nCatalog::new((new Config)
->set('translation.locale', 'xx_XX')
->set('translation.dir', __DIR__ . '/../Fixtures')
->set('translation.catalog', ArrayCatalog::class)
));

$this->assertInstanceOf(NoCatalog::class, I18n::catalogs()['xx_XX'],
'Expected ArrayCatalog, but it fallback to NoCatalog (invalid array)');
}
}
31 changes: 31 additions & 0 deletions tests/Unit/NormalizeLocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\Koded\I18n\Unit;

use Koded\I18n\I18n;
use Koded\I18n\I18nCatalog;
use Koded\I18n\NoCatalog;
use Koded\Stdlib\Config;
use PHPUnit\Framework\TestCase;

class NormalizeLocaleTest extends TestCase
{
public function test_should_normalize_locale()
{
$this->assertSame('mk_MK', I18n::catalogs()['mk_MK']->locale(),
'The locale is normalized');
}

protected function setUp(): void
{
I18n::register(I18nCatalog::new((new Config)
->set('translation.catalog', NoCatalog::class)
->set('translation.locale', 'mk_MK_UTF8')
));
}

protected function tearDown(): void
{
I18n::flush();
}
}

0 comments on commit 97cf139

Please sign in to comment.