coldume / translated-exception
Automatic exception message translation
Installs: 30 570
Dependents: 3
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
- symfony/config: ~2.4
- symfony/translation: ~2.4
Requires (Dev)
- phpunit/phpunit: ~4.3
This package is not auto-updated.
Last update: 2024-11-19 04:42:28 UTC
README
Depict
Previously:
+-------------+ 'hello' received
| Client side | <-----------------------+
+-------------+ |
| |
v |
+-------------------+ |
| Do somthing wrong | |
+-------------------+ |
| |
v |
+-------------------------------+ |
| throw new \Exception('hello') | |
+-------------------------------+ |
| |
v |
+---------------------------------+ |
| Catch and echo $e->getMessage() | ----+
+---------------------------------+
Now:
+-------------+ '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:
{ "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.
http://symfony.com/doc/current/components/translation/index.html