Skip to content

Commit

Permalink
wop
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Salarmehr committed Jan 30, 2020
1 parent e519ead commit 67983cb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 86 deletions.
29 changes: 29 additions & 0 deletions examples/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php // example.php

use Salarmehr\Cosmopolitan;

require_once '../vendor/autoload.php';

foreach (['en', 'en_UK', 'de_DE', 'zh_CH', 'fa_IR'] as $locale) {

$intl = new \Salarmehr\Cosmopolitan\Intl($locale, 'Australia/Sydney');
// or using the helper $intl=intl($locale);

// $language = $intl->language(\Locale::getPrimaryLanguage($locale));
// $country = $intl->country(\Locale::getRegion($locale));

echo $intl->language('en') . "\n";
echo $intl->country('AU') . "\n";
echo $intl->datetime(time()) . "\n";
echo $intl->datetime(time()) . "\n";
echo $intl->ordinal(2) . "\n";
echo $intl->date(time(), Cosmopolitan\Intl::FULL) . "\n";
echo $intl->time(time(), Cosmopolitan\Intl::SHORT) . "\n";
echo $intl->quote("Reza!") . "\n";
echo $intl->number(123400.567) . "\n";
echo $intl->percentage(.14) . "\n";
echo $intl->spellout(10000000001) . "\n";
echo $intl->currency('AUD') . "\n";
echo $intl->money(12.3, 'AUD') . "\n";
echo $intl->duration(599) . "\n\n";
}
20 changes: 0 additions & 20 deletions src/Bundle.php

This file was deleted.

104 changes: 42 additions & 62 deletions src/Intl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
declare(strict_types=1);

namespace Salarmehr\Cosmopolitan;
require_once "Exception.php";
require_once "Bundle.php";

class Intl
{
Expand All @@ -20,20 +18,30 @@ class Intl
public $locale;
public $defaults = [
'useLocalCalender' => true,
'bundleName' => null,
'fallback' => true,
'bundleName' => null,
'fallback' => true,
];
private $timezone;
private $useLocalCalender;

public function __construct(string $locale = null, string $timezone = null, array $options = [])
{
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']);
}

public function get(\ResourceBundle $bundle, string $key) {
$output = $bundle->get($key);
if ($output == null && intl_is_failure($bundle->getErrorCode())) {
throw new Exception($bundle->getErrorMessage());
}
return $output;
}

private function getBundle(string $bundle): \ResourceBundle {
return \ResourceBundle::create($this->locale, $bundle);
}

#region key/value functions
Expand All @@ -43,18 +51,11 @@ public function __construct(string $locale = null, string $timezone = null, arra
* @return string
* @throws Exception
*/
public function currency(string $currency, bool $symbole = false): string
{
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
Expand All @@ -63,9 +64,9 @@ public function get($bundle, string $key): \ResourceBundle
* @return string
* @throws Exception
*/
public function language($language): string
{
return $this->get('ICUDATA-lang', 'Languages')->get(strtolower($language));
public function language($language): string {
$bundle = $this->getBundle('ICUDATA-lang')->get('Languages');
return $this->get($bundle, strtolower($language));
}

/**
Expand All @@ -76,10 +77,9 @@ public function language($language): string
* @return string
* @throws Exception
*/
public function country(string $country): string
{
$x=$this->get('ICUDATA-region', 'Countries');
return $x->get(strtoupper($country));
public function country(string $country): string {
$bundle = $this->getBundle('ICUDATA-region')->get('Countries');
return $this->get($bundle, strtoupper($country));
}

/**
Expand All @@ -88,25 +88,22 @@ public function country(string $country): string
* @return string
* @throws Exception
*/
public function script(string $script): string
{
return $this->get('ICUDATA-lang', 'Scripts')->get(ucwords($script));
public function script(string $script): string {
return $this->getBundle('ICUDATA-lang')->get('Scripts')->get(ucwords($script));
}

public function calendar(string $calendar): string
{
return $this->get('ICUDATA-lang', 'Types')->get('calendar')->get($calendar);
public function calendar(string $calendar): string {
return $this->getBundle('ICUDATA-lang')->get('Types')->get('calendar')->get($calendar);
}

#endregion
public function message(string $message, array $args): string
{
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');
public function quote(string $quote): string {
$bundle = $this->getBundle('ICUDATA')->get('delimiters');
return $bundle->get('quotationStart') . $quote . $bundle->get('quotationEnd');
}

/**
Expand All @@ -123,8 +120,7 @@ public function quote(string $quote): string
* @param string|null $pattern
* @return string
*/
public function money(float $value, string $currency, string $pattern = ''): string
{
public function money(float $value, string $currency, string $pattern = ''): string {
return (new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY, $pattern))->formatCurrency($value, $currency);
}

Expand All @@ -133,30 +129,25 @@ public function money(float $value, string $currency, string $pattern = ''): str
* @param int $precision
* @return string
*/
public function percentage(float $value, int $precision = 3): 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
{
public function number(float $number): string {
return (new \NumberFormatter($this->locale, \NumberFormatter::DEFAULT_STYLE))->format($number);
}

public function ordinal(int $number): string
{
public function ordinal(int $number): string {
return (new \NumberFormatter($this->locale, \NumberFormatter::ORDINAL))->format($number);
}

public function spellout(float $number): string
{
public function spellout(float $number): string {
return (new \NumberFormatter($this->locale, \NumberFormatter::SPELLOUT))->format($number);
}

public function duration(float $duration): string
{
public function duration(float $duration): string {
return $number = \NumberFormatter::create($this->locale, \NumberFormatter::DURATION)->format($duration);
}

Expand All @@ -166,40 +157,29 @@ public function duration(float $duration): string
* @param $format
* @return false|string
*/
public function customTime($value, string $format): 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
{
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
{
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
{
public function time($value, int $length = self::MEDIUM): string {
return $this->datetime($value, self::NONE, $length);
}

private function verify($local): void
{
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'));
}
2 changes: 1 addition & 1 deletion src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
if (!function_exists('intl')) {
function intl($locale)
{
return new \Salarmehr\Cosmopolitan($locale);
return new \Salarmehr\Cosmopolitan\Intl($locale);
}
}
5 changes: 2 additions & 3 deletions tests/CosmopolitanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function test__construct()
public function languageProvider()
{
return [
['en', 'en', 'English'],
['en', 'en_AU', 'English'],
['en_US', 'en', 'English'],
['en_AU', 'en', 'English'],
['fa', 'en', 'انگلیسی'],
['fa', 'fa', 'فارسی'],
];
Expand Down Expand Up @@ -102,7 +102,6 @@ public function countryProvider()
{
return [
['en', 'AU', 'Australia'],
['en', 'en_AU', 'Australia'],
['fa', 'AU', 'استرالیا'],
];
}
Expand Down

0 comments on commit 67983cb

Please sign in to comment.