Autodoc
  • Namespace
  • Function
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • FormattedString
  • Helpers
  • Locale
  • NumberFormatter
  • Translator

Functions

  • date_period
  • format_currency
  • format_date
  • format_datetime
  • format_number
  • format_size
  • format_time
  • get_cldr
  • get_language
  • get_locale
  • set_locale
  • t
  1 <?php
  2 
  3 /*
  4  * This file is part of the ICanBoogie package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace ICanBoogie\I18n;
 13 
 14 /**
 15  * Returns a locale.
 16  *
 17  * @param string $id Idenfitier of the locale or `null` to retrieve the current locale.
 18  *
 19  * @return \ICanBoogie\I18n\Locale
 20  */
 21 function get_locale($id=null)
 22 {
 23     return Helpers::get_locale($id);
 24 }
 25 
 26 /**
 27  * Sets the current locale.
 28  *
 29  * @param string $id Locale identifier.
 30  *
 31  * @return \ICanBoogie\I18n\Locale
 32  */
 33 function set_locale($id)
 34 {
 35     return Helpers::set_locale($id);
 36 }
 37 
 38 /**
 39  * Returns the language of the current locale.
 40  *
 41  * @return string The language of the current locale or `null` if there is none.
 42  */
 43 function get_language()
 44 {
 45     return Helpers::get_language();
 46 }
 47 
 48 /**
 49  * Returns the CLDR representation.
 50  *
 51  * @return \ICanBoogie\CLDR\Repository
 52  */
 53 function get_cldr()
 54 {
 55     return Helpers::get_cldr();
 56 }
 57 
 58 /**
 59  * Translates a string using the current locale.
 60  *
 61  * @param string $str The native string to translate.
 62  * @param array $args Arguments used to format the translated string.
 63  * @param array $options Options for the translation.
 64  *
 65  * @return string The translated string.
 66  */
 67 function t($str, array $args=array(), array $options=array())
 68 {
 69     return Helpers::t($str, $args, $options);
 70 }
 71 
 72 /**
 73  * Formats a size in "b", "Kb", "Mb", "Gb" or "Tb".
 74  *
 75  * @param int $size
 76  *
 77  * @return string
 78  */
 79 function format_size($size)
 80 {
 81     if ($size < 1024)
 82     {
 83         $str = ":size\xC2\xA0b";
 84     }
 85     else if ($size < 1024 * 1024)
 86     {
 87         $str = ":size\xC2\xA0Kb";
 88         $size = $size / 1024;
 89     }
 90     else if ($size < 1024 * 1024 * 1024)
 91     {
 92         $str = ":size\xC2\xA0Mb";
 93         $size = $size / (1024 * 1024);
 94     }
 95     else if ($size < 1024 * 1024 * 1024 * 1024)
 96     {
 97         $str = ":size\xC2\xA0Gb";
 98         $size = $size / (1024 * 1024 * 1024);
 99     }
100     else
101     {
102         $str = ":size\xC2\xA0Tb";
103         $size = $size / (1024 * 1024 * 1024 * 1024);
104     }
105 
106     return t($str, array(':size' => round($size)));
107 }
108 
109 function format_number($number)
110 {
111     $locale = get_locale();
112     $decimal_point = $locale['numbers']['symbols-numberSystem-' . $locale['numbers']['defaultNumberingSystem']]['decimal'];
113     $thousands_sep = ' ';
114 
115     return number_format($number, ($number - floor($number) < .009) ? 0 : 2, $decimal_point, $thousands_sep);
116 }
117 
118 function format_currency($value, $currency)
119 {
120     return get_locale()->number_formatter->format_currency($value, $currency);
121 }
122 
123 /**
124  * Formats a date.
125  *
126  * @param mixed $datetime
127  * @param string $pattern_or_width
128  *
129  * @see \ICanBoogie\CLDR\DateFormatter
130  */
131 function format_date($datetime, $pattern_or_width='medium')
132 {
133     return get_locale()->calendar->date_formatter->format($datetime, $pattern_or_width);
134 }
135 
136 /**
137  * Formats a time.
138  *
139  * @param mixed $datetime
140  * @param string $pattern_or_width
141  *
142  * @see \ICanBoogie\CLDR\TimeFormatter
143  */
144 function format_time($datetime, $pattern_or_width='medium')
145 {
146     return get_locale()->calendar->time_formatter->format($datetime, $pattern_or_width);
147 }
148 
149 /**
150  * Formats a date and time.
151  *
152  * @param mixed $datetime
153  * @param string $pattern_or_width_or_skeleton
154  *
155  * @see \ICanBoogie\CLDR\DateTimeFormatter
156  */
157 function format_datetime($datetime, $pattern_or_width_or_skeleton='medium')
158 {
159     return get_locale()->calendar->datetime_formatter->format($datetime, $pattern_or_width_or_skeleton);
160 }
161 
162 function date_period($date)
163 {
164     static $relative;
165 
166     if (is_numeric($date))
167     {
168         $date_secs = $date;
169         $date = date('Y-m-d', $date);
170     }
171     else
172     {
173         $date_secs = strtotime($date);
174     }
175 
176     $today_days = strtotime(date('Y-m-d')) / (60 * 60 * 24);
177     $date_days = strtotime(date('Y-m-d', $date_secs)) / (60 * 60 * 24);
178 
179     $diff = round($date_days - $today_days);
180     $locale_id = get_language();
181 
182     if (empty($relative[$locale_id]))
183     {
184         $locale = get_locale();
185         $relative[$locale_id] = $locale['dateFields']['day'];
186     }
187 
188     if (isset($relative[$locale_id]["relative-type-{$diff}"]))
189     {
190         return $relative[$locale_id]["relative-type-{$diff}"];
191     }
192     else if ($diff > -6)
193     {
194         return ucfirst(format_date($date_secs, 'EEEE'));
195     }
196 
197     return format_date($date);
198 }
199 
200 /**
201  * Patchable helpers of the ICanBoogie/I18n package.
202  */
203 class Helpers
204 {
205     static private $jumptable = array
206     (
207         'get_locale' => array(__CLASS__, 'get_locale'),
208         'set_locale' => array(__CLASS__, 'set_locale'),
209         'get_language' => array(__CLASS__, 'get_language'),
210         'get_cldr' => array(__CLASS__, 'get_cldr'),
211         't' => array(__CLASS__, 't')
212     );
213 
214     /**
215      * Calls the callback of a patchable function.
216      *
217      * @param string $name Name of the function.
218      * @param array $arguments Arguments.
219      *
220      * @return mixed
221      */
222     static public function __callstatic($name, array $arguments)
223     {
224         return call_user_func_array(self::$jumptable[$name], $arguments);
225     }
226 
227     /**
228      * Patches a patchable function.
229      *
230      * @param string $name Name of the function.
231      * @param collable $callback Callback.
232      *
233      * @throws \RuntimeException is attempt to patch an undefined function.
234      */
235     static public function patch($name, $callback)
236     {
237         if (empty(self::$jumptable[$name]))
238         {
239             throw new \RuntimeException("Undefined patchable: $name.");
240         }
241 
242         self::$jumptable[$name] = $callback;
243     }
244 
245     /*
246      * Default implementations
247      */
248 
249     /**
250      * Current locale.
251      *
252      * @var \ICanBoogie\I18n\Locale
253      */
254     static private $locale;
255 
256     static private function get_locale($id=null)
257     {
258         return $id ? Locale::from($id) : (self::$locale ? self::$locale : self::$locale = Locale::from('en'));
259     }
260 
261     static private function set_locale($id)
262     {
263         return self::$locale = Locale::from($id);
264     }
265 
266     static private function get_language()
267     {
268         return self::$locale ? self::$locale->language : null;
269     }
270 
271     static private function get_cldr()
272     {
273         static $cldr;
274 
275         if (!$cldr)
276         {
277             $provider = new \ICanBoogie\CLDR\Provider
278             (
279                 new \ICanBoogie\CLDR\RunTimeCache(new \ICanBoogie\CLDR\FileCache(REPOSITORY)),
280                 new \ICanBoogie\CLDR\Retriever
281             );
282 
283             $cldr = new \ICanBoogie\CLDR\Repository($provider);
284         }
285 
286         return $cldr;
287     }
288 
289     static private function t($str, array $args=array(), array $options=array())
290     {
291         $locale = get_locale(empty($options['language']) ? null : $options['language']);
292 
293         return $locale->translator($str, $args, $options);
294     }
295 }
Autodoc API documentation generated by ApiGen 2.8.0