forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Translate] add an intl MessageFormatter interpolator (phalcon#892)
- Loading branch information
Showing
11 changed files
with
342 additions
and
133 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Phalcon\Translate\Interpolator; | ||
|
||
use Phalcon\Translate\InterpolatorInterface; | ||
use Phalcon\Translate\Exception; | ||
use MessageFormatter; | ||
use IntlException; | ||
|
||
class Intl implements InterpolatorInterface | ||
{ | ||
private $locale; | ||
|
||
public function __construct($locale) | ||
{ | ||
$this->locale = $locale; | ||
} | ||
|
||
/** | ||
* Replaces placeholders by the values passed | ||
* Use the MessageFormatter class, | ||
* See http://php.net/manual/en/class.messageformatter.php | ||
*/ | ||
public function replacePlaceholders($translation, $placeholders = null) | ||
{ | ||
if (is_array($placeholders) && count($placeholders)) { | ||
try { | ||
// TODO (?) : keep an internal cache of the MessageFormatter objects (key = locale.translation) | ||
$fmt = new MessageFormatter($this->locale, $translation); | ||
} catch (IntlException $e) { | ||
$fmt = null; | ||
} finally { | ||
// for php 7.x the original exception message is "Constructor failed" | ||
// for php 5.6 the constructor returns null, see this wont fix bug https://bugs.php.net/bug.php?id=58631 | ||
// make it a bit more understandable | ||
if (is_null($fmt)) { | ||
throw new Exception( | ||
"Unable to instantiate a MessageFormatter. Check locale and string syntax.", | ||
0, | ||
isset($e) ? $e : null | ||
); | ||
} | ||
} | ||
|
||
$translation = $fmt->format($placeholders); | ||
if ($translation === false) { | ||
throw new Exception($fmt->getErrorMessage(), $fmt->getErrorCode()); | ||
} | ||
} | ||
return $translation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Phalcon\Translate\Interpolator | ||
|
||
Usage examples of the interpolators available here: | ||
|
||
## Intl | ||
|
||
It needs the extension [intl](php.net/manual/book.intl.php) to be installed in PHP, and it uses [MessageFormatter](http://php.net/manual/en/class.messageformatter.php) objects in an interpolator interface. | ||
More about the syntax convention can be read on this [formating guide](https://www.sitepoint.com/localization-demystified-understanding-php-intl/) and on the [ICU documentation](http://userguide.icu-project.org/formatparse/messages). | ||
|
||
```php | ||
<?php | ||
use Phalcon\Translate\Adapter\NativeArray; | ||
use Phalcon\Translate\Interpolator\Intl; | ||
|
||
$translate = new NativeArray([ | ||
'interpolator' => new Intl('en_US'), // this interpolator must be locale aware | ||
'content' => ['hi-name' => 'Hello {name}, it\'s {time, number, integer} o\'clock'] | ||
]); | ||
|
||
$name = 'Henry'; | ||
$translate->_('hi-name', ['name' => $name, 'time' => 8]); // Hello Henry, it's 8 o'clock | ||
``` | ||
|
||
```php | ||
<?php | ||
use Phalcon\Translate\Adapter\NativeArray; | ||
use Phalcon\Translate\Interpolator\Intl; | ||
|
||
$translate = new NativeArray([ | ||
'interpolator' => new Intl('fr_FR'), // this interpolator must be locale aware | ||
'content' => ['apples' => "{count, plural, =0{Je n'ai aucune pomme} =1{J'ai une pomme} other{J'ai # pommes}}."] | ||
]); | ||
|
||
// thousands separator is " " (blank space) for fr_FR | ||
echo $translate->_('apples', ['count' => 1000]); // J'ai 1 000 pommes | ||
``` |
Oops, something went wrong.