Skip to content
1 change: 1 addition & 0 deletions content/mu-plugins/locale-switcher/build/index.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wp-components', 'wp-element', 'wp-i18n', 'wp-url'), 'version' => '9e7e1066364df75c678b');
1 change: 1 addition & 0 deletions content/mu-plugins/locale-switcher/build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#wpadminbar #wp-admin-bar-locale-switcher>.ab-item{cursor:pointer}#wpadminbar #wp-admin-bar-locale-switcher>.ab-item:before{content:"";top:2px}
1 change: 1 addition & 0 deletions content/mu-plugins/locale-switcher/build/style-index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#wpadminbar #wp-admin-bar-locale-switcher>.ab-item{cursor:pointer}#wpadminbar #wp-admin-bar-locale-switcher>.ab-item:before{content:"";top:2px}
135 changes: 135 additions & 0 deletions content/mu-plugins/locale-switcher/locale-switcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace WordPressdotorg\LocaleSwitcher;

use function WordPressdotorg\Locales\{ get_locales_with_native_names };

defined( 'WPINC' ) || die();

/**
* Actions and filters.
*/
add_action( 'plugins_loaded', __NAMESPACE__ . '\maybe_load', 99 );

/**
* Hook up the functionality only if the Locale Detection plugin is activated.
*
* @return void
*/
function maybe_load() {
if ( class_exists( '\WordPressdotorg\LocaleDetection\Detector' ) ) {
add_action( 'admin_bar_menu', __NAMESPACE__ . '\admin_bar_node' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_action( 'wp_print_footer_scripts', __NAMESPACE__ . '\locale_switcher_container' );
}
}

/**
* Get the list of available locales in the locale switcher.
*
* @return array
*/
function get_locale_options() {
$locales = get_locales_with_native_names();
ksort( $locales );

$locale_options = array_reduce(
array_keys( $locales ),
function( $accumulator, $key ) use ( $locales ) {
$accumulator[] = array(
'label' => $locales[ $key ],
'value' => $key,
);

return $accumulator;
},
array()
);

/**
* Filter: Modify the list of available locales in the locale switcher.
*
* @param array $locale_options Each locale is an associative array containing a `label` key and a `value` key.
*/
return apply_filters( 'wporg_locale_switcher_options', $locale_options );
}

/**
* Add a Locale node to the admin bar on the front end.
*
* @param \WP_Admin_Bar $wp_admin_bar
*
* @return void
*/
function admin_bar_node( $wp_admin_bar ) {
// This only needs to be shown on the front end.
if ( is_admin() ) {
return;
}

$all_locales = get_locales_with_native_names();
$current_locale = get_locale();

$node = array(
'id' => 'locale-switcher',
'parent' => 'top-secondary',
'title' => sprintf(
__( '<span class="screen-reader-text">Current language:</span> %s', 'wporg' ),
$all_locales[ $current_locale ]
),
'href' => '#',
);

$wp_admin_bar->add_node( $node );
}

/**
* Enqueue script and style assets.
*
* @return void
*/
function enqueue_assets() {
if ( ! is_admin_bar_showing() ) {
return;
}

$script_data = require __DIR__ . '/build/index.asset.php';

wp_enqueue_style(
'wporg-locale-switcher',
plugins_url( 'build/style-index.css', __FILE__ ),
array( 'wp-components' ),
$script_data['version'],
'screen'
);

wp_enqueue_script(
'wporg-locale-switcher',
plugins_url( 'build/index.js', __FILE__ ),
$script_data['dependencies'],
$script_data['version'],
array( 'strategy' => 'defer' )
);

$locale_options = get_locale_options();

$locale_config = array(
'initialValue' => get_locale(),
'options' => $locale_options,
);

wp_add_inline_script(
'wporg-locale-switcher',
'var wporgLocaleSwitcherConfig = ' . wp_json_encode( $locale_config ) . ';',
'before'
);
}

/**
* Render a container for the locale switcher.
*
* @return void
*/
function locale_switcher_container() {
echo '<div id="wporg-locale-switcher-container"></div>';
}
100 changes: 100 additions & 0 deletions content/mu-plugins/locales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Plugin Name: GlotPress/WordPress Locales
* Description: Defines <code>GP_Locale</code> and <code>GP_Locales</code> and extends them with custom locales used throughout wordpress.org.
* License: GPLv2 or later
*/

namespace {
require_once __DIR__ . '/locales/locales.php';
require_once __DIR__ . '/locale-switcher/locale-switcher.php';
}

namespace WordPressdotorg\Locales {

use GP_Locales, GP_Locale;

/**
* Sets available languages to all possible locales.
*/
function set_available_languages() {
static $locales;

if ( ! isset( $locales ) ) {
$locales = GP_Locales::locales();
$locales = array_column( $locales, 'wp_locale' );
$locales = array_filter( $locales );
}

return $locales;
}
add_filter( 'get_available_languages', __NAMESPACE__ . '\set_available_languages', 10, 0 );

/**
* Retrieves all available locales.
*
* @return GP_Locale[] Array of locale objects.
*/
function get_locales() {
wp_cache_add_global_groups( array( 'locale-associations' ) );

$wp_locales = wp_cache_get( 'locale-list', 'locale-associations' );
if ( false === $wp_locales ) {
$wp_locales = (array) $GLOBALS['wpdb']->get_col( 'SELECT locale FROM wporg_locales' );
wp_cache_set( 'locale-list', $wp_locales, 'locale-associations' );
}

$wp_locales[] = 'en_US';
$locales = array();

foreach ( $wp_locales as $locale ) {
$gp_locale = GP_Locales::by_field( 'wp_locale', $locale );
if ( ! $gp_locale ) {
continue;
}

$locales[ $locale ] = $gp_locale;
}

natsort( $locales );

return $locales;
}

/**
* Get an array of locales with the locale code as key and the native name as value.
*
* @return array
*/
function get_locales_with_native_names() {
$locales = get_locales();

return wp_list_pluck( $locales, 'native_name', 'wp_locale' );
}

/**
* Get an array of locales with the locale code as key and the English name as value.
*
* @return array
*/
function get_locales_with_english_names() {
$locales = get_locales();

return wp_list_pluck( $locales, 'english_name', 'wp_locale' );
}

/**
* Get the name of a locale from the code.
*
* @param string $code The locale code to look up. E.g. en_US.
* @param string $name_type Optional. 'native' or 'english'. Default 'native'.
*
* @return mixed|string
*/
function get_locale_name_from_code( $code, $name_type = 'native' ) {
$function = __NAMESPACE__ . "\get_locales_with_{$name_type}_names";
$locales = $function();

return $locales[ $code ] ?? '';
}
}
Loading