WordPress locale, where it is not simply xx_XX. */ const LOCALES = array( 'en' => 'en_US', 'ar' => 'ar', 'zh-CN' => 'zh_CN', 'zh-TW' => 'zh_TW', 'ja' => 'ja', 'ko' => 'ko_KR', 'he' => 'he_IL', 'iw' => 'he_IL', 'hi' => 'hi_IN', 'ur' => 'ur', 'fa' => 'fa_IR', 'el' => 'el', 'sv' => 'sv_SE', 'da' => 'da_DK', 'nb' => 'nb_NO', 'no' => 'nb_NO', 'cs' => 'cs_CZ', 'uk' => 'uk', 'vi' => 'vi', 'pt' => 'pt_PT', 'ca' => 'ca', 'eu' => 'eu', 'gl' => 'gl_ES', 'et' => 'et', 'fi' => 'fi', 'th' => 'th', 'sq' => 'sq', 'hy' => 'hy', 'az' => 'az', 'bn' => 'bn_BD', 'bs' => 'bs_BA', 'cy' => 'cy', 'ka' => 'ka_GE', 'kk' => 'kk', 'km' => 'km', 'lo' => 'lo', 'ms' => 'ms_MY', 'my' => 'my_MM', 'ne' => 'ne_NP', 'sl' => 'sl_SI', 'sr' => 'sr_RS', 'sw' => 'sw', 'ta' => 'ta_IN', 'te' => 'te', 'tl' => 'tl', 'ga' => 'ga', 'ps' => 'ps', 'eo' => 'eo', ); /** @var Settings */ private static $settings; /** @var string|null */ private static $current = null; public static function init( Settings $settings ): void { self::$settings = $settings; } public static function source(): string { return (string) self::$settings->get( 'source_language', 'en' ); } /** @return string[] Target language codes in switcher order. */ public static function targets(): array { return (array) self::$settings->get( 'languages', array() ); } /** @return string[] Source first, then targets. */ public static function all(): array { return array_merge( array( self::source() ), self::targets() ); } public static function is_enabled( string $code ): bool { return in_array( $code, self::all(), true ); } public static function is_target( string $code ): bool { return in_array( $code, self::targets(), true ); } public static function current(): string { return self::$current ?: self::source(); } public static function set_current( string $code ): void { self::$current = self::is_enabled( $code ) ? $code : self::source(); } public static function is_source_current(): bool { return self::current() === self::source(); } /** URL slug for a language, e.g. "zh-CN" => "zh-cn". */ public static function slug( string $code ): string { return strtolower( $code ); } public static function from_slug( string $slug ): string { $slug = strtolower( $slug ); foreach ( self::all() as $code ) { if ( self::slug( $code ) === $slug ) { return $code; } } return ''; } public static function locale( string $code ): string { if ( isset( self::LOCALES[ $code ] ) ) { return self::LOCALES[ $code ]; } if ( strpos( $code, '-' ) ) { list( $l, $r ) = explode( '-', $code, 2 ); return strtolower( $l ) . '_' . strtoupper( $r ); } return strtolower( $code ) . '_' . strtoupper( $code ); } /** BCP-47 tag for hreflang / html lang, e.g. "zh-CN", "fr". */ public static function bcp47( string $code ): string { return str_replace( '_', '-', $code ); } public static function is_rtl( string $code ): bool { return in_array( strtolower( strtok( $code, '-' ) ), self::RTL, true ); } /** * Display names for codes, in their own language where known (autonyms). */ public static function name( string $code, bool $native = false ): string { $names = self::names(); if ( $native && class_exists( '\Locale' ) ) { $native_name = \Locale::getDisplayLanguage( str_replace( '-', '_', $code ), str_replace( '-', '_', $code ) ); if ( $native_name && strtolower( $native_name ) !== strtolower( $code ) ) { return function_exists( 'mb_convert_case' ) ? mb_convert_case( $native_name, MB_CASE_TITLE, 'UTF-8' ) : ucfirst( $native_name ); } } return $names[ $code ] ?? strtoupper( $code ); } /** @return array code => English name. */ public static function names(): array { $cached = get_transient( 'translateplus_languages' ); if ( is_array( $cached ) && $cached ) { return $cached; } return array( 'ar' => 'Arabic', 'zh-CN' => 'Chinese (Simplified)', 'zh-TW' => 'Chinese (Traditional)', 'nl' => 'Dutch', 'en' => 'English', 'fr' => 'French', 'de' => 'German', 'hi' => 'Hindi', 'it' => 'Italian', 'ja' => 'Japanese', 'ko' => 'Korean', 'pl' => 'Polish', 'pt' => 'Portuguese', 'ru' => 'Russian', 'es' => 'Spanish', 'tr' => 'Turkish', 'ur' => 'Urdu', ); } }