I’m posting this at the request of one of our Discord users, who doesn’t use WPSE:
Stack Overflow / Stack Exchange is kind of a bizarre place. Why is
that question voted “-2”? The question is basically asking more or
less the same thing that multiple WordPress tickets are asking for:https://core.trac.wordpress.org/ticket/48152
https://core.trac.wordpress.org/ticket/50183
Also the W3C itself seems to recommend the same thing (avoiding the
region in most cases):https://www.w3.org/International/articles/language-tags/
https://www.w3.org/International/questions/qa-choosing-language-tags
But somehow the question offended the WordPress techbros, so it got
voted -2To answer the question: WordPress does sort of have a way of getting
this, but it’s awfully obscure:https://api.wordpress.org/translations/core/1.0/
If you pretty-print that, the entries look like this:
{ "language": "en_GB", "version": "6.4.1", "updated": "2023-11-24 12:26:36", "english_name": "English (UK)", "native_name": "English (UK)", "package": "https://downloads.wordpress.org/translation/core/6.4.1/en_GB.zip", "iso": { "1": "en", "2": "eng", "3": "eng" }, "strings": { "continue": "Continue" } },
The “iso” property contains the 2-letter and/or 3-letter language
codes, without any region code. (I’m not sure why “eng” is there
twice.) WordPress itself uses this for the lang=”…” attribute in the
language dropdown on the page wp-admin/options-general.php Also
“en_US” is a special case (since it has no translation) and is just
hard-coded as lang=”en”:https://github.com/WordPress/WordPress/blob/6.4.1/wp-includes/l10n.php#L1616