Elgg  Version 5.1
LocaleService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\I18n;
4 
5 use Elgg\Config;
6 
14 
18  protected $config;
19 
23  protected $locale;
24 
30  public function __construct(Config $config) {
31  $this->config = $config;
32 
33  $this->initializeElggLocale();
34  }
35 
41  public function getLanguageCodes() {
42  return array_keys($this->locale);
43  }
44 
53  public function setLocaleForLanguage(string $language, array $locale = []) {
54  $this->locale[$language] = $locale;
55  }
56 
64  public function getLocaleForLanguage(string $language) {
65  return $this->locale[$language] ?? [];
66  }
67 
75  public function getLocale(int $category) {
76  return (array) setlocale($category, 0);
77  }
78 
90  public function setLocale(int $category, $locale) {
91  $current = $this->getLocale($category);
92 
93  setlocale($category, $locale);
94 
95  return $current;
96  }
97 
106  public function setLocaleFromLanguageKey(int $category, string $language = null) {
107  if (!isset($language)) {
109  }
110 
111  $locale = [
112  $language,
113  ];
115 
116  return $this->setLocale($category, $locale);
117  }
118 
128  private function initializeElggLocale() {
129  $this->locale = [
130  'aa' => [], // 'Afar'
131  'ab' => [], // 'Abkhazian'
132  'af' => [], // 'Afrikaans'
133  'am' => [], // 'Amharic'
134  'ar' => [], // 'Arabic'
135  'as' => [], // 'Assamese'
136  'ay' => [], // 'Aymara'
137  'az' => [], // 'Azerbaijani'
138  'ba' => [], // 'Bashkir'
139  'be' => [], // 'Byelorussian'
140  'bg' => [], // 'Bulgarian'
141  'bh' => [], // 'Bihari'
142  'bi' => [], // 'Bislama'
143  'bn' => [], // 'Bengali; Bangla'
144  'bo' => [], // 'Tibetan'
145  'br' => [], // 'Breton'
146  'ca' => [], // 'Catalan'
147  'cmn' => [], // 'Mandarin Chinese' // ISO 639-3
148  'co' => [], // 'Corsican'
149  'cs' => [], // 'Czech'
150  'cy' => [], // 'Welsh'
151  'da' => [], // 'Danish'
152  'de' => [], // 'German'
153  'dz' => [], // 'Bhutani'
154  'el' => [], // 'Greek'
155  'en' => [], // 'English'
156  'eo' => [], // 'Esperanto'
157  'es' => [], // 'Spanish'
158  'et' => [], // 'Estonian'
159  'eu' => [], // 'Basque'
160  'eu_es' => [], // 'Basque (Spain)'
161  'fa' => [], // 'Persian'
162  'fi' => [], // 'Finnish'
163  'fj' => [], // 'Fiji'
164  'fo' => [], // 'Faeroese'
165  'fr' => [], // 'French'
166  'fy' => [], // 'Frisian'
167  'ga' => [], // 'Irish'
168  'gd' => [], // 'Scots / Gaelic'
169  'gl' => [], // 'Galician'
170  'gn' => [], // 'Guarani'
171  'gu' => [], // 'Gujarati'
172  'he' => [], // 'Hebrew'
173  'ha' => [], // 'Hausa'
174  'hi' => [], // 'Hindi'
175  'hr' => [], // 'Croatian'
176  'hu' => [], // 'Hungarian'
177  'hy' => [], // 'Armenian'
178  'ia' => [], // 'Interlingua'
179  'id' => [], // 'Indonesian'
180  'ie' => [], // 'Interlingue'
181  'ik' => [], // 'Inupiak'
182  'is' => [], // 'Icelandic'
183  'it' => [], // 'Italian'
184  'iu' => [], // 'Inuktitut'
185  'iw' => [], // 'Hebrew (obsolete)'
186  'ja' => [], // 'Japanese'
187  'ji' => [], // 'Yiddish (obsolete)'
188  'jw' => [], // 'Javanese'
189  'ka' => [], // 'Georgian'
190  'kk' => [], // 'Kazakh'
191  'kl' => [], // 'Greenlandic'
192  'km' => [], // 'Cambodian'
193  'kn' => [], // 'Kannada'
194  'ko' => [], // 'Korean'
195  'ks' => [], // 'Kashmiri'
196  'ku' => [], // 'Kurdish'
197  'ky' => [], // 'Kirghiz'
198  'la' => [], // 'Latin'
199  'ln' => [], // 'Lingala'
200  'lo' => [], // 'Laothian'
201  'lt' => [], // 'Lithuanian'
202  'lv' => [], // 'Latvian/Lettish'
203  'mg' => [], // 'Malagasy'
204  'mi' => [], // 'Maori'
205  'mk' => [], // 'Macedonian'
206  'ml' => [], // 'Malayalam'
207  'mn' => [], // 'Mongolian'
208  'mo' => [], // 'Moldavian'
209  'mr' => [], // 'Marathi'
210  'ms' => [], // 'Malay'
211  'mt' => [], // 'Maltese'
212  'my' => [], // 'Burmese'
213  'na' => [], // 'Nauru'
214  'ne' => [], // 'Nepali'
215  'nl' => [], // 'Dutch'
216  'no' => [], // 'Norwegian'
217  'oc' => [], // 'Occitan'
218  'om' => [], // '(Afan) Oromo'
219  'or' => [], // 'Oriya'
220  'pa' => [], // 'Punjabi'
221  'pl' => [], // 'Polish'
222  'ps' => [], // 'Pashto / Pushto'
223  'pt' => [], // 'Portuguese'
224  'pt_br' => [], // 'Portuguese (Brazil)'
225  'qu' => [], // 'Quechua'
226  'rm' => [], // 'Rhaeto-Romance'
227  'rn' => [], // 'Kirundi'
228  'ro' => [], // 'Romanian'
229  'ro_ro' => [], // 'Romanian (Romania)'
230  'ru' => [], // 'Russian'
231  'rw' => [], // 'Kinyarwanda'
232  'sa' => [], // 'Sanskrit'
233  'sd' => [], // 'Sindhi'
234  'sg' => [], // 'Sangro'
235  'sh' => [], // 'Serbo-Croatian'
236  'si' => [], // 'Singhalese'
237  'sk' => [], // 'Slovak'
238  'sl' => [], // 'Slovenian'
239  'sm' => [], // 'Samoan'
240  'sn' => [], // 'Shona'
241  'so' => [], // 'Somali'
242  'sq' => [], // 'Albanian'
243  'sr' => [], // 'Serbian'
244  'sr_latin' => [], // 'Serbian (Latin)'
245  'ss' => [], // 'Siswati'
246  'st' => [], // 'Sesotho'
247  'su' => [], // 'Sundanese'
248  'sv' => [], // 'Swedish'
249  'sw' => [], // 'Swahili'
250  'ta' => [], // 'Tamil'
251  'te' => [], // 'Tegulu'
252  'tg' => [], // 'Tajik'
253  'th' => [], // 'Thai'
254  'ti' => [], // 'Tigrinya'
255  'tk' => [], // 'Turkmen'
256  'tl' => [], // 'Tagalog'
257  'tn' => [], // 'Setswana'
258  'to' => [], // 'Tonga'
259  'tr' => [], // 'Turkish'
260  'ts' => [], // 'Tsonga'
261  'tt' => [], // 'Tatar'
262  'tw' => [], // 'Twi'
263  'ug' => [], // 'Uigur'
264  'uk' => [], // 'Ukrainian'
265  'ur' => [], // 'Urdu'
266  'uz' => [], // 'Uzbek'
267  'vi' => [], // 'Vietnamese'
268  'vo' => [], // 'Volapuk'
269  'wo' => [], // 'Wolof'
270  'xh' => [], // 'Xhosa'
271  'yi' => [], // 'Yiddish'
272  'yo' => [], // 'Yoruba'
273  'za' => [], // 'Zuang'
274  'zh' => [], // 'Chinese'
275  'zh_hans' => [], // 'Chinese Simplified'
276  'zu' => [], // 'Zulu'
277  ];
278 
279  // merge $CONFIG mapping into the array
280  // this can be set in settings.php
281  if (is_array($this->config->language_to_locale_mapping)) {
282  $this->locale = array_merge($this->locale, $this->config->language_to_locale_mapping);
283  }
284  }
285 }
elgg_get_current_language()
Get the current system/user language or &#39;en&#39;.
Definition: languages.php:27
getLocaleForLanguage(string $language)
Returns the configured locale settings for a given language key.
setLocaleForLanguage(string $language, array $locale=[])
Set the locale settings for a language key.
setLocale(int $category, $locale)
Set the system locale to a new value, return the locale before the change.
setLocaleFromLanguageKey(int $category, string $language=null)
Set the locale from a language key.
getLanguageCodes()
Returns a list of supported laguage codes.
__construct(Config $config)
Create new service.
Provides locale related features.
getLocale(int $category)
Get the current locale.
$language
Definition: useradd.php:17