SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/I18n.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Kohana;
  17.  
  18. use Sprout\Helpers\Locales\LocaleInfo;
  19.  
  20.  
  21. /**
  22. * Formats data according to local conventions.
  23. * This is an easier-to-use helper version of the LocaleInfo set of classes.
  24. *
  25. * To set the locale, set the config param:
  26. * sprout.locale
  27. *
  28. * The default is AUS - Australia
  29. **/
  30. class I18n
  31. {
  32. private static $locale;
  33.  
  34.  
  35. /**
  36.   * Set the locale to the default as per the configuration
  37.   **/
  38. public static function init()
  39. {
  40. $l = Kohana::config('sprout.locale');
  41. if ($l == '') $l = 'AUS';
  42. self::$locale = LocaleInfo::get($l);
  43. }
  44.  
  45.  
  46. /**
  47.   * Set the locale to something other than the default
  48.   * Useful for multi-currency shopping carts, for example.
  49.   *
  50.   * @param string $country The country to set the locale to, e.g. 'AUS'.
  51.   **/
  52. public static function setLocale($country)
  53. {
  54. self::$locale = LocaleInfo::get($country);
  55. }
  56.  
  57.  
  58. /**
  59.   * Return JavaScript code which initialises the I18n javascript library with the locale parameters
  60.   *
  61.   * @return string HTML <script> tag
  62.   */
  63. public static function initJavaScript()
  64. {
  65. Needs::fileGroup('i18n');
  66. $out = '<script>';
  67. $out .= 'I18n.setLocale(' . json_encode(self::$locale->getParameters()) . ');';
  68. $out .= '</script>';
  69. return $out;
  70. }
  71.  
  72.  
  73. /**
  74.   * Return a formatted number
  75.   *
  76.   * @param float $number The number to format
  77.   * @param int $precision The number of digits to show after the decimal point
  78.   **/
  79. public static function number($number, $precision)
  80. {
  81. return self::$locale->numberFormat($number, $precision);
  82. }
  83.  
  84.  
  85. /**
  86.   * Return a formatted currency value
  87.   *
  88.   * @param float $number The number to format
  89.   **/
  90. public static function money($number, $precision = null)
  91. {
  92. return self::$locale->moneyFormat($number, $precision);
  93. }
  94.  
  95.  
  96. /**
  97.   * Return a formatted date in a short format (e.g. 3/1/2003)
  98.   *
  99.   * @param int $timestamp. A unix timestamp. Defaults to the current time
  100.   **/
  101. public static function shortdate($timestamp = 0)
  102. {
  103. if (! $timestamp) $timestamp = time();
  104. return self::$locale->shortdate($timestamp);
  105. }
  106.  
  107.  
  108. /**
  109.   * Return a formatted date in a short format (e.g. Mon 3rd Jan 2003)
  110.   *
  111.   * @param int $timestamp. A unix timestamp. Defaults to the current time
  112.   **/
  113. public static function longdate($timestamp = 0)
  114. {
  115. if (! $timestamp) $timestamp = time();
  116. return self::$locale->longdate($timestamp);
  117. }
  118.  
  119.  
  120. /**
  121.   * Return a formatted dtime (e.g. 5:30pm)
  122.   *
  123.   * @param int $timestamp. A unix timestamp. Defaults to the current time
  124.   **/
  125. public static function time($timestamp = 0)
  126. {
  127. if (! $timestamp) $timestamp = time();
  128. return self::$locale->time($timestamp);
  129. }
  130.  
  131.  
  132. /**
  133.   * Returns HTML for address fields
  134.   *
  135.   * @param bool $required If the fields should be listed as required or not
  136.   **/
  137. public static function addressFields($required)
  138. {
  139. self::$locale->outputAddressFields('', $required);
  140. }
  141.  
  142. }
  143.