Basically I have a lot of language codes it, en, en-GB, de, de-CH, and so on...
and from these I need to get a full locale code format: LANGCODE-COUNTRYCODE
with the default country of the language if the country code is not already specified.
An example of what I mean/need:
INPUT OUTPUT
it -> it-IT
it-IT -> it-IT
en-GB -> en-GB
en -> en-US
es-AR -> es-AR
es-MX -> es-MX
es -> es-ES
is there any library I’m unaware of or a simple way of achieving this in PHP?
I’ve tried finding solutions on google a lot but either it doesn’t exist or I’m just using the wrong keywords…
Do I really have to make a manual array of this by hand? there must be a better way, I’m sure!
2
Answers
Thanks to the help of iso.org and localeplanet.com plus some good old googling and a lot of elbow grease, I came up with this list below. It might not be perfect, but it will do the job for me... I Hope it can be of help to others!
If you have any suggestion on how I might emproove it, feel free to comment below!
The rules you describe in your follow-up comment are nothing but your subjective opinion about what countries are "more important" in regards of a given language. You pick Spain arguing Spanish is originally from there, but then pick United States for English (German is not mentioned, but it probably pre-dates Germany itself). You won’t find an algorithm for such ruleset, so there’s no other way than composing our own hard-coded list. Once you do that, the PHP portion can be as simple as an array lookup:
Demo