ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CldrData.php
Go to the documentation of this file.
1 <?php
2 namespace Gettext\Languages;
3 
4 use Exception;
5 
9 class CldrData
10 {
15  const OTHER_CATEGORY = 'other';
20  public static $categories = array('zero', 'one', 'two', 'few', 'many', self::OTHER_CATEGORY);
25  private static $data;
30  private static function getData($key)
31  {
32  if (!isset(self::$data)) {
33  $fixKeys = function ($list, &$standAlone = null) {
34  $result = array();
35  $standAlone = array();
36  $match = null;
37  foreach ($list as $key => $value) {
38  $variant = '';
39  if (preg_match('/^(.+)-alt-(short|variant|stand-alone|long)$/', $key, $match)) {
40  $key = $match[1];
41  $variant = $match[2];
42  }
43  $key = str_replace('-', '_', $key);
44  switch ($key) {
45  case 'root': // Language: Root
46  case 'und': // Language: Unknown Language
47  case 'zxx': // Language: No linguistic content
48  case 'ZZ': // Territory: Unknown Region
49  case 'Zinh': // Script: Inherited
50  case 'Zmth': // Script: Mathematical Notation
51  case 'Zsym': // Script: Symbols
52  case 'Zxxx': // Script: Unwritten
53  case 'Zyyy': // Script: Common
54  case 'Zzzz': // Script: Unknown Script
55  break;
56  default:
57  switch ($variant) {
58  case 'stand-alone':
59  $standAlone[$key] = $value;
60  break;
61  case '':
62  $result[$key] = $value;
63  break;
64  }
65  break;
66  }
67  }
68 
69  return $result;
70  };
71  $data = array();
72  $json = json_decode(file_get_contents(__DIR__.'/cldr-data/main/en-US/languages.json'), true);
73  $data['languages'] = $fixKeys($json['main']['en-US']['localeDisplayNames']['languages']);
74  $json = json_decode(file_get_contents(__DIR__.'/cldr-data/main/en-US/territories.json'), true);
75  $data['territories'] = $fixKeys($json['main']['en-US']['localeDisplayNames']['territories']);
76  $json = json_decode(file_get_contents(__DIR__.'/cldr-data/supplemental/plurals.json'), true);
77  $data['plurals'] = $fixKeys($json['supplemental']['plurals-type-cardinal']);
78  $json = json_decode(file_get_contents(__DIR__.'/cldr-data/main/en-US/scripts.json'), true);
79  $data['scripts'] = $fixKeys($json['main']['en-US']['localeDisplayNames']['scripts'], $data['standAloneScripts']);
80  $data['standAloneScripts'] = array_merge($data['scripts'], $data['standAloneScripts']);
81  $data['scripts'] = array_merge($data['standAloneScripts'], $data['scripts']);
82  $data['supersededLanguages'] = array();
83  // Remove the languages for which we don't have plurals
84  $m = null;
85  foreach (array_keys(array_diff_key($data['languages'], $data['plurals'])) as $missingPlural) {
86  if (preg_match('/^([a-z]{2,3})_/', $missingPlural, $m)) {
87  if (!isset($data['plurals'][$m[1]])) {
88  unset($data['languages'][$missingPlural]);
89  }
90  } else {
91  unset($data['languages'][$missingPlural]);
92  }
93  }
94  // Fix the languages for which we have plurals
95  $formerCodes = array(
96  'in' => 'id', // former Indonesian
97  'iw' => 'he', // former Hebrew
98  'ji' => 'yi', // former Yiddish
99  'jw' => 'jv', // former Javanese
100  'mo' => 'ro_MD', // former Moldavian
101  );
102  $knownMissingLanguages = array(
103  'bh' => 'Bihari',
104  'guw' => 'Gun',
105  'nah' => 'Nahuatl',
106  'smi' => 'Sami',
107  );
108  foreach (array_keys(array_diff_key($data['plurals'], $data['languages'])) as $missingLanguage) {
109  if (isset($formerCodes[$missingLanguage]) && isset($data['languages'][$formerCodes[$missingLanguage]])) {
110  $data['languages'][$missingLanguage] = $data['languages'][$formerCodes[$missingLanguage]];
111  $data['supersededLanguages'][$missingLanguage] = $formerCodes[$missingLanguage];
112  } else {
113  if (isset($knownMissingLanguages[$missingLanguage])) {
114  $data['languages'][$missingLanguage] = $knownMissingLanguages[$missingLanguage];
115  } else {
116  throw new Exception("We have the plural rule for the language '$missingLanguage' but we don't have its language name");
117  }
118  }
119  }
120  ksort($data['languages'], SORT_STRING);
121  ksort($data['territories'], SORT_STRING);
122  ksort($data['plurals'], SORT_STRING);
123  ksort($data['scripts'], SORT_STRING);
124  ksort($data['standAloneScripts'], SORT_STRING);
125  ksort($data['supersededLanguages'], SORT_STRING);
126  self::$data = $data;
127  }
128  if (!@isset(self::$data[$key])) {
129  throw new Exception("Invalid CLDR data key: '$key'");
130  }
131 
132  return self::$data[$key];
133  }
140  public static function getLanguageNames()
141  {
142  return self::getData('languages');
143  }
150  public static function getTerritoryNames()
151  {
152  return self::getData('territories');
153  }
161  public static function getScriptNames($standAlone)
162  {
163  return self::getData($standAlone ? 'standAloneScripts' : 'scripts');
164  }
168  private static $plurals;
182  public static function getPlurals()
183  {
184  return self::getData('plurals');
185  }
190  public static function getSupersededLanguages()
191  {
192  return self::getData('supersededLanguages');
193  }
199  public static function getLanguageInfo($id)
200  {
201  $result = null;
202  $matches = array();
203  if (preg_match('/^([a-z]{2,3})(?:[_\-]([a-z]{4}))?(?:[_\-]([a-z]{2}|[0-9]{3}))?(?:$|-)/i', $id, $matches)) {
204  $languageId = strtolower($matches[1]);
205  $scriptId = (isset($matches[2]) && ($matches[2] !== '')) ? ucfirst(strtolower($matches[2])) : null;
206  $territoryId = (isset($matches[3]) && ($matches[3] !== '')) ? strtoupper($matches[3]) : null;
207  $normalizedId = $languageId;
208  if (isset($scriptId)) {
209  $normalizedId .= '_'.$scriptId;
210  }
211  if (isset($territoryId)) {
212  $normalizedId .= '_'.$territoryId;
213  }
214  // Structure precedence: see Likely Subtags - http://www.unicode.org/reports/tr35/tr35-31/tr35.html#Likely_Subtags
215  $variants = array();
216  $variantsWithScript = array();
217  $variantsWithTerritory = array();
218  if (isset($scriptId) && isset($territoryId)) {
219  $variantsWithTerritory[] = $variantsWithScript[] = $variants[] = "{$languageId}_{$scriptId}_{$territoryId}";
220  }
221  if (isset($scriptId)) {
222  $variantsWithScript[] = $variants[] = "{$languageId}_{$scriptId}";
223  }
224  if (isset($territoryId)) {
225  $variantsWithTerritory[] = $variants[] = "{$languageId}_{$territoryId}";
226  }
227  $variants[] = $languageId;
228  $allGood = true;
229  $scriptName = null;
230  $scriptStandAloneName = null;
231  if (isset($scriptId)) {
232  $scriptNames = self::getScriptNames(false);
233  if (isset($scriptNames[$scriptId])) {
234  $scriptName = $scriptNames[$scriptId];
235  $scriptStandAloneNames = self::getScriptNames(true);
236  $scriptStandAloneName = $scriptStandAloneNames[$scriptId];
237  } else {
238  $allGood = false;
239  }
240  }
241  $territoryName = null;
242  if (isset($territoryId)) {
243  $territoryNames = self::getTerritoryNames();
244  if (isset($territoryNames[$territoryId])) {
245  if ($territoryId !== '001') {
246  $territoryName = $territoryNames[$territoryId];
247  }
248  } else {
249  $allGood = false;
250  }
251  }
252  $languageName = null;
253  $languageNames = self::getLanguageNames();
254  foreach ($variants as $variant) {
255  if (isset($languageNames[$variant])) {
256  $languageName = $languageNames[$variant];
257  if (isset($scriptName) && (!in_array($variant, $variantsWithScript))) {
258  $languageName = $scriptName.' '.$languageName;
259  }
260  if (isset($territoryName) && (!in_array($variant, $variantsWithTerritory))) {
261  $languageName .= ' ('.$territoryNames[$territoryId].')';
262  }
263  break;
264  }
265  }
266  if (!isset($languageName)) {
267  $allGood = false;
268  }
269  $baseLanguage = null;
270  if (isset($scriptId) || isset($territoryId)) {
271  if (isset($languageNames[$languageId]) && ($languageNames[$languageId] !== $languageName)) {
272  $baseLanguage = $languageNames[$languageId];
273  }
274  }
275  $plural = null;
276  $plurals = self::getPlurals();
277  foreach ($variants as $variant) {
278  if (isset($plurals[$variant])) {
279  $plural = $plurals[$variant];
280  break;
281  }
282  }
283  if (!isset($plural)) {
284  $allGood = false;
285  }
286  $supersededBy = null;
287  $supersededBys = self::getSupersededLanguages();
288  foreach ($variants as $variant) {
289  if (isset($supersededBys[$variant])) {
290  $supersededBy = $supersededBys[$variant];
291  break;
292  }
293  }
294  if ($allGood) {
295  $result = array();
296  $result['id'] = $normalizedId;
297  $result['name'] = $languageName;
298  if (isset($supersededBy)) {
299  $result['supersededBy'] = $supersededBy;
300  }
301  if (isset($scriptStandAloneName)) {
302  $result['script'] = $scriptStandAloneName;
303  }
304  if (isset($territoryName)) {
305  $result['territory'] = $territoryName;
306  }
307  if (isset($baseLanguage)) {
308  $result['baseLanguage'] = $baseLanguage;
309  }
310  $result['categories'] = $plural;
311  }
312  }
313 
314  return $result;
315  }
316 }
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
$result
if(!array_key_exists('StateId', $_REQUEST)) $id
static getData($key)
Returns the loaded CLDR data.
Definition: CldrData.php:30
static getLanguageNames()
Returns a dictionary containing the language names.
Definition: CldrData.php:140
static getLanguageInfo($id)
Retrieve the name of a language, as well as if a language code is deprecated in favor of another lang...
Definition: CldrData.php:199
static getTerritoryNames()
Return a dictionary containing the territory names (in US English).
Definition: CldrData.php:150
Holds the CLDR data.
Definition: CldrData.php:9
$key
Definition: croninfo.php:18
static getScriptNames($standAlone)
Return a dictionary containing the script names (in US English).
Definition: CldrData.php:161
static getSupersededLanguages()
Return a list of superseded language codes.
Definition: CldrData.php:190
$data
Definition: bench.php:6