coldume/translated-exception

Automatic exception message translation

v1.0.0 2014-10-24 15:09 UTC

This package is not auto-updated.

Last update: 2024-11-19 04:42:28 UTC


README

Build Status

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