Skip to content

Commit 9e75df3

Browse files
committed
feat: use symfony/intl for locale display names
Replace the hardcoded locale name array in locale_names() with a proper ICU-backed implementation using Symfony\Component\Intl\Locales. - Add symfony/intl ^6.0|^7.0 as a required dependency - locale_names() now calls Locales::getName($locale, $locale) for each active locale, returning the name in the locale's own language - Falls back gracefully to the locale code if the ICU data doesn't know it - Site-level overrides via the localeNames config key still work The hardcoded list covered only 7 locales; with ICU data the function works for any locale code without needing manual maintenance. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent edaa984 commit 9e75df3

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"require": {
1313
"php": "^8.0",
14-
"elaborate-code/php-json-tongue": "^1.0"
14+
"elaborate-code/php-json-tongue": "^1.0",
15+
"symfony/intl": "^6.0|^7.0"
1516
},
1617
"require-dev": {
1718
"bamarni/composer-bin-plugin": "^1.8",
@@ -55,5 +56,8 @@
5556
}
5657
},
5758
"minimum-stability": "stable",
58-
"prefer-stable": true
59+
"prefer-stable": true,
60+
"suggest": {
61+
"ext-intl": "For faster locale display name resolution"
62+
}
5963
}

src/helpers.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Symfony\Component\Intl\Locales;
4+
35
/**
46
* @see https://www.w3.org/International/articles/language-tags/
57
* @see https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
@@ -120,7 +122,7 @@ function packageDefaultLocale($page = null): string
120122

121123
/**
122124
* Returns the display name map for all supported locales.
123-
* Falls back to the locale code if not found.
125+
* Each locale name is rendered in the locale's own language using ICU data.
124126
*
125127
* Site-level overrides can be provided via a `localeNames` config key.
126128
*
@@ -129,21 +131,21 @@ function packageDefaultLocale($page = null): string
129131
*/
130132
function locale_names($page): array
131133
{
132-
static $defaults = [
133-
'en' => 'English',
134-
'cs' => 'Čeština',
135-
'fr' => 'Français',
136-
'nb-NO' => 'Norsk bokmål',
137-
'pt' => 'Português',
138-
'pt-BR' => 'Português Brasil',
139-
'ta' => 'தமிழ்',
140-
];
141-
142134
if (isset($page->localeNames) && is_array($page->localeNames)) {
143135
return $page->localeNames;
144136
}
145137

146-
return $defaults;
138+
return $page->localization->keys()
139+
->mapWithKeys(function ($locale) {
140+
// Symfony\Component\Intl\Locales uses underscores (BCP 47 with underscore)
141+
$icu = str_replace('-', '_', $locale);
142+
$name = Locales::exists($icu)
143+
? Locales::getName($icu, $icu)
144+
: $locale;
145+
146+
return [$locale => $name];
147+
})
148+
->all();
147149
}
148150

149151
/**

0 commit comments

Comments
 (0)