Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
LocaleHelper.php
1 <?php
2 /**
3  * Class for making compatible YII locale and date format with formats of other extensions.
4  */
5 class LocaleHelper
6 {
7  /**
8  * @var array map of yii date format to jquery-ui date format
9  */
10  private static $_yiiToJq = array(
11 // 'D' => 'o',not used
12 // 'DDD' => 'oo',
13  'EEE' => 'D',
14  'EEEE' => 'DD',
15  'cccc' => 'DD',
16  'M' => 'm',
17  'MM' => 'mm',
18  'MMM' => 'M',
19  'MMMM' => 'MM',
20  'y' => 'yy',
21  'yy' => 'y',
22  'yyyy' => 'yy'
23  );
24 
25  /**
26  * @var array jQuery locales
27  */
28  private static $_jqLocales = array('af', 'ar', 'az', 'bg', 'bs', 'ca', 'cs', 'da', 'de-CH', 'de', 'el', 'en-GB', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr-CH', 'fr', 'he', 'hr', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ko', 'lt', 'lv', 'ms', 'nl-BE', 'nl', 'no', 'pl', 'pt-BR', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr-SR', 'sr', 'sv', 'ta', 'th', 'tr', 'uk', 'vi', 'zh-CN', 'zh-HK', 'zh-TW');
29 
30  /**
31  * @var array map of Yii locales to jQuery locales
32  */
33  private static $_yiiToJqLocaleMap = array(
34  'sr_cyrl' => 'sr',
35  'sr_latn' => 'sr_sr'
36  );
37 
38  /**
39  * @static
40  * @param $format YII date format
41  * @return mixed JQuery date format
42  */
43  public static function yiiFormatToJq($format)
44  {
45  return self::convert(self::$_yiiToJq, $format);
46  }
47 
48  private static function convert($substitution, $format)
49  {
50  if (empty($format)) return false;
51  $i = 0;
52  $finished = false;
53  while (!$finished) {
54  $singleFormat = '';
55  $prevL = $format[$i];
56  $end = false;
57  while (!$end) {
58  if ($prevL != @$format[$i])
59  $end = true;
60  else {
61  $singleFormat .= $format[$i];
62  $i++;
63  }
64  }
65  if (isset($substitution[$singleFormat])) {
66  $format = substr_replace($format, $substitution[$singleFormat], $i - strlen($singleFormat), strlen($singleFormat));
67  $i += strlen($substitution[$singleFormat]) - strlen($singleFormat);
68  }
69  if (@$format[$i] === '')
70  $finished = true;
71  }
72  return $format;
73  }
74 
75  /**
76  * @static
77  * @param $locale YII locale
78  * @return string jQuery locale
79  */
80  public static function yiiLocaleToJq($locale)
81  {
82  if (empty($locale)) return false;
83  $parts = explode('_', $locale);
84  if ($parts[0] == 'en')
85  return 'en-GB';
86  elseif (count($parts) == 1)
87  return $locale;
88  elseif (count($parts) == 3) {
89  $locale = $parts[0] . '_' . $parts[1];
90  }
91  if (isset(self::$_yiiToJqLocaleMap[$locale]))
92  $locale = self::$_yiiToJqLocaleMap[$locale];
93 
94  $parts = explode('_', $locale);
95  $jqLocale = $parts[0];
96  $jqExtLocale = isset($parts[1]) ? $parts[0] . '-' . strtoupper($parts[1]) : $parts[0];
97  if (in_array($jqExtLocale, self::$_jqLocales))
98  return $jqExtLocale;
99  elseif (in_array($jqLocale, self::$_jqLocales))
100  return $jqLocale;
101  else
102  return 'en-GB';
103 
104  }
105 }