Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:12:18+00:00 2026-06-02T22:12:18+00:00

I’m using ISO 3166-1-alpha 2 codes to pass to an application to retrieve a

  • 0

I’m using ISO 3166-1-alpha 2 codes to pass to an application to retrieve a localised feed e.g. /feeds/us for the USA. I have a switch statement which serves a feed based on that country_code.

Is there a way to convert that two digit code to the language code e.g. en_US ? I’m wondering if there is a standard / function / library for doing this in PHP or whether I need to build my own array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:12:20+00:00Added an answer on June 2, 2026 at 10:12 pm

    As other have pointed out, there is no built-in function as this likely due to the reality of many countries having multiple languages. So unfortunately, I can’t point you to a library that does this, but I did go ahead and write a little function which does what you want.

    There are two caveats, one being if it isn’t provided a language it will just pick the first locale in the list. To get around this, you’d have to put some logic around the function call to provide it with the appropriate language. The other is that it needs to have php5-intl installed.

    <?php
    
    /**
    /* Returns a locale from a country code that is provided.
    /*
    /* @param $country_code  ISO 3166-2-alpha 2 country code
    /* @param $language_code ISO 639-1-alpha 2 language code
    /* @returns  a locale, formatted like en_US, or null if not found
    /**/
    function country_code_to_locale($country_code, $language_code = '')
    {
        // Locale list taken from:
        // http://stackoverflow.com/questions/3191664/
        // list-of-all-locales-and-their-short-codes
        $locales = array('af-ZA',
                        'am-ET',
                        'ar-AE',
                        'ar-BH',
                        'ar-DZ',
                        'ar-EG',
                        'ar-IQ',
                        'ar-JO',
                        'ar-KW',
                        'ar-LB',
                        'ar-LY',
                        'ar-MA',
                        'arn-CL',
                        'ar-OM',
                        'ar-QA',
                        'ar-SA',
                        'ar-SY',
                        'ar-TN',
                        'ar-YE',
                        'as-IN',
                        'az-Cyrl-AZ',
                        'az-Latn-AZ',
                        'ba-RU',
                        'be-BY',
                        'bg-BG',
                        'bn-BD',
                        'bn-IN',
                        'bo-CN',
                        'br-FR',
                        'bs-Cyrl-BA',
                        'bs-Latn-BA',
                        'ca-ES',
                        'co-FR',
                        'cs-CZ',
                        'cy-GB',
                        'da-DK',
                        'de-AT',
                        'de-CH',
                        'de-DE',
                        'de-LI',
                        'de-LU',
                        'dsb-DE',
                        'dv-MV',
                        'el-GR',
                        'en-029',
                        'en-AU',
                        'en-BZ',
                        'en-CA',
                        'en-GB',
                        'en-IE',
                        'en-IN',
                        'en-JM',
                        'en-MY',
                        'en-NZ',
                        'en-PH',
                        'en-SG',
                        'en-TT',
                        'en-US',
                        'en-ZA',
                        'en-ZW',
                        'es-AR',
                        'es-BO',
                        'es-CL',
                        'es-CO',
                        'es-CR',
                        'es-DO',
                        'es-EC',
                        'es-ES',
                        'es-GT',
                        'es-HN',
                        'es-MX',
                        'es-NI',
                        'es-PA',
                        'es-PE',
                        'es-PR',
                        'es-PY',
                        'es-SV',
                        'es-US',
                        'es-UY',
                        'es-VE',
                        'et-EE',
                        'eu-ES',
                        'fa-IR',
                        'fi-FI',
                        'fil-PH',
                        'fo-FO',
                        'fr-BE',
                        'fr-CA',
                        'fr-CH',
                        'fr-FR',
                        'fr-LU',
                        'fr-MC',
                        'fy-NL',
                        'ga-IE',
                        'gd-GB',
                        'gl-ES',
                        'gsw-FR',
                        'gu-IN',
                        'ha-Latn-NG',
                        'he-IL',
                        'hi-IN',
                        'hr-BA',
                        'hr-HR',
                        'hsb-DE',
                        'hu-HU',
                        'hy-AM',
                        'id-ID',
                        'ig-NG',
                        'ii-CN',
                        'is-IS',
                        'it-CH',
                        'it-IT',
                        'iu-Cans-CA',
                        'iu-Latn-CA',
                        'ja-JP',
                        'ka-GE',
                        'kk-KZ',
                        'kl-GL',
                        'km-KH',
                        'kn-IN',
                        'kok-IN',
                        'ko-KR',
                        'ky-KG',
                        'lb-LU',
                        'lo-LA',
                        'lt-LT',
                        'lv-LV',
                        'mi-NZ',
                        'mk-MK',
                        'ml-IN',
                        'mn-MN',
                        'mn-Mong-CN',
                        'moh-CA',
                        'mr-IN',
                        'ms-BN',
                        'ms-MY',
                        'mt-MT',
                        'nb-NO',
                        'ne-NP',
                        'nl-BE',
                        'nl-NL',
                        'nn-NO',
                        'nso-ZA',
                        'oc-FR',
                        'or-IN',
                        'pa-IN',
                        'pl-PL',
                        'prs-AF',
                        'ps-AF',
                        'pt-BR',
                        'pt-PT',
                        'qut-GT',
                        'quz-BO',
                        'quz-EC',
                        'quz-PE',
                        'rm-CH',
                        'ro-RO',
                        'ru-RU',
                        'rw-RW',
                        'sah-RU',
                        'sa-IN',
                        'se-FI',
                        'se-NO',
                        'se-SE',
                        'si-LK',
                        'sk-SK',
                        'sl-SI',
                        'sma-NO',
                        'sma-SE',
                        'smj-NO',
                        'smj-SE',
                        'smn-FI',
                        'sms-FI',
                        'sq-AL',
                        'sr-Cyrl-BA',
                        'sr-Cyrl-CS',
                        'sr-Cyrl-ME',
                        'sr-Cyrl-RS',
                        'sr-Latn-BA',
                        'sr-Latn-CS',
                        'sr-Latn-ME',
                        'sr-Latn-RS',
                        'sv-FI',
                        'sv-SE',
                        'sw-KE',
                        'syr-SY',
                        'ta-IN',
                        'te-IN',
                        'tg-Cyrl-TJ',
                        'th-TH',
                        'tk-TM',
                        'tn-ZA',
                        'tr-TR',
                        'tt-RU',
                        'tzm-Latn-DZ',
                        'ug-CN',
                        'uk-UA',
                        'ur-PK',
                        'uz-Cyrl-UZ',
                        'uz-Latn-UZ',
                        'vi-VN',
                        'wo-SN',
                        'xh-ZA',
                        'yo-NG',
                        'zh-CN',
                        'zh-HK',
                        'zh-MO',
                        'zh-SG',
                        'zh-TW',
                        'zu-ZA',);
    
        foreach ($locales as $locale)
        {
            $locale_region = locale_get_region($locale);
            $locale_language = locale_get_primary_language($locale);
            $locale_array = array('language' => $locale_language,
                                 'region' => $locale_region);
    
            if (strtoupper($country_code) == $locale_region &&
                $language_code == '')
            {
                return locale_compose($locale_array);
            }
            elseif (strtoupper($country_code) == $locale_region &&
                    strtolower($language_code) == $locale_language)
            {
                return locale_compose($locale_array);
            }
        }
    
        return null;
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.