From e519ead76e1fcf948989866847fb6d14751e9701 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 30 Jan 2020 09:09:36 +1100 Subject: [PATCH] wop --- composer.json | 2 +- readme.md | 6 ++ src/Bundle.php | 20 ++++ src/Cosmopolitan.php | 147 -------------------------- src/Exception.php | 12 +++ src/Intl.php | 205 +++++++++++++++++++++++++++++++++++++ src/IntlException.php | 6 -- tests/CosmopolitanTest.php | 10 +- 8 files changed, 249 insertions(+), 159 deletions(-) create mode 100644 src/Bundle.php delete mode 100644 src/Cosmopolitan.php create mode 100644 src/Exception.php create mode 100644 src/Intl.php delete mode 100644 src/IntlException.php diff --git a/composer.json b/composer.json index 62d24df..0c4a04e 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ }, "autoload": { "psr-4": { - "Salarmehr\\": "src/" + "Salarmehr\\Cosmopolitan\\": "src/" }, "files": [ "src/helper.php" diff --git a/readme.md b/readme.md index 334332c..5a802e3 100644 --- a/readme.md +++ b/readme.md @@ -120,3 +120,9 @@ Localising some values for: فارسی (ایران) Licence ======= MIT + +Links +===== +- [Locale Explorer](http://demo.icu-project.org/icu-bin/locexp) +- [ICU Data](https://github.com/unicode-org/icu/tree/release-65-1/icu4c/source/data) +- [ICU data tables by Alexander Makarov](https://intl.rmcreative.ru/) \ No newline at end of file diff --git a/src/Bundle.php b/src/Bundle.php new file mode 100644 index 0000000..5008545 --- /dev/null +++ b/src/Bundle.php @@ -0,0 +1,20 @@ +locale = $locale ?: \Locale::getDefault(); - $this->timezone = $timezone; - - // use the local calendar system in the countries which use non-gregorian calendar. - $this->useLocaleCalender = $userLocalCalender; - } - - /** - * Translate the language of this locale (e.g. En -> English) - * @param $locale - * @return string - */ - public function language($locale): string - { - return \Locale::getDisplayLanguage($locale, $this->locale); - } - - /** - * Translate the country of a locale (e.g. AU -> Australia) - * @param string $local ISO 3166 country codes or a valid locale - * @return string - */ - public function country(string $local): string - { - if (!preg_match('#[-_]#', $local)) $local = '-' . $local; - return \Locale::getDisplayRegion($local, $this->locale); - } - - public function message(string $message, array $args): string - { - return \MessageFormatter::formatMessage($this->locale, $message, $args); - } - - public function quote(string $quote): string - { - return $this->bundle()->get('delimiters')->get('quotationStart') . $quote . $this->bundle()->get('delimiters')->get('quotationEnd'); - } - - public function bundle($name = null): \ResourceBundle - { - return \ResourceBundle::create($this->locale, $name); - } - - /** - * @param float $value - * @param string $currency - * The 3-letter ISO 4217 currency code indicating the currency to use. - * @param string|null $pattern - * @return string - */ - public function currency(float $value, string $currency, string $pattern = ''): string - { - return (new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY, $pattern))->formatCurrency($value, $currency); - } - - /** - * @param float $value - * @param int $precision - * @return string - */ - public function percentage(float $value, int $precision = 3): string - { - $formatter = new \NumberFormatter($this->locale, \NumberFormatter::PERCENT); - $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $precision); - return $formatter->format($value); - } - - public function number(float $number): string - { - return (new \NumberFormatter($this->locale, \NumberFormatter::DEFAULT_STYLE))->format($number); - } - - public function ordinal(int $number): string - { - return (new \NumberFormatter($this->locale, \NumberFormatter::ORDINAL))->format($number); - } - - public function spellout(float $number): string - { - return (new \NumberFormatter($this->locale, \NumberFormatter::SPELLOUT))->format($number); - } - - public function duration(float $duration): string - { - return $number = \NumberFormatter::create($this->locale, \NumberFormatter::DURATION)->format($duration); - } - - private function verify($local): void - { - $local = preg_replace('#-#', '_', $local); - if (!in_array($local, \ResourceBundle::getLocales(''))) { - throw new IntlException("Invalid locale $local"); - } - } - - /** - * https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details - * @param $value - * @param $format - * @return false|string - */ - public function customTime($value, string $format): string - { - $formatter = new \IntlDateFormatter($this->locale, null, null, $this->timezone, $this->calendarType, $format); - return $formatter->format($value); - } - - public function datetime($value, int $datetype = self::SHORT, int $timetype = self::MEDIUM): string - { - $calendarType = $this->useLocaleCalender ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN; - $formatter = new \IntlDateFormatter($this->locale, $datetype, $timetype, $this->timezone, $calendarType); - return $formatter->format($value); - } - - public function date($value, int $length = self::SHORT): string - { - return $this->datetime($value, $length, self::NONE); - } - - public function time($value, int $length = self::MEDIUM): string - { - return $this->datetime($value, self::NONE, $length); - } -} \ No newline at end of file diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000..15f3841 --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,12 @@ + true, + 'bundleName' => null, + 'fallback' => true, + ]; + private $timezone; + private $useLocalCalender; + + public function __construct(string $locale = null, string $timezone = null, array $options = []) + { + $options = $options + $this->defaults; + $this->locale = $locale ?: \Locale::getDefault(); + $this->timezone = $timezone; + + $this->useLocalCalender = $options['useLocalCalender']; + $this->bundle = \ResourceBundle::create($this->locale, $options['bundleName'], $options['fallback']); + } + + #region key/value functions + + /** + * @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use. + * @return string + * @throws Exception + */ + public function currency(string $currency, bool $symbole = false): string + { + $bundle = \ResourceBundle::create($this->locale, 'ICUDATA-curr'); + return $this->get($bundle, 'Currencies')[$currency][$symbole ? 0 : 1]; + } + + public function get($bundle, string $key): \ResourceBundle + { + $bundle = new \Salarmehr\Cosmopolitan\Bundle($this->locale, $bundle); + return $bundle->get($key, false); + } + + /** + * Translate a language identifier (e.g. En -> English, glk -> Gilaki) + * If you have a locale identifier (en-Au) instead of language + * use \Locale::getPrimaryLanguage($locale) to extract the language + * @param $language + * @return string + * @throws Exception + */ + public function language($language): string + { + return $this->get('ICUDATA-lang', 'Languages')->get(strtolower($language)); + } + + /** + * Translate a language identifier (e.g. En -> English, glk -> Gilaki) + * If you have a locale identifier (en-Au) instead of language + * use \Locale::getPrimaryLanguage($locale) to extract the language + * @param string $country ISO 3166 country codes + * @return string + * @throws Exception + */ + public function country(string $country): string + { + $x=$this->get('ICUDATA-region', 'Countries'); + return $x->get(strtoupper($country)); + } + + /** + * Translate the scriptidentifier (e.g. 'zh_Hans' -> 'Simplified Chinese') + * @param $script + * @return string + * @throws Exception + */ + public function script(string $script): string + { + return $this->get('ICUDATA-lang', 'Scripts')->get(ucwords($script)); + } + + public function calendar(string $calendar): string + { + return $this->get('ICUDATA-lang', 'Types')->get('calendar')->get($calendar); + } + + #endregion + public function message(string $message, array $args): string + { + return \MessageFormatter::formatMessage($this->locale, $message, $args); + } + + public function quote(string $quote): string + { + return $this->get('ICUDATA', 'delimiters')->get('quotationStart') . $quote . $this->get('ICUDATA', 'delimiters')->get('quotationEnd'); + } + + /** + * Translate the calendar identifier (e.g. "buddhist" -> "Buddhist Calendar") + * @param $calendar + * @return string + * @throws Exception + */ + + + /** + * @param float $value + * @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use. + * @param string|null $pattern + * @return string + */ + public function money(float $value, string $currency, string $pattern = ''): string + { + return (new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY, $pattern))->formatCurrency($value, $currency); + } + + /** + * @param float $value + * @param int $precision + * @return string + */ + public function percentage(float $value, int $precision = 3): string + { + $formatter = new \NumberFormatter($this->locale, \NumberFormatter::PERCENT); + $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $precision); + return $formatter->format($value); + } + + public function number(float $number): string + { + return (new \NumberFormatter($this->locale, \NumberFormatter::DEFAULT_STYLE))->format($number); + } + + public function ordinal(int $number): string + { + return (new \NumberFormatter($this->locale, \NumberFormatter::ORDINAL))->format($number); + } + + public function spellout(float $number): string + { + return (new \NumberFormatter($this->locale, \NumberFormatter::SPELLOUT))->format($number); + } + + public function duration(float $duration): string + { + return $number = \NumberFormatter::create($this->locale, \NumberFormatter::DURATION)->format($duration); + } + + /** + * https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details + * @param $value + * @param $format + * @return false|string + */ + public function customTime($value, string $format): string + { + $formatter = new \IntlDateFormatter($this->locale, null, null, $this->timezone, $this->calendarType, $format); + return $formatter->format($value); + } + + public function date($value, int $length = self::SHORT): string + { + return $this->datetime($value, $length, self::NONE); + } + + public function datetime($value, int $datetype = self::SHORT, int $timetype = self::MEDIUM): string + { + $calendarType = $this->useLocalCalender ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN; + $formatter = new \IntlDateFormatter($this->locale, $datetype, $timetype, $this->timezone, $calendarType); + return $formatter->format($value); + } + + public function time($value, int $length = self::MEDIUM): string + { + return $this->datetime($value, self::NONE, $length); + } + + private function verify($local): void + { + $local = preg_replace('#-#', '_', $local); + if (!in_array($local, \ResourceBundle::getLocales(''))) { + throw new Exception("Invalid locale $local"); + } + } +} + +$intl = new Intl('fa', 'Australia/Sydney'); +$intl = new Intl('en', 'Australia/Sydney'); +// or using the helper $intl=intl($locale); +var_dump($intl->country('sasdfsadfir')); +//var_dump($intl->language('EN')); \ No newline at end of file diff --git a/src/IntlException.php b/src/IntlException.php deleted file mode 100644 index 383662e..0000000 --- a/src/IntlException.php +++ /dev/null @@ -1,6 +0,0 @@ -assertEquals($intl->language($language), $name); } @@ -51,7 +51,7 @@ public function quoteProvider() */ public function testQute($local, $text, $quote) { - $intl = new Cosmopolitan($local); + $intl = new Intl($local); $this->assertEquals($intl->quote($text), $quote); } @@ -84,7 +84,7 @@ public function messageProvider() */ public function testMessage($local, $text, $arguments, $message) { - $intl = new Cosmopolitan($local); + $intl = new Intl($local); $this->assertEquals($intl->message($text, $arguments), $message); } @@ -112,7 +112,7 @@ public function countryProvider() */ public function testCountry($local, $country, $name) { - $intl = new Cosmopolitan($local); + $intl = new Intl($local); $this->assertEquals($intl->country($country), $name); }