Skip to content

Commit 23e5ba0

Browse files
committed
refactor(l10n): move locale handling logic to L10Helper and improve test coverage
1 parent a8f5029 commit 23e5ba0

6 files changed

Lines changed: 175 additions & 164 deletions

File tree

src/CommonPlugin.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace Eclipse\Common;
44

55
use Eclipse\Common\Admin\Filament\Clusters\Settings;
6-
use Eclipse\Common\Exceptions\InvalidConfigurationException;
76
use Eclipse\Common\Foundation\Plugins\Plugin;
8-
use Eclipse\Common\Helpers\LocalizationHelper;
97

108
class CommonPlugin extends Plugin
119
{
@@ -25,29 +23,4 @@ public function settingsCluster(?string $settingsCluster): self
2523

2624
return $this;
2725
}
28-
29-
/**
30-
* Get a list of available locales for the application.
31-
* Returns an array of locale codes (as keys) and their corresponding names (as values).
32-
*
33-
* @throws InvalidConfigurationException
34-
*/
35-
public function getAvailableLocales(): array
36-
{
37-
$config = config('eclipse-common.available_locales');
38-
39-
if (is_array($config)) {
40-
$locales = $config;
41-
} elseif (is_callable($config)) {
42-
$locales = $config();
43-
} else {
44-
throw new InvalidConfigurationException('Configuration "eclipse-common.available_locales" must be an array or a callable that returns an array.');
45-
}
46-
47-
if (empty($locales)) {
48-
throw new InvalidConfigurationException('Configuration "eclipse-common.available_locales" must contain at least one locale.');
49-
}
50-
51-
return array_map(fn ($locale) => LocalizationHelper::getLanguageNameFromCode($locale, true), $locales);
52-
}
5326
}

src/Helpers/L10nHelper.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Eclipse\Common\Helpers;
4+
5+
use Eclipse\Common\Exceptions\InvalidConfigurationException;
6+
use Locale;
7+
8+
class L10nHelper
9+
{
10+
/**
11+
* Get the available locale codes from the configuration.
12+
*
13+
* @return string[]
14+
* @throws InvalidConfigurationException
15+
*/
16+
public static function getAvailable(): array
17+
{
18+
$config = config('eclipse-common.available_locales');
19+
20+
if (is_array($config)) {
21+
$locales = $config;
22+
} elseif (is_callable($config)) {
23+
$locales = $config();
24+
} else {
25+
throw new InvalidConfigurationException('Configuration "eclipse-common.available_locales" must be an array or a callable that returns an array.');
26+
}
27+
28+
if (empty($locales)) {
29+
throw new InvalidConfigurationException('Configuration "eclipse-common.available_locales" must contain at least one locale.');
30+
}
31+
32+
// Return the array with values as keys
33+
return array_combine($locales, $locales);
34+
}
35+
36+
/**
37+
* Get a list of available locales for the application.
38+
* Returns an array of locale codes (as keys) and their corresponding names (as values).
39+
*
40+
* @return string[]
41+
* @throws InvalidConfigurationException
42+
*/
43+
public static function getOptions(): array
44+
{
45+
return array_map(fn ($locale) => static::getLanguageName($locale, true), static::getAvailable());
46+
}
47+
48+
/**
49+
* Get the language name from the language code.
50+
* 1. If the `intl` PHP extension is installed, use the `\Locale` class to get the language name.
51+
* 2. Otherwise, return the language code
52+
*
53+
* @param string $code Language code
54+
* @param bool $include_code Include the language code in the output (in parentheses)
55+
* @return string Language name
56+
*/
57+
public static function getLanguageName(string $code, bool $include_code = false): string
58+
{
59+
$code = trim(str_replace('_', '-', $code));
60+
61+
if ($code === '') {
62+
return '';
63+
}
64+
65+
$name = Locale::getDisplayLanguage($code, config('app.locale'));
66+
67+
if (is_string($name) && ! empty($name)) {
68+
return $name . ($include_code ? ' (' . $code . ')' : '');
69+
}
70+
71+
return $code;
72+
}
73+
}

src/Helpers/LocalizationHelper.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/CommonPluginTest.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
use Eclipse\Common\CommonPlugin;
44
use Eclipse\Common\Admin\Filament\Clusters\Settings;
5-
use Eclipse\Common\Exceptions\InvalidConfigurationException;
6-
use Illuminate\Support\Facades\Config;
75

86
it('can get and set settings cluster', function () {
97
$plugin = new CommonPlugin();
@@ -16,48 +14,3 @@
1614
$plugin->settingsCluster(null);
1715
expect($plugin->getSettingsCluster())->toBeNull();
1816
});
19-
20-
it('can get available locales from array config', function () {
21-
// We need to use a new instance because of the static variable in getAvailableLocales
22-
$plugin = new CommonPlugin();
23-
24-
Config::set('eclipse-common.available_locales', ['en', 'sl']);
25-
26-
$locales = $plugin->getAvailableLocales();
27-
28-
expect($locales)->toBeArray()
29-
->and($locales)->toHaveCount(2)
30-
->and($locales)->toContain('English (en)')
31-
->and($locales)->toContain('Slovenian (sl)');
32-
});
33-
34-
it('can get available locales from callable config', function () {
35-
$plugin = new CommonPlugin();
36-
37-
Config::set('eclipse-common.available_locales', fn() => ['en', 'de']);
38-
39-
$locales = $plugin->getAvailableLocales();
40-
41-
expect($locales)->toBeArray()
42-
->and($locales)->toHaveCount(2)
43-
->and($locales)->toContain('English (en)')
44-
->and($locales)->toContain('German (de)');
45-
});
46-
47-
it('throws exception for invalid locales configuration', function () {
48-
$plugin = new CommonPlugin();
49-
50-
Config::set('eclipse-common.available_locales', 'invalid');
51-
52-
expect(fn() => $plugin->getAvailableLocales())
53-
->toThrow(InvalidConfigurationException::class, 'Configuration "eclipse-common.available_locales" must be an array or a callable that returns an array.');
54-
});
55-
56-
it('throws exception for empty locales configuration', function () {
57-
$plugin = new CommonPlugin();
58-
59-
Config::set('eclipse-common.available_locales', []);
60-
61-
expect(fn() => $plugin->getAvailableLocales())
62-
->toThrow(InvalidConfigurationException::class, 'Configuration "eclipse-common.available_locales" must contain at least one locale.');
63-
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
use Eclipse\Common\Helpers\L10nHelper as Helper;
4+
use Eclipse\Common\Exceptions\InvalidConfigurationException;
5+
use Illuminate\Support\Facades\Config;
6+
7+
test('it returns empty string for empty language code', function () {
8+
expect(Helper::getLanguageName(''))->toBe('');
9+
});
10+
11+
test('it returns the language name for a given code', function () {
12+
// Assuming English is available
13+
$name = Helper::getLanguageName('en');
14+
15+
expect($name)->toBe('English');
16+
});
17+
18+
test('it returns the language name with code for a given code', function () {
19+
$name = Helper::getLanguageName('en', true);
20+
21+
expect($name)->toBe('English (en)');
22+
});
23+
24+
test('it handles language codes with underscores', function () {
25+
$name = Helper::getLanguageName('en_US');
26+
27+
// Depending on ICU version and environment, this might return 'English' or 'English (United States)'
28+
// But for display language only it should be 'English'
29+
expect($name)->toBe('English');
30+
});
31+
32+
test('it handles more specific language codes', function () {
33+
$name = Helper::getLanguageName('sl_SI');
34+
35+
// Ensure it works for other languages too
36+
expect($name)->toBe('Slovenian');
37+
});
38+
39+
test('it handles language codes with hyphens', function () {
40+
$name = Helper::getLanguageName('en-GB');
41+
expect($name)->toBe('English');
42+
});
43+
44+
test('it includes the code in the output when requested', function () {
45+
$name = Helper::getLanguageName('en', true);
46+
expect($name)->toBe('English (en)');
47+
});
48+
49+
test('it returns the code for unknown/invalid languages if intl is not helping', function () {
50+
$name = Helper::getLanguageName('xyz');
51+
expect($name)->toBe('xyz');
52+
});
53+
54+
test('it trims the language code', function () {
55+
$name = Helper::getLanguageName(' en ');
56+
57+
expect($name)->toBe('English');
58+
});
59+
60+
test('it can get available locales from array config', function () {
61+
Config::set('eclipse-common.available_locales', ['en', 'sl']);
62+
63+
$locales = Helper::getAvailable();
64+
65+
expect($locales)->toBeArray()
66+
->and($locales)->toHaveCount(2)
67+
->and($locales)->toBe(['en' => 'en', 'sl' => 'sl']);
68+
});
69+
70+
test('it can get available locales from callable config', function () {
71+
Config::set('eclipse-common.available_locales', fn () => ['en', 'de']);
72+
73+
$locales = Helper::getAvailable();
74+
75+
expect($locales)->toBeArray()
76+
->and($locales)->toHaveCount(2)
77+
->and($locales)->toBe(['en' => 'en', 'de' => 'de']);
78+
});
79+
80+
test('it throws exception for invalid locales configuration', function () {
81+
Config::set('eclipse-common.available_locales', 'invalid');
82+
83+
expect(fn () => Helper::getAvailable())
84+
->toThrow(InvalidConfigurationException::class, 'Configuration "eclipse-common.available_locales" must be an array or a callable that returns an array.');
85+
});
86+
87+
test('it throws exception for empty locales configuration', function () {
88+
Config::set('eclipse-common.available_locales', []);
89+
90+
expect(fn () => Helper::getAvailable())
91+
->toThrow(InvalidConfigurationException::class, 'Configuration "eclipse-common.available_locales" must contain at least one locale.');
92+
});
93+
94+
test('it can get options for locales', function () {
95+
Config::set('eclipse-common.available_locales', ['en', 'sl']);
96+
97+
$options = Helper::getOptions();
98+
99+
expect($options)->toBeArray()
100+
->and($options)->toHaveCount(2)
101+
->and($options)->toBe(['en' => 'English (en)', 'sl' => 'Slovenian (sl)']);
102+
});

tests/Unit/Helpers/LocalizationHelperTest.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)