Completed
Push — master ( 7f1bf2...5d1be5 )
by David
02:45
created

Wordlift_Languages   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 291
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_languages() 0 16 3
B format_code_lang() 0 202 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Wordlift_Languages class
4
 *
5
 * This class provides the list of languages supported by WordLift.
6
 *
7
 * @link    https://wordlift.io
8
 *
9
 * @package Wordlift
10
 * @since   3.9.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Define the {@link Wordlift_Languages} class.
19
 *
20
 * @since 3.9.0
21
 */
22
class Wordlift_Languages {
23
24
	/**
25
	 * An array that will contain language codes => language names pairs. It gets lazily loaded the first time by the
26
	 * `get_languages` function.
27
	 *
28
	 * @since 3.9.0
29
	 * @var array|null An array of language codes => language names pairs or NULL if not initialized yet.
30
	 */
31
	private static $languages = null;
32
33
	/**
34
	 * The list of supported language codes.
35
	 *
36
	 * @since 3.9.0
37
	 *
38
	 * @var array An array of language codes.
39
	 */
40
	private static $codes = array(
41
		'be',
42
		'bg',
43
		'ca',
44
		'cs',
45
		'da',
46
		'de',
47
		'en',
48
		'es',
49
		'et',
50
		'fi',
51
		'fr',
52
		'hr',
53
		'hu',
54
		'id',
55
		'is',
56
		'it',
57
		'lt',
58
		'lv',
59
		'nl',
60
		'no',
61
		'pl',
62
		'pt',
63
		'ro',
64
		'ru',
65
		'sk',
66
		'sl',
67
		'sq',
68
		'sr',
69
		'sv',
70
		'tr',
71
		'uk',
72
		'zh',
73
	);
74
75
	/**
76
	 * Get the list of WordLift's supported languages in an array with language code => language name pairs.
77
	 *
78
	 * @since 3.9.0
79
	 *
80
	 * @return array An array with language code => language name pairs.
81
	 */
82
	public static function get_languages() {
83
84
		// Lazily load the languages.
85
		if ( null === self::$languages ) {
86
87
			// Get the language names from WP's own (multisite) function.
88
			foreach ( self::$codes as $key ) {
89
				self::$languages[ $key ] = self::format_code_lang( $key );
90
			}
91
92
			// Sort by language name.
93
			asort( self::$languages );
94
		}
95
96
		return self::$languages;
97
	}
98
99
	/**
100
	 * Returns the language for a language code. This function is a clone of WP's function provided in `ms.php`.
101
	 *
102
	 * @since 3.9.3
103
	 *
104
	 * @param string $code Optional. The two-letter language code. Default empty.
105
	 *
106
	 * @return string The language corresponding to $code if it exists. If it does not exist,
107
	 *                then the first two letters of $code is returned.
108
	 */
109
	private static function format_code_lang( $code = '' ) {
110
		$code       = strtolower( substr( $code, 0, 2 ) );
111
		$lang_codes = array(
112
			'aa' => 'Afar',
113
			'ab' => 'Abkhazian',
114
			'af' => 'Afrikaans',
115
			'ak' => 'Akan',
116
			'sq' => 'Albanian',
117
			'am' => 'Amharic',
118
			'ar' => 'Arabic',
119
			'an' => 'Aragonese',
120
			'hy' => 'Armenian',
121
			'as' => 'Assamese',
122
			'av' => 'Avaric',
123
			'ae' => 'Avestan',
124
			'ay' => 'Aymara',
125
			'az' => 'Azerbaijani',
126
			'ba' => 'Bashkir',
127
			'bm' => 'Bambara',
128
			'eu' => 'Basque',
129
			'be' => 'Belarusian',
130
			'bn' => 'Bengali',
131
			'bh' => 'Bihari',
132
			'bi' => 'Bislama',
133
			'bs' => 'Bosnian',
134
			'br' => 'Breton',
135
			'bg' => 'Bulgarian',
136
			'my' => 'Burmese',
137
			'ca' => 'Catalan; Valencian',
138
			'ch' => 'Chamorro',
139
			'ce' => 'Chechen',
140
			'zh' => 'Chinese',
141
			'cu' => 'Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic',
142
			'cv' => 'Chuvash',
143
			'kw' => 'Cornish',
144
			'co' => 'Corsican',
145
			'cr' => 'Cree',
146
			'cs' => 'Czech',
147
			'da' => 'Danish',
148
			'dv' => 'Divehi; Dhivehi; Maldivian',
149
			'nl' => 'Dutch; Flemish',
150
			'dz' => 'Dzongkha',
151
			'en' => 'English',
152
			'eo' => 'Esperanto',
153
			'et' => 'Estonian',
154
			'ee' => 'Ewe',
155
			'fo' => 'Faroese',
156
			'fj' => 'Fijjian',
157
			'fi' => 'Finnish',
158
			'fr' => 'French',
159
			'fy' => 'Western Frisian',
160
			'ff' => 'Fulah',
161
			'ka' => 'Georgian',
162
			'de' => 'German',
163
			'gd' => 'Gaelic; Scottish Gaelic',
164
			'ga' => 'Irish',
165
			'gl' => 'Galician',
166
			'gv' => 'Manx',
167
			'el' => 'Greek, Modern',
168
			'gn' => 'Guarani',
169
			'gu' => 'Gujarati',
170
			'ht' => 'Haitian; Haitian Creole',
171
			'ha' => 'Hausa',
172
			'he' => 'Hebrew',
173
			'hz' => 'Herero',
174
			'hi' => 'Hindi',
175
			'ho' => 'Hiri Motu',
176
			'hu' => 'Hungarian',
177
			'ig' => 'Igbo',
178
			'is' => 'Icelandic',
179
			'io' => 'Ido',
180
			'ii' => 'Sichuan Yi',
181
			'iu' => 'Inuktitut',
182
			'ie' => 'Interlingue',
183
			'ia' => 'Interlingua (International Auxiliary Language Association)',
184
			'id' => 'Indonesian',
185
			'ik' => 'Inupiaq',
186
			'it' => 'Italian',
187
			'jv' => 'Javanese',
188
			'ja' => 'Japanese',
189
			'kl' => 'Kalaallisut; Greenlandic',
190
			'kn' => 'Kannada',
191
			'ks' => 'Kashmiri',
192
			'kr' => 'Kanuri',
193
			'kk' => 'Kazakh',
194
			'km' => 'Central Khmer',
195
			'ki' => 'Kikuyu; Gikuyu',
196
			'rw' => 'Kinyarwanda',
197
			'ky' => 'Kirghiz; Kyrgyz',
198
			'kv' => 'Komi',
199
			'kg' => 'Kongo',
200
			'ko' => 'Korean',
201
			'kj' => 'Kuanyama; Kwanyama',
202
			'ku' => 'Kurdish',
203
			'lo' => 'Lao',
204
			'la' => 'Latin',
205
			'lv' => 'Latvian',
206
			'li' => 'Limburgan; Limburger; Limburgish',
207
			'ln' => 'Lingala',
208
			'lt' => 'Lithuanian',
209
			'lb' => 'Luxembourgish; Letzeburgesch',
210
			'lu' => 'Luba-Katanga',
211
			'lg' => 'Ganda',
212
			'mk' => 'Macedonian',
213
			'mh' => 'Marshallese',
214
			'ml' => 'Malayalam',
215
			'mi' => 'Maori',
216
			'mr' => 'Marathi',
217
			'ms' => 'Malay',
218
			'mg' => 'Malagasy',
219
			'mt' => 'Maltese',
220
			'mo' => 'Moldavian',
221
			'mn' => 'Mongolian',
222
			'na' => 'Nauru',
223
			'nv' => 'Navajo; Navaho',
224
			'nr' => 'Ndebele, South; South Ndebele',
225
			'nd' => 'Ndebele, North; North Ndebele',
226
			'ng' => 'Ndonga',
227
			'ne' => 'Nepali',
228
			'nn' => 'Norwegian Nynorsk; Nynorsk, Norwegian',
229
			'nb' => 'Bokmål, Norwegian, Norwegian Bokmål',
230
			'no' => 'Norwegian',
231
			'ny' => 'Chichewa; Chewa; Nyanja',
232
			'oc' => 'Occitan, Provençal',
233
			'oj' => 'Ojibwa',
234
			'or' => 'Oriya',
235
			'om' => 'Oromo',
236
			'os' => 'Ossetian; Ossetic',
237
			'pa' => 'Panjabi; Punjabi',
238
			'fa' => 'Persian',
239
			'pi' => 'Pali',
240
			'pl' => 'Polish',
241
			'pt' => 'Portuguese',
242
			'ps' => 'Pushto',
243
			'qu' => 'Quechua',
244
			'rm' => 'Romansh',
245
			'ro' => 'Romanian',
246
			'rn' => 'Rundi',
247
			'ru' => 'Russian',
248
			'sg' => 'Sango',
249
			'sa' => 'Sanskrit',
250
			'sr' => 'Serbian',
251
			'hr' => 'Croatian',
252
			'si' => 'Sinhala; Sinhalese',
253
			'sk' => 'Slovak',
254
			'sl' => 'Slovenian',
255
			'se' => 'Northern Sami',
256
			'sm' => 'Samoan',
257
			'sn' => 'Shona',
258
			'sd' => 'Sindhi',
259
			'so' => 'Somali',
260
			'st' => 'Sotho, Southern',
261
			'es' => 'Spanish; Castilian',
262
			'sc' => 'Sardinian',
263
			'ss' => 'Swati',
264
			'su' => 'Sundanese',
265
			'sw' => 'Swahili',
266
			'sv' => 'Swedish',
267
			'ty' => 'Tahitian',
268
			'ta' => 'Tamil',
269
			'tt' => 'Tatar',
270
			'te' => 'Telugu',
271
			'tg' => 'Tajik',
272
			'tl' => 'Tagalog',
273
			'th' => 'Thai',
274
			'bo' => 'Tibetan',
275
			'ti' => 'Tigrinya',
276
			'to' => 'Tonga (Tonga Islands)',
277
			'tn' => 'Tswana',
278
			'ts' => 'Tsonga',
279
			'tk' => 'Turkmen',
280
			'tr' => 'Turkish',
281
			'tw' => 'Twi',
282
			'ug' => 'Uighur; Uyghur',
283
			'uk' => 'Ukrainian',
284
			'ur' => 'Urdu',
285
			'uz' => 'Uzbek',
286
			've' => 'Venda',
287
			'vi' => 'Vietnamese',
288
			'vo' => 'Volapük',
289
			'cy' => 'Welsh',
290
			'wa' => 'Walloon',
291
			'wo' => 'Wolof',
292
			'xh' => 'Xhosa',
293
			'yi' => 'Yiddish',
294
			'yo' => 'Yoruba',
295
			'za' => 'Zhuang; Chuang',
296
			'zu' => 'Zulu'
297
		);
298
299
		/**
300
		 * Filters the language codes.
301
		 *
302
		 * @since MU
303
		 *
304
		 * @param array $lang_codes Key/value pair of language codes where key is the short version.
305
		 * @param string $code A two-letter designation of the language.
306
		 */
307
		$lang_codes = apply_filters( 'lang_codes', $lang_codes, $code );
308
309
		return strtr( $code, $lang_codes );
310
	}
311
312
}
313