TranslatedException
, (*1)
Depict
Previously:, (*2)
+-------------+ 'hello' received
| Client side | <-----------------------+
+-------------+ |
| |
v |
+-------------------+ |
| Do somthing wrong | |
+-------------------+ |
| |
v |
+-------------------------------+ |
| throw new \Exception('hello') | |
+-------------------------------+ |
| |
v |
+---------------------------------+ |
| Catch and echo $e->getMessage() | ----+
+---------------------------------+
Now:, (*3)
+-------------+ 'bonjour' received
| Client side | <-------------------------------------+
+-------------+ |
| |
v |
+-------------------+ |
| Do somthing wrong | |
+-------------------+ |
| |
v |
+----------------------------------------+ |
| throw new TranslatedException('hello') | |
+----------------------------------------+ |
| |
v +---------------------+ |
+---------------------+ | Translator | |
| Inside | --> | locale: "fr" | |
| TranslatedException | <-- | dictionary: "fr-en" | |
+---------------------+ +---------------------+ |
| |
v |
+---------------------------------+ |
| Catch and echo $e->getMessage() | ------------------+
+---------------------------------+
Installation
Simply add a dependency on coldume/translated-exception to your project's
composer.json file:, (*4)
{
"require": {
"coldume/translated-exception": "~1.0"
}
}
Usage
use TranslatedException\TranslatedException;
$options = [
'locale' => 'fr',
'cache_dir' => __DIR__.'/foo',
'debug' => true,
];
TranslatedException::init($options);
TranslatedException::addResourceDir(__DIR__.'/bar');
try {
throw new TranslatedException('foo', 'hello.%name%', ['%name%' => 'foo']);
} catch (TranslatedException $e) {
echo $e->getMessage();
}
Resources
-
Symfony translation component., (*5)
http://symfony.com/doc/current/components/translation/index.html, (*6)