Skip to content

Instantly share code, notes, and snippets.

@heiglandreas
Created October 18, 2023 15:22
Show Gist options
  • Save heiglandreas/40d3546e9f1dae454fd42cfc7ff47df2 to your computer and use it in GitHub Desktop.
Save heiglandreas/40d3546e9f1dae454fd42cfc7ff47df2 to your computer and use it in GitHub Desktop.
<?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