1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\I18n;
use ICanBoogie\CLDR\Repository;
use ICanBoogie\Prototype\MethodNotDefined;
/**
* A locale refers to a set of user preferences that tend to be shared across significant swaths
* of the world. Traditionally, the data associated with it provides support for formatting and
* parsing of dates, times, numbers, and currencies; for measurement units, for
* sort-order (collation), plus translated names for time zones, languages, countries, and
* scripts. The data can also include support for text boundaries (character, word, line,
* and sentence), text transformations (including transliterations), and other services.
*
* @property-read string $id Locale id
* @property-read string $language Language of the locale.
* @property-read string $territory Territory of the locale.
* @property-read array $calendar The data of the default calendar for the locale.
* @property-read Conventions $conventions The UNICODE conventions for the locale.
* @property-read DateFormatter $date_formatter The data formatter for the locale.
* @property-read NumberFormatter $number_formatter The number formatter for the locale.
* @property-read Translator $translator The translator for the locale.
*/
class Locale extends \ICanBoogie\CLDR\Locale
{
/**
* Instantiated locales.
*
* @var array[string]Locale
*/
static private $locales = array();
/**
* Returns the locale for the specified id.
*
* @param string $id The locale id.
*
* @return Locale.
*/
static public function from($id)
{
if (isset(self::$locales[$id]))
{
return self::$locales[$id];
}
return self::$locales[$id] = new static(get_cldr(), $id);
}
/**
* Language identifier.
*
* @var string
*/
protected $id;
/**
* Language of the locale.
*
* @var string
*/
protected $language;
/**
* Territory code for this locale.
*
* @var string
*/
protected $territory;
/**
* Initializes the {@link $language} and {@link $territory} properties.
*
* @param string $id Locale identifier. The underscore character "_" is replace with the
* hypen-minus character "-" as advised by the {@link http://www.rfc-editor.org/rfc/bcp/bcp47.txt BCP 47}.
*/
public function __construct(Repository $repository, $id)
{
$id = strtr($id, '_', '-');
$this->id = $id;
list($this->language, $this->territory) = explode('-', $id) + array(1 => null);
parent::__construct($repository, $id);
}
public function __get($property)
{
switch ($property)
{
case 'id': return $this->id;
case 'language': return $this->language;
case 'territory': return $this->territory;
case 'calendar': return $this->$property = $this->get_calendar();
case 'number_formatter': return $this->$property = $this->get_number_formatter();
case 'translator': return $this->$property = $this->get_translator();
}
return parent::__get($property);
}
public function __call($method, $arguments)
{
if (is_callable(array($this, $method)))
{
return call_user_func_array($this->$method, $arguments);
}
throw new MethodNotDefined(array($method, $this));
}
/**
* Returns the locale identifier.
*
* @return string
*/
public function __toString()
{
return $this->id;
}
/**
* Returns the data of the default calendar for the locale.
*
* @return array
*/
protected function get_calendar()
{
return $this->calendars['gregorian'];
}
/**
* Returns the number formatter for the locale.
*
* @return NumberFormatter
*/
protected function get_number_formatter()
{
return new NumberFormatter($this);
}
/**
* Returns the string translator for the locale.
*
* @return Translator
*/
protected function get_translator()
{
return Translator::get($this->id);
}
}