Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
LocaleHelper.php
1 <?php
2 /**
3  * Created by JetBrains PhpStorm.
4  * User: Andrey
5  * Date: 3/5/13
6  * Time: 12:11 PM
7  * To change this template use File | Settings | File Templates.
8  */
9 
11 {
12  /**
13  * @var array map of yii date format to jquery-ui date format
14  */
15  private static $_yiiToJq = array(
16 // 'D' => 'o',not used
17 // 'DDD' => 'oo',
18  'EEE' => 'D',
19  'EEEE' => 'DD',
20  'cccc' => 'DD',
21  'M' => 'm',
22  'MM' => 'mm',
23  'MMM' => 'M',
24  'MMMM' => 'MM',
25  'y' => 'yy',
26  'yy' => 'y',
27  'yyyy' => 'yy'
28  );
29 
30  /**
31  * @var array jQuery locales
32  */
33  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');
34 
35  /**
36  * @var array map of Yii locales to jQuery locales
37  */
38  private static $_yiiToJqLocaleMap = array(
39  'sr_cyrl' => 'sr',
40  'sr_latn' => 'sr_sr'
41  );
42 
43  /**
44  * @static
45  * @param $format YII date format
46  * @return mixed JQuery date format
47  */
48  public static function yiiFormatToJq($format)
49  {
50  return self::convert(self::$_yiiToJq, $format);
51  }
52 
53  private static function convert($substitution, $format)
54  {
55  if (empty($format)) return false;
56  $i = 0;
57  $finished = false;
58  while (!$finished) {
59  $singleFormat = '';
60  $prevL = $format[$i];
61  $end = false;
62  while (!$end) {
63  if ($prevL != @$format[$i])
64  $end = true;
65  else {
66  $singleFormat .= $format[$i];
67  $i++;
68  }
69  }
70  if (isset($substitution[$singleFormat])) {
71  $format = substr_replace($format, $substitution[$singleFormat], $i - strlen($singleFormat), strlen($singleFormat));
72  $i += strlen($substitution[$singleFormat]) - strlen($singleFormat);
73  }
74  if (@$format[$i] === '')
75  $finished = true;
76  }
77  return $format;
78  }
79 
80  /**
81  * @static
82  * @param $locale YII locale
83  * @return string jQuery locale
84  */
85  public static function yiiLocaleToJq($locale)
86  {
87  if (empty($locale)) return false;
88  $parts = explode('_', $locale);
89  if ($parts[0] == 'en')
90  return 'en-GB';
91  elseif (count($parts) == 1)
92  return $locale;
93  elseif (count($parts) == 3) {
94  $locale = $parts[0] . '_' . $parts[1];
95  }
96  if (isset(self::$_yiiToJqLocaleMap[$locale]))
97  $locale = self::$_yiiToJqLocaleMap[$locale];
98 
99  $parts = explode('_', $locale);
100  $jqLocale = $parts[0];
101  $jqExtLocale = isset($parts[1]) ? $parts[0] . '-' . strtoupper($parts[1]) : $parts[0];
102  if (in_array($jqExtLocale, self::$_jqLocales))
103  return $jqExtLocale;
104  elseif (in_array($jqLocale, self::$_jqLocales))
105  return $jqLocale;
106  else
107  return 'en-GB';
108 
109  }
110 }