Home » Php » php – standard function to translate iso-639 codes to language name?

php – standard function to translate iso-639 codes to language name?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I guess there should be some standard method for this, just to avoid everybody retyping dull constants for their applications. 😉
I am looking for a function (usable in a php web app on linux) that can take two ISO639 language codes and returns the name of the first language in the second language, i.e.
foo("fr","de") should return "französisch" and foo("de","fr") should return "allemagne".

Is there?

How to&Answers:

Locale::getDisplayLanguage is what you need. It is in PHP International Extension so if it is not on you have to turn on php_intl.so (or dll if Windows).

if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    exit ('php_intl extension is available on PHP 5.3.0 or later.');
}    
if (!class_exists('Locale')) {
    exit ('You need to install php_intl extension.');
}

echo Locale::getDisplayLanguage('fr', 'de');