I have the following code, which I believe is supposed to output fr_FR as the locale, but for some reason it outputs en_US_POSIX (regardless of time zone). What did i do wrong?
$loc = IntlCalendar::createInstance(new DateTimeZone('Europe/Paris')); echo $loc->getLocale(Locale::VALID_LOCALE);
Reference links: https://www.php.net/manual/en/intlcalendar.createinstance.php and https://www.php.net/manual/en/intlcalendar.getlocale.php
;
Looks like this isn't the right approach (although the code is valid) - is there a more suitable way to find the "default" locale for a given time zone?
You set your time zone to that of Paris. But you didn't set the locale. They are different things. The locale defines the language and formatting conventions, while the time zone sets the rules for converting UTC to local time and back again. What you define applies to Americans in Paris. This is a valid use case, especially in August!
Please try the following code:
$loc = IntlCalendar::createInstance( new DateTimeZone( 'Europe/Paris' ), 'fr_FR' ); echo $loc->getLocale( Locale::VALID_LOCALE );
You can start by getting the associated country (and country code) from a given time zone:
You can then combine this information with the resources available in the ICU library to get the most likely language for a given country code:
Please note that this does not apply to every user. This is a good default starting point, but you should always ask the user for their language preference.
$possibleLocale = getLanguage($countryCode) . '_' . $countryCode; // fr_FR