Skip to content

Commit

Permalink
Added LocalizeTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 16, 2016
1 parent ba19aa9 commit 5c2d816
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
22 changes: 3 additions & 19 deletions lib/ListFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
* Formats variable-length lists of things such as "Monday, Tuesday, Friday, and Saturday".
*
* @see http://www.unicode.org/reports/tr35/tr35-general.html#ListPatterns
*
* @method LocalizedListFormatter localize($locale_code)
*/
class ListFormatter implements Formatter
{
use AccessorTrait;
use RepositoryPropertyTrait;
use LocalizeTrait;

/**
* @param Repository $repository
Expand Down Expand Up @@ -115,23 +118,4 @@ private function format_pattern($pattern, $v0, $v1)

]);
}

/**
* Localize the instance.
*
* @param string $locale_code
*
* @return LocalizedListFormatter
*
* @throw \LogicException when the instance was created without a repository.
*/
public function localize($locale_code)
{
if (!$this->repository)
{
throw new \LogicException("The instance was created without a repository.");
}

return $this->repository->locales[$locale_code]->localize($this);
}
}
39 changes: 39 additions & 0 deletions lib/LocalizeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\CLDR;

/**
* @property-read Repository $repository
*/
trait LocalizeTrait
{
/**
* Localize the instance.
*
* @param string $locale_code
*
* @return mixed
*
* @throw \LogicException when the instance was created without a repository.
*/
public function localize($locale_code)
{
$repository = $this->repository;

if (!$repository)
{
throw new \LogicException("The instance was created without a repository.");
}

return $repository->locales[$locale_code]->localize($this);
}
}
22 changes: 3 additions & 19 deletions lib/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

/**
* Formats numbers.
*
* @method LocalizedNumberFormatter localize($locale_code)
*/
class NumberFormatter implements Formatter
{
use AccessorTrait;
use RepositoryPropertyTrait;
use LocalizeTrait;

static private $default_symbols = [

Expand Down Expand Up @@ -154,23 +157,4 @@ public function format($number, $pattern, array $symbols = [])

]);
}

/**
* Localize the instance.
*
* @param string $locale_code
*
* @return LocalizedNumberFormatter
*
* @throw \LogicException when the instance was created without a repository.
*/
public function localize($locale_code)
{
if (!$this->repository)
{
throw new \LogicException("The instance was created without a repository.");
}

return $this->repository->locales[$locale_code]->localize($this);
}
}

0 comments on commit 5c2d816

Please sign in to comment.