Created
October 18, 2023 15:22
-
-
Save heiglandreas/40d3546e9f1dae454fd42cfc7ff47df2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Test; | |
enum Locale: string | |
{ | |
case en_GB = 'en_GB'; | |
case de_DE = 'de_DE'; | |
case fr_CA = 'fr_CA'; | |
public static function fromLocaleString(string $locale): self | |
{ | |
$parts = \Locale::parseLocale($locale); | |
$myOne = $parts['language'] . '_' . strtoupper($parts['region']); | |
return self::from($myOne); | |
} | |
} | |
foreach ([ | |
'de-DE', | |
'de-CH', | |
'en-US', | |
'fr_CA.ISO-8895-1@euro', | |
'fr-CA-u-ca-gregorian-nu-arab', | |
] as $localeString) { | |
try { | |
$locale = Locale::fromLocaleString($localeString); | |
echo $locale->value . ' selected' . PHP_EOL; | |
} catch (\Throwable) { | |
echo 'Invalid Locale ' . $localeString . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment