l10n.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Determine the current locale desired for the request. |
| 5 | * |
| 6 | * @since 5.0.0 |
| 7 | * |
| 8 | * @global string $pagenow |
| 9 | * |
| 10 | * @return string The determined locale. |
| 11 | */ |
| 12 | if ( ! function_exists( 'determine_locale' ) ) : |
| 13 | function determine_locale() { |
| 14 | /** |
| 15 | * Filters the locale for the current request prior to the default determination process. |
| 16 | * |
| 17 | * Using this filter allows to override the default logic, effectively short-circuiting the function. |
| 18 | * |
| 19 | * @since 5.0.0 |
| 20 | * |
| 21 | * @param string|null The locale to return and short-circuit, or null as default. |
| 22 | */ |
| 23 | $determined_locale = apply_filters( 'pre_determine_locale', null ); |
| 24 | if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) { |
| 25 | return $determined_locale; |
| 26 | } |
| 27 | |
| 28 | $determined_locale = get_locale(); |
| 29 | |
| 30 | if ( function_exists( 'get_user_locale' ) && is_admin() ) { |
| 31 | $determined_locale = get_user_locale(); |
| 32 | } |
| 33 | |
| 34 | if ( function_exists( 'get_user_locale' ) && isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] ) { |
| 35 | $determined_locale = get_user_locale(); |
| 36 | } |
| 37 | |
| 38 | if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { |
| 39 | $determined_locale = sanitize_text_field( $_GET['wp_lang'] ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Filters the locale for the current request. |
| 44 | * |
| 45 | * @since 5.0.0 |
| 46 | * |
| 47 | * @param string $locale The locale. |
| 48 | */ |
| 49 | return apply_filters( 'determine_locale', $determined_locale ); |
| 50 | } |
| 51 | endif; |
| 52 | |
| 53 | /* |
| 54 | * acf_get_locale |
| 55 | * |
| 56 | * Returns the current locale. |
| 57 | * |
| 58 | * @date 16/12/16 |
| 59 | * @since 5.5.0 |
| 60 | * |
| 61 | * @param void |
| 62 | * @return string |
| 63 | */ |
| 64 | function acf_get_locale() { |
| 65 | |
| 66 | // Determine local. |
| 67 | $locale = determine_locale(); |
| 68 | |
| 69 | // Fallback to parent language for regions without translation. |
| 70 | // https://wpastra.com/docs/complete-list-wordpress-locale-codes/ |
| 71 | $langs = array( |
| 72 | 'az_TR' => 'az', // Azerbaijani (Turkey) |
| 73 | 'zh_HK' => 'zh_TW', // Chinese (Hong Kong) |
| 74 | 'nl_BE' => 'nl_NL', // Dutch (Belgium) |
| 75 | 'fr_BE' => 'fr_FR', // French (Belgium) |
| 76 | 'nn_NO' => 'nb_NO', // Norwegian (Nynorsk) |
| 77 | 'fa_AF' => 'fa_IR', // Persian (Afghanistan) |
| 78 | 'ru_UA' => 'ru_RU', // Russian (Ukraine) |
| 79 | ); |
| 80 | if ( isset( $langs[ $locale ] ) ) { |
| 81 | $locale = $langs[ $locale ]; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Filters the determined local. |
| 86 | * |
| 87 | * @date 8/1/19 |
| 88 | * @since 5.7.10 |
| 89 | * |
| 90 | * @param string $locale The local. |
| 91 | */ |
| 92 | return apply_filters( 'acf/get_locale', $locale ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * acf_load_textdomain |
| 97 | * |
| 98 | * Loads the plugin's translated strings similar to load_plugin_textdomain(). |
| 99 | * |
| 100 | * @date 8/1/19 |
| 101 | * @since 5.7.10 |
| 102 | * |
| 103 | * @param string $locale The plugin's current locale. |
| 104 | * @return void |
| 105 | */ |
| 106 | function acf_load_textdomain( $domain = 'acf' ) { |
| 107 | |
| 108 | /** |
| 109 | * Filters a plugin's locale. |
| 110 | * |
| 111 | * @date 8/1/19 |
| 112 | * @since 5.7.10 |
| 113 | * |
| 114 | * @param string $locale The plugin's current locale. |
| 115 | * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
| 116 | */ |
| 117 | $locale = apply_filters( 'plugin_locale', acf_get_locale(), $domain ); |
| 118 | $mofile = $domain . '-' . $locale . '.mo'; |
| 119 | |
| 120 | // Try to load from the languages directory first. |
| 121 | if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // Load from plugin lang folder. |
| 126 | return load_textdomain( $domain, acf_get_path( 'lang/' . $mofile ) ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * _acf_apply_language_cache_key |
| 131 | * |
| 132 | * Applies the current language to the cache key. |
| 133 | * |
| 134 | * @date 23/1/19 |
| 135 | * @since 5.7.11 |
| 136 | * |
| 137 | * @param string $key The cache key. |
| 138 | * @return string |
| 139 | */ |
| 140 | function _acf_apply_language_cache_key( $key ) { |
| 141 | |
| 142 | // Get current language. |
| 143 | $current_language = acf_get_setting( 'current_language' ); |
| 144 | if ( $current_language ) { |
| 145 | $key = "{$key}:{$current_language}"; |
| 146 | } |
| 147 | |
| 148 | // Return key. |
| 149 | return $key; |
| 150 | } |
| 151 | |
| 152 | // Hook into filter. |
| 153 | add_filter( 'acf/get_cache_key', '_acf_apply_language_cache_key' ); |
| 154 |