From f90df31f01218ac4ea7f62b537a9715504791190 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 10 Nov 2023 15:09:15 +0300 Subject: [PATCH] Added localization of locale names --- .gitattributes | 4 -- composer.json | 3 +- config/private.php | 83 +------------------------------ src/Concerns/Aliases.php | 4 +- src/Concerns/Localized.php | 39 +++++++++++++++ src/Concerns/Mapping.php | 7 ++- src/Data/LocaleData.php | 14 ++++-- src/Data/NativeData.php | 47 +++++++++++++++++ src/Services/Locales.php | 6 ++- tests/Unit/Data/LocalizedTest.php | 33 ++++++++++++ 10 files changed, 142 insertions(+), 98 deletions(-) create mode 100644 src/Concerns/Localized.php create mode 100644 src/Data/NativeData.php create mode 100644 tests/Unit/Data/LocalizedTest.php diff --git a/.gitattributes b/.gitattributes index 43438f7..da09df5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,13 +4,9 @@ tests/ export-ignore -.env.example export-ignore - .editorconfig export-ignore .gitattributes export-ignore .gitignore export-ignore -package.json export-ignore - phpunit.php export-ignore phpunit.xml export-ignore diff --git a/composer.json b/composer.json index aafe609..7668eee 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,8 @@ "ext-json": "*", "archtechx/enums": "^0.3.2", "dragon-code/support": "^6.11.3", - "illuminate/collections": "^10.0" + "illuminate/collections": "^10.0", + "laravel-lang/native-locale-names": "^1.2" }, "require-dev": { "orchestra/testbench": "^8.0", diff --git a/config/private.php b/config/private.php index 0bd575c..3e4117d 100644 --- a/config/private.php +++ b/config/private.php @@ -22,552 +22,473 @@ Locale::Afrikaans->value => [ 'type' => 'Latn', 'name' => 'Afrikaans', - 'native' => 'Afrikaans', 'regional' => 'af_ZA', ], Locale::Albanian->value => [ 'type' => 'Latn', 'name' => 'Albanian', - 'native' => 'Shqip', 'regional' => 'sq_AL', ], Locale::Arabic->value => [ 'type' => 'Arab', 'name' => 'Arabic', - 'native' => 'العربية', 'regional' => 'ar_AE', ], Locale::Armenian->value => [ 'type' => 'Armn', 'name' => 'Armenian', - 'native' => 'Հայերեն', 'regional' => 'hy_AM', ], Locale::Azerbaijani->value => [ 'type' => 'Latn', 'name' => 'Azerbaijani (Latin)', - 'native' => 'Azərbaycanca', 'regional' => 'az_AZ', ], Locale::Basque->value => [ 'type' => 'Latn', 'name' => 'Basque', - 'native' => 'Euskara', 'regional' => 'eu_ES', ], Locale::Belarusian->value => [ 'type' => 'Cyrl', 'name' => 'Belarusian', - 'native' => 'Беларуская', 'regional' => 'be_BY', ], Locale::Bengali->value => [ 'type' => 'Beng', 'name' => 'Bengali', - 'native' => 'বাংলা', 'regional' => 'bn_BD', ], Locale::Bosnian->value => [ 'type' => 'Latn', 'name' => 'Bosnian', - 'native' => 'Bosanski', 'regional' => 'bs_BA', ], Locale::Bulgarian->value => [ 'type' => 'Cyrl', 'name' => 'Bulgarian', - 'native' => 'Български', 'regional' => 'bg_BG', ], Locale::Catalan->value => [ 'type' => 'Latn', 'name' => 'Catalan', - 'native' => 'Català', 'regional' => 'ca_ES', ], Locale::CentralKhmer->value => [ 'type' => 'Khmr', 'name' => 'Khmer', - 'native' => 'ភាសាខ្មែរ', 'regional' => 'km_KH', ], Locale::Chinese->value => [ 'type' => 'Hans', 'name' => 'Chinese (Simplified)', - 'native' => '简体中文', 'regional' => 'zh_CN', ], Locale::ChineseHongKong->value => [ 'type' => 'Hans', 'name' => 'Chinese (Hong Kong)', - 'native' => '简体中文', 'regional' => 'zh_HK', ], Locale::ChineseT->value => [ 'type' => 'Hans', 'name' => 'Chinese (Taiwan)', - 'native' => '简体中文', 'regional' => 'zh_TW', ], Locale::Croatian->value => [ 'type' => 'Latn', 'name' => 'Croatian', - 'native' => 'Hrvatski', 'regional' => 'hr_HR', ], Locale::Czech->value => [ 'type' => 'Latn', 'name' => 'Czech', - 'native' => 'Čeština', 'regional' => 'cs_CZ', ], Locale::Danish->value => [ 'type' => 'Latn', 'name' => 'Danish', - 'native' => 'Dansk', 'regional' => 'da_DK', ], Locale::Dutch->value => [ 'type' => 'Latn', 'name' => 'Dutch', - 'native' => 'Nederlands', 'regional' => 'nl_NL', ], Locale::English->value => [ 'type' => 'Latn', 'name' => 'English', - 'native' => 'English', 'regional' => 'en_GB', ], Locale::Estonian->value => [ 'type' => 'Latn', 'name' => 'Estonian', - 'native' => 'Eesti', 'regional' => 'et_EE', ], Locale::Finnish->value => [ 'type' => 'Latn', 'name' => 'Finnish', - 'native' => 'Suomalainen', 'regional' => 'fi_FI', ], Locale::French->value => [ 'type' => 'Latn', 'name' => 'French', - 'native' => 'Français', 'regional' => 'fr_FR', ], Locale::Galician->value => [ 'type' => 'Latn', 'name' => 'Galician', - 'native' => 'Galego', 'regional' => 'gl_ES', ], Locale::Georgian->value => [ 'type' => 'Geor', 'name' => 'Georgian', - 'native' => 'ქართული', 'regional' => 'ka_GE', ], Locale::German->value => [ 'type' => 'Latn', 'name' => 'German', - 'native' => 'Deutsch', 'regional' => 'de_DE', ], Locale::GermanSwitzerland->value => [ 'type' => 'Latn', 'name' => 'Swiss High German', - 'native' => 'Schweizer Hochdeutsch', 'regional' => 'de_CH', ], Locale::Greek->value => [ 'type' => 'Grek', 'name' => 'Greek', - 'native' => 'Ελληνικά', 'regional' => 'el_GR', ], Locale::Gujarati->value => [ 'type' => 'Gujr', 'name' => 'Gujarati', - 'native' => 'ગુજરાતી', 'regional' => 'gu_IN', ], Locale::Hebrew->value => [ 'type' => 'Hebr', 'name' => 'Hebrew', - 'native' => 'עברית', 'regional' => 'he_IL', ], Locale::Hindi->value => [ 'type' => 'Deva', 'name' => 'Hindi', - 'native' => 'हिन्दी', 'regional' => 'hi_IN', ], Locale::Hungarian->value => [ 'type' => 'Latn', 'name' => 'Hungarian', - 'native' => 'Magyar', 'regional' => 'hu_HU', ], Locale::Icelandic->value => [ 'type' => 'Latn', 'name' => 'Icelandic', - 'native' => 'Íslenska', 'regional' => 'is_IS', ], Locale::Indonesian->value => [ 'type' => 'Latn', 'name' => 'Indonesian', - 'native' => 'Bahasa Indonesia', 'regional' => 'id_ID', ], Locale::Italian->value => [ 'type' => 'Latn', 'name' => 'Italian', - 'native' => 'Italiano', 'regional' => 'it_IT', ], Locale::Japanese->value => [ 'type' => 'Jpan', 'name' => 'Japanese', - 'native' => '日本語', 'regional' => 'ja_JP', ], Locale::Kannada->value => [ 'type' => 'Knda', 'name' => 'Kannada', - 'native' => 'ಕನ್ನಡ', 'regional' => 'kn_IN', ], Locale::Kazakh->value => [ 'type' => 'Cyrl', 'name' => 'Kazakh', - 'native' => 'Қазақ тілі', 'regional' => 'kk_KZ', ], Locale::Korean->value => [ 'type' => 'Hang', 'name' => 'Korean', - 'native' => '한국어', 'regional' => 'ko_KR', ], Locale::Latvian->value => [ 'type' => 'Latn', 'name' => 'Latvian', - 'native' => 'Latviešu', 'regional' => 'lv_LV', ], Locale::Lithuanian->value => [ 'type' => 'Latn', 'name' => 'Lithuanian', - 'native' => 'Lietuvių', 'regional' => 'lt_LT', ], Locale::Macedonian->value => [ 'type' => 'Cyrl', 'name' => 'Macedonian', - 'native' => 'Македонски', 'regional' => 'mk_MK', ], Locale::Malay->value => [ 'type' => 'Latn', 'name' => 'Malay', - 'native' => 'Bahasa Melayu', 'regional' => 'ms_MY', ], Locale::Marathi->value => [ 'type' => 'Deva', 'name' => 'Marathi', - 'native' => 'मराठी', 'regional' => 'mr_IN', ], Locale::Mongolian->value => [ 'type' => 'Mong', 'name' => 'Mongolian', - 'native' => 'ᠮᠣᠨᠭᠭᠣᠯ ᠬᠡᠯᠡ', 'regional' => 'mn_MN', ], Locale::Nepali->value => [ - 'type' => 'Deva', - 'name' => 'Nepali', - 'native' => 'नेपाली', + 'type' => 'Deva', + 'name' => 'Nepali', ], Locale::NorwegianBokmal->value => [ 'type' => 'Latn', 'name' => 'Norwegian Bokmål', - 'native' => 'Norwegian Bokmål', 'regional' => 'nb_NO', ], Locale::NorwegianNynorsk->value => [ 'type' => 'Latn', 'name' => 'Norwegian Nynorsk', - 'native' => 'Norwegian Nynorsk', 'regional' => 'nn_NO', ], Locale::Occitan->value => [ 'type' => 'Latn', 'name' => 'Occitan', - 'native' => 'Occitan', 'regional' => 'oc_FR', ], Locale::Pashto->value => [ 'type' => 'Arab', 'name' => 'Pashto', - 'native' => 'پښتو', 'regional' => 'ps_AF', ], Locale::Persian->value => [ 'type' => 'Arab', 'name' => 'Persian', - 'native' => 'فارسی', 'regional' => 'fa_IR', ], Locale::Pilipino->value => [ 'type' => 'Latn', 'name' => 'Filipino', - 'native' => 'Filipino', 'regional' => 'fil_PH', ], Locale::Polish->value => [ 'type' => 'Latn', 'name' => 'Polish', - 'native' => 'Polski', 'regional' => 'pl_PL', ], Locale::Portuguese->value => [ 'type' => 'Latn', 'name' => 'Portuguese', - 'native' => 'Português', 'regional' => 'pt_PT', ], Locale::PortugueseBrazil->value => [ 'type' => 'Latn', 'name' => 'Brazilian Portuguese', - 'native' => 'Português do Brasil', 'regional' => 'pt_BR', ], Locale::Romanian->value => [ 'type' => 'Latn', 'name' => 'Romanian', - 'native' => 'Română', 'regional' => 'ro_RO', ], Locale::Russian->value => [ 'type' => 'Cyrl', 'name' => 'Russian', - 'native' => 'Русский', 'regional' => 'ru_RU', ], Locale::Sardinian->value => [ 'type' => 'Latn', 'name' => 'Sardinian', - 'native' => 'Sardu', 'regional' => 'sc_IT', ], Locale::SerbianCyrillic->value => [ 'type' => 'Cyrl', 'name' => 'Serbian (Cyrillic)', - 'native' => 'Српски', 'regional' => 'sr_RS', ], Locale::SerbianLatin->value => [ 'type' => 'Latn', 'name' => 'Serbian (Latin)', - 'native' => 'Srpski', 'regional' => 'sr_RS', ], Locale::SerbianMontenegrin->value => [ 'type' => 'Latn', 'name' => 'Serbian (Montenegro)', - 'native' => 'Serbian (Latin, Montenegro)', 'regional' => 'sr_Latn_ME', ], Locale::Sinhala->value => [ 'type' => 'Sinh', 'name' => 'Sinhala', - 'native' => 'සිංහල', 'regional' => 'si_LK', ], Locale::Slovak->value => [ 'type' => 'Latn', 'name' => 'Slovak', - 'native' => 'Slovenčina', 'regional' => 'sk_SK', ], Locale::Slovenian->value => [ 'type' => 'Latn', 'name' => 'Slovene', - 'native' => 'Slovenščina', 'regional' => 'sl_SI', ], Locale::Spanish->value => [ 'type' => 'Latn', 'name' => 'Spanish', - 'native' => 'Español', 'regional' => 'es_ES', ], Locale::Swahili->value => [ 'type' => 'Latn', 'name' => 'Swahili', - 'native' => 'Kiswahili', 'regional' => 'sw_KE', ], Locale::Swedish->value => [ 'type' => 'Latn', 'name' => 'Swedish', - 'native' => 'Svenska', 'regional' => 'sv_SE', ], Locale::Tagalog->value => [ 'type' => 'Latn', 'name' => 'Tagalog', - 'native' => 'Tagalog', 'regional' => 'tl_PH', ], Locale::Tajik->value => [ 'type' => 'Cyrl', 'name' => 'Tajik (Cyrillic)', - 'native' => 'Тоҷикӣ', 'regional' => 'tg_TJ', ], Locale::Thai->value => [ 'type' => 'Thai', 'name' => 'Thai', - 'native' => 'ไทย', 'regional' => 'th_TH', ], Locale::Turkish->value => [ 'type' => 'Latn', 'name' => 'Turkish', - 'native' => 'Türkçe', 'regional' => 'tr_TR', ], Locale::Turkmen->value => [ 'type' => 'Cyrl', 'name' => 'Turkmen', - 'native' => 'Түркменче', 'regional' => 'tk_TM', ], Locale::Uighur->value => [ 'type' => 'Arab', 'name' => 'Uyghur', - 'native' => 'ئۇيغۇرچە', 'regional' => 'ug_CN', ], Locale::Ukrainian->value => [ 'type' => 'Cyrl', 'name' => 'Ukrainian', - 'native' => 'Українська', 'regional' => 'uk_UA', ], Locale::Urdu->value => [ 'type' => 'Arab', 'name' => 'Urdu', - 'native' => 'اردو', 'regional' => 'ur_PK', ], Locale::UzbekCyrillic->value => [ 'type' => 'Cyrl', 'name' => 'Uzbek (Cyrillic)', - 'native' => 'Ўзбек', 'regional' => 'uz_UZ', ], Locale::UzbekLatin->value => [ 'type' => 'Latn', 'name' => 'Uzbek (Latin)', - 'native' => 'Oʼzbekcha', 'regional' => 'uz_UZ', ], Locale::Vietnamese->value => [ 'type' => 'Latn', 'name' => 'Vietnamese', - 'native' => 'Tiếng Việt', 'regional' => 'vi_VN', ], Locale::Welsh->value => [ 'type' => 'Latn', 'name' => 'Welsh', - 'native' => 'Cymraeg', 'regional' => 'cy_GB', ], ], diff --git a/src/Concerns/Aliases.php b/src/Concerns/Aliases.php index b353142..cf757a0 100644 --- a/src/Concerns/Aliases.php +++ b/src/Concerns/Aliases.php @@ -22,7 +22,7 @@ trait Aliases { - protected function fromAlias(LocaleCode|string|null $locale, LocaleCode|string|null $default = null): ?string + protected function fromAlias(LocaleCode|string|null $locale): ?string { if ($locale = $locale?->value ?? $locale) { return collect($this->aliases())->flip()->get($locale, $locale); @@ -31,7 +31,7 @@ protected function fromAlias(LocaleCode|string|null $locale, LocaleCode|string|n return $this->stringify($locale); } - protected function toAlias(LocaleCode|string|null $locale, LocaleCode|string|null $default = null): ?string + protected function toAlias(LocaleCode|string|null $locale): ?string { if ($locale = $locale?->value ?? $locale) { return collect($this->aliases())->get($locale, $locale); diff --git a/src/Concerns/Localized.php b/src/Concerns/Localized.php new file mode 100644 index 0000000..e42cde3 --- /dev/null +++ b/src/Concerns/Localized.php @@ -0,0 +1,39 @@ + + * @copyright 2023 Laravel Lang Team + * @license MIT + * + * @see https://laravel-lang.com + */ + +declare(strict_types=1); + +namespace LaravelLang\Locales\Concerns; + +use LaravelLang\Locales\Data\NativeData; +use LaravelLang\NativeLocaleNames\Native; + +trait Localized +{ + public function localized(): NativeData + { + return $this->registry([__METHOD__, $this->currentLocale()], fn () => new NativeData( + Native::get(), + Native::get($this->currentLocale()) + )); + } + + protected function currentLocale(): string + { + return $this->fromAlias( + $this->raw->getDefault() + ); + } +} diff --git a/src/Concerns/Mapping.php b/src/Concerns/Mapping.php index 90f97a0..115270e 100644 --- a/src/Concerns/Mapping.php +++ b/src/Concerns/Mapping.php @@ -23,13 +23,11 @@ trait Mapping { - use Aliases; - protected function map(string $locale): LocaleData { $locale = $this->findLocale($locale); - return new LocaleData($locale, $this->mapData($locale)); + return new LocaleData($locale, $this->mapData($locale), $this->localized()); } protected function mapLocales(array $locales): array @@ -40,7 +38,8 @@ protected function mapLocales(array $locales): array protected function findLocale(string $locale): LocaleEnum { return LocaleEnum::tryFrom($this->toAlias($locale)) - ?: LocaleEnum::tryFrom($this->fromAlias($locale)); + ?: LocaleEnum::tryFrom($this->fromAlias($locale)) + ?: LocaleEnum::English; } protected function mapData(LocaleEnum $locale): array diff --git a/src/Data/LocaleData.php b/src/Data/LocaleData.php index cf0bfdf..88dab68 100644 --- a/src/Data/LocaleData.php +++ b/src/Data/LocaleData.php @@ -32,15 +32,19 @@ class LocaleData public readonly string $native; - public readonly string $regional; + public readonly string $localized; - public function __construct(LocaleEnum $locale, array $data) + public readonly ?string $regional; + + public function __construct(LocaleEnum $locale, array $data, NativeData $native) { - $this->code = $this->toAlias($locale, $locale); + $this->code = $this->toAlias($locale); $this->type = $data['type'] ?? 'Latn'; $this->name = $data['name']; - $this->native = $data['native']; - $this->regional = $data['regional'] ?? ''; + $this->regional = $data['regional'] ?? null; + + $this->native = $native->getCurrent($this->code); + $this->localized = $native->getLocalized($this->code); } } diff --git a/src/Data/NativeData.php b/src/Data/NativeData.php new file mode 100644 index 0000000..ada0999 --- /dev/null +++ b/src/Data/NativeData.php @@ -0,0 +1,47 @@ + + * @copyright 2023 Laravel Lang Team + * @license MIT + * + * @see https://laravel-lang.com + */ + +declare(strict_types=1); + +namespace LaravelLang\Locales\Data; + +use LaravelLang\Locales\Concerns\Aliases; + +class NativeData +{ + use Aliases; + + public function __construct( + public readonly array $current, + public readonly array $localized + ) {} + + public function getCurrent(string $locale): string + { + return $this->get($this->current, $locale); + } + + public function getLocalized(string $locale): string + { + return $this->get($this->localized, $locale); + } + + protected function get(array $items, string $locale): string + { + $locale = $this->fromAlias($locale); + + return $items[$locale]; + } +} diff --git a/src/Services/Locales.php b/src/Services/Locales.php index 78cdad6..2c8d365 100644 --- a/src/Services/Locales.php +++ b/src/Services/Locales.php @@ -17,6 +17,8 @@ namespace LaravelLang\Locales\Services; +use LaravelLang\Locales\Concerns\Aliases; +use LaravelLang\Locales\Concerns\Localized; use LaravelLang\Locales\Concerns\Mapping; use LaravelLang\Locales\Concerns\Registry; use LaravelLang\Locales\Data\LocaleData; @@ -24,8 +26,10 @@ class Locales { - use Registry; + use Aliases; + use Localized; use Mapping; + use Registry; public function __construct( protected RawLocales $raw diff --git a/tests/Unit/Data/LocalizedTest.php b/tests/Unit/Data/LocalizedTest.php new file mode 100644 index 0000000..96d08ce --- /dev/null +++ b/tests/Unit/Data/LocalizedTest.php @@ -0,0 +1,33 @@ + + * @copyright 2023 Laravel Lang Team + * @license MIT + * + * @see https://laravel-lang.com + */ + +declare(strict_types=1); + +use LaravelLang\Locales\Facades\Locales; +use LaravelLang\NativeLocaleNames\Native; + +it('checks language localization', function (string $locale) { + app()->setLocale($locale); + + expect(app()->getLocale())->toBe($locale); + + $combined = Native::get(); + $localized = Native::get($locale); + + foreach (Locales::available() as $item) { + expect($item->native)->toBe($combined[$item->code]); + expect($item->localized)->toBe($localized[$item->code]); + } +})->with('locales');