WPSEO_OpenGraph::locale PHP Method

locale() public method

Last update/compare with FB list done on 2015-03-16 by Rarst
See also: http://www.facebook.com/translations/FacebookLocales.xml for the list of supported locales
public locale ( boolean $echo = true ) : string
$echo boolean Whether to echo or return the locale.
return string $locale
    public function locale($echo = true)
    {
        /**
         * Filter: 'wpseo_locale' - Allow changing the locale output
         *
         * @api string $unsigned Locale string
         */
        $locale = apply_filters('wpseo_locale', get_locale());
        // Catch some weird locales served out by WP that are not easily doubled up.
        $fix_locales = array('ca' => 'ca_ES', 'en' => 'en_US', 'el' => 'el_GR', 'et' => 'et_EE', 'ja' => 'ja_JP', 'sq' => 'sq_AL', 'uk' => 'uk_UA', 'vi' => 'vi_VN', 'zh' => 'zh_CN');
        if (isset($fix_locales[$locale])) {
            $locale = $fix_locales[$locale];
        }
        // Convert locales like "es" to "es_ES", in case that works for the given locale (sometimes it does).
        if (strlen($locale) == 2) {
            $locale = strtolower($locale) . '_' . strtoupper($locale);
        }
        // These are the locales FB supports.
        $fb_valid_fb_locales = array('af_ZA', 'ak_GH', 'am_ET', 'ar_AR', 'as_IN', 'ay_BO', 'az_AZ', 'be_BY', 'bg_BG', 'bn_IN', 'br_FR', 'bs_BA', 'ca_ES', 'cb_IQ', 'ck_US', 'co_FR', 'cs_CZ', 'cx_PH', 'cy_GB', 'da_DK', 'de_DE', 'el_GR', 'en_GB', 'en_IN', 'en_PI', 'en_UD', 'en_US', 'eo_EO', 'es_CL', 'es_CO', 'es_ES', 'es_LA', 'es_MX', 'es_VE', 'et_EE', 'eu_ES', 'fa_IR', 'fb_LT', 'ff_NG', 'fi_FI', 'fo_FO', 'fr_CA', 'fr_FR', 'fy_NL', 'ga_IE', 'gl_ES', 'gn_PY', 'gu_IN', 'gx_GR', 'ha_NG', 'he_IL', 'hi_IN', 'hr_HR', 'hu_HU', 'hy_AM', 'id_ID', 'ig_NG', 'is_IS', 'it_IT', 'ja_JP', 'ja_KS', 'jv_ID', 'ka_GE', 'kk_KZ', 'km_KH', 'kn_IN', 'ko_KR', 'ku_TR', 'ky_KG', 'la_VA', 'lg_UG', 'li_NL', 'ln_CD', 'lo_LA', 'lt_LT', 'lv_LV', 'mg_MG', 'mi_NZ', 'mk_MK', 'ml_IN', 'mn_MN', 'mr_IN', 'ms_MY', 'mt_MT', 'my_MM', 'nb_NO', 'nd_ZW', 'ne_NP', 'nl_BE', 'nl_NL', 'nn_NO', 'ny_MW', 'or_IN', 'pa_IN', 'pl_PL', 'ps_AF', 'pt_BR', 'pt_PT', 'qu_PE', 'rm_CH', 'ro_RO', 'ru_RU', 'rw_RW', 'sa_IN', 'sc_IT', 'se_NO', 'si_LK', 'sk_SK', 'sl_SI', 'sn_ZW', 'so_SO', 'sq_AL', 'sr_RS', 'sv_SE', 'sw_KE', 'sy_SY', 'sz_PL', 'ta_IN', 'te_IN', 'tg_TJ', 'th_TH', 'tk_TM', 'tl_PH', 'tl_ST', 'tr_TR', 'tt_RU', 'tz_MA', 'uk_UA', 'ur_PK', 'uz_UZ', 'vi_VN', 'wo_SN', 'xh_ZA', 'yi_DE', 'yo_NG', 'zh_CN', 'zh_HK', 'zh_TW', 'zu_ZA', 'zz_TR');
        // Check to see if the locale is a valid FB one, if not, use en_US as a fallback.
        if (!in_array($locale, $fb_valid_fb_locales)) {
            $locale = strtolower(substr($locale, 0, 2)) . '_' . strtoupper(substr($locale, 0, 2));
            if (!in_array($locale, $fb_valid_fb_locales)) {
                $locale = 'en_US';
            }
        }
        if ($echo !== false) {
            $this->og_tag('og:locale', $locale);
        }
        return $locale;
    }

Usage Example

 /**
  * @covers WPSEO_OpenGraph::locale
  */
 public function test_locale()
 {
     global $locale;
     $this->assertEquals('en_US', self::$class_instance->locale(false));
     $locale = 'ca';
     $this->assertEquals('ca_ES', self::$class_instance->locale(false));
     $locale = 'nl';
     $this->assertEquals('nl_NL', self::$class_instance->locale(false));
     $locale = 'nl_NL';
     $this->assertEquals('nl_NL', self::$class_instance->locale(true));
     $this->expectOutput('<meta property="og:locale" content="nl_NL" />' . "\n");
 }