From 02943383672ae103591f5ef072433a2fe5aa5d4f Mon Sep 17 00:00:00 2001 From: Takayuki Miyoshi Date: Sun, 7 May 2023 15:54:37 +0900 Subject: [PATCH] Update language-functions.php #251 --- includes/language-functions.php | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/includes/language-functions.php b/includes/language-functions.php index 02f9336..d3a5d88 100644 --- a/includes/language-functions.php +++ b/includes/language-functions.php @@ -629,6 +629,10 @@ function bogo_locale_is_alone( $locale ) { return strlen( $slug ) < strlen( $tag ); } + +/** + * Retrieves the short version of the specified language name. + */ function bogo_get_short_name( $orig_name ) { $short_name = $orig_name = (string) $orig_name; @@ -655,6 +659,11 @@ function bogo_get_short_name( $orig_name ) { return trim( $short_name ); } + +/** + * Retrieves the regular expression pattern that matches + * all available language slugs on this site. + */ function bogo_get_lang_regex() { $langs = array_map( 'bogo_lang_slug', bogo_available_locales() ); $langs = array_filter( $langs ); @@ -666,6 +675,14 @@ function bogo_get_lang_regex() { return '(' . implode( '|', $langs ) . ')'; } + +/** + * Retrieves the locale that is active on this site and + * closest to the specified locale. + * + * @param string $locale_orig Locale code. + * @return string|bool Locale code. False if there is no close locale. + */ function bogo_get_closest_locale( $locale_orig ) { $locale_orig = strtolower( $locale_orig ); $locale_pattern = '/^([a-z]{2,3})(?:[_-]([a-z]{2})(?:[_-]([a-z0-9]+))?)?$/'; @@ -712,6 +729,13 @@ function bogo_get_closest_locale( $locale_orig ) { return false; } + +/** + * Returns an ordered list of language tags based on the client + * language preference. + * + * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language + */ function bogo_http_accept_languages() { if ( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { return false; @@ -743,6 +767,10 @@ function bogo_http_accept_languages() { return array_reverse( array_keys( $languages ) ); } + +/** + * A wrapper function of bogo_get_url_with_lang(). + */ function bogo_url( $url = null, $lang = null ) { if ( ! $lang ) { $lang = determine_locale(); @@ -755,6 +783,15 @@ function bogo_url( $url = null, $lang = null ) { return bogo_get_url_with_lang( $url, $lang, $args ); } + +/** + * Returns a URL that is a different language version of the original URL. + * + * @param string $url The original URL. + * @param string $lang Locale code. + * @param string|array $args Options. + * @return string The result URL. + */ function bogo_get_url_with_lang( $url = null, $lang = null, $args = '' ) { global $wp_rewrite; @@ -864,6 +901,12 @@ function bogo_get_url_with_lang( $url = null, $lang = null, $args = '' ) { return $url; } + +/** + * Determines the language from the specified URL. + * + * @param string $url URL. + */ function bogo_get_lang_from_url( $url = '' ) { if ( ! $url ) { $url = is_ssl() ? 'https://' : 'http://';