-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlocales.php
More file actions
100 lines (81 loc) · 2.47 KB
/
locales.php
File metadata and controls
100 lines (81 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 ] ?? '';
}
}