Skip to content

Commit

Permalink
Update language-functions.php
Browse files Browse the repository at this point in the history
  • Loading branch information
takayukister committed May 7, 2023
1 parent 918317f commit 0294338
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions includes/language-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 );
Expand All @@ -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]+))?)?$/';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;

Expand Down Expand Up @@ -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://';
Expand Down

0 comments on commit 0294338

Please sign in to comment.