|
| 1 | +# TransModifier |
| 2 | + |
| 3 | +The `|trans` modifier translates string values using Symfony Translation component. |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +The `|trans` modifier enables internationalization by translating string keys into their localized equivalents, supporting multi-language applications with minimal configuration. |
| 8 | + |
| 9 | +## Behavior |
| 10 | + |
| 11 | +The modifier follows the Chain of Responsibility pattern and only processes string values with the `trans` pipe: |
| 12 | + |
| 13 | +- **Non-trans pipes**: Delegates to next modifier without modification |
| 14 | +- **Non-string values**: Delegates to next modifier without modification |
| 15 | +- **String values with `trans` pipe**: Passes through translator and continues chain |
| 16 | +- **Missing translations**: Returns the original key unchanged |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### Simple Usage (Default BypassTranslator) |
| 21 | + |
| 22 | +```php |
| 23 | +use Respect\StringFormatter\PlaceholderFormatter; |
| 24 | + |
| 25 | +// Works out of the box - no dependencies required |
| 26 | +$formatter = new PlaceholderFormatter(['message' => 'hello']); |
| 27 | + |
| 28 | +echo $formatter->format('{{message|trans}}'); |
| 29 | +// Outputs: "hello" (original input returned) |
| 30 | +``` |
| 31 | + |
| 32 | +### With Real Translator |
| 33 | + |
| 34 | +```php |
| 35 | +use Respect\StringFormatter\PlaceholderFormatter; |
| 36 | +use Respect\StringFormatter\Modifier\TransModifier; |
| 37 | +use Symfony\Component\Translation\Translator; |
| 38 | +use Symfony\Component\Translation\Loader\ArrayLoader; |
| 39 | + |
| 40 | +$translator = new Translator('en'); |
| 41 | +$translator->addLoader('array', new ArrayLoader()); |
| 42 | +$translator->addResource('array', ['greeting' => 'Hello World'], 'en'); |
| 43 | + |
| 44 | +$formatter = new PlaceholderFormatter( |
| 45 | + ['message' => 'greeting'], |
| 46 | + new TransModifier($translator) // Inject real translator |
| 47 | +); |
| 48 | + |
| 49 | +echo $formatter->format('{{message|trans}}'); |
| 50 | +// Outputs: "Hello World" (translated result) |
| 51 | +``` |
| 52 | + |
| 53 | +## Examples |
| 54 | + |
| 55 | +### With BypassTranslator (Default) |
| 56 | + |
| 57 | +| Parameters | Template | Output | |
| 58 | +| ---------------------- | ---------------------- | --------------- | |
| 59 | +| `['msg' => 'hello']` | `"{{msg|trans}}"` | `"hello"` | |
| 60 | +| `['msg' => 'welcome']` | `"{{msg|trans}}"` | `"welcome"` | |
| 61 | +| `['msg' => 'unknown']` | `"{{msg|trans}}"` | `"unknown"` | |
| 62 | + |
| 63 | +### With Real Translator |
| 64 | + |
| 65 | +| Parameters | Template | Output | |
| 66 | +| --------------------------- | ---------------------- | --------------- | |
| 67 | +| `['msg' => 'greeting']` | `"{{msg|trans}}"` | `"Hello World"` | |
| 68 | + |
| 69 | +### Mixed with Regular Placeholders |
| 70 | + |
| 71 | +| Parameters | Template | Output | |
| 72 | +| ------------------------------------------- | -------------------------------------------- | ------------------------- | |
| 73 | +| `['name' => 'John', 'greeting' => 'hello']` | `"{{greeting|trans}}, {{name}}"` | `"hello, John"` | |
| 74 | + |
| 75 | +## Common Use Cases |
| 76 | + |
| 77 | +1. **User interface messages** - Internationalize button labels, error messages, notifications |
| 78 | +2. **Email templates** - Send localized emails to international users |
| 79 | +3. **Error presentation** - Show validation errors in user's preferred language |
| 80 | +4. **Report generation** - Generate reports in different languages |
| 81 | +5. **Content management** - Translate article headings, descriptions, UI elements |
| 82 | + |
| 83 | +## Implementation Notes |
| 84 | + |
| 85 | +The `TransModifier` requires a `Symfony\Contracts\Translation\TranslatorInterface` implementation. The modifier: |
| 86 | + |
| 87 | +1. Checks for exact match with `trans` pipe value |
| 88 | +2. Validates input is a string |
| 89 | +3. Calls `translator->trans($value)` without parameters for simplicity |
| 90 | +4. Passes translated result to next modifier in chain |
| 91 | +5. Returns original key if translation not found |
| 92 | + |
| 93 | +## Integration |
| 94 | + |
| 95 | +`TransModifier` is typically used in the middle of the modification chain: |
| 96 | + |
| 97 | +```php |
| 98 | +CustomModifier -> TransModifier -> RawModifier -> StringifyModifier |
| 99 | +``` |
| 100 | + |
| 101 | +The modifier processes translations first, then passes the result to subsequent modifiers for consistent formatting across the application. |
| 102 | + |
| 103 | +## Dependencies |
| 104 | + |
| 105 | +**Required:** |
| 106 | +- `symfony/translation-contracts`: Provides the `TranslatorInterface` contract |
| 107 | + |
| 108 | +**Optional/Suggested:** |
| 109 | +- `symfony/translation`: Implementation of the translation interface |
| 110 | + - Install with: `composer require symfony/translation` |
| 111 | + - Required for actual translation functionality |
| 112 | + - Package is suggested and will be installed when needed |
| 113 | + |
| 114 | +## Default Behavior |
| 115 | + |
| 116 | +The `TransModifier` works out of the box without requiring symfony/translation. By default, it uses a `BypassTranslator` that returns the original input unchanged. This means the modifier is always available and functional. |
| 117 | + |
| 118 | +## Real Translations |
| 119 | + |
| 120 | +To enable actual translations, install symfony/translation and inject it into the constructor: |
| 121 | + |
| 122 | +```php |
| 123 | +use Respect\StringFormatter\Modifier\TransModifier; |
| 124 | +use Symfony\Component\Translation\Translator; |
| 125 | +use Symfony\Component\Translation\Loader\ArrayLoader; |
| 126 | + |
| 127 | +$translator = new Translator('en'); |
| 128 | +$translator->addLoader('array', new ArrayLoader()); |
| 129 | +$translator->addResource('array', ['hello' => 'Hello World'], 'en'); |
| 130 | + |
| 131 | +// Inject into specific TransModifier instance |
| 132 | +$modifier = new TransModifier($nextModifier, $translator); |
| 133 | +``` |
| 134 | + |
| 135 | +## Testing |
| 136 | + |
| 137 | +For testing purposes, the `TestingTranslator` implementation is provided: |
| 138 | + |
| 139 | +```php |
| 140 | +use Respect\StringFormatter\Test\Helper\TestingTranslator; |
| 141 | + |
| 142 | +$translator = new TestingTranslator([ |
| 143 | + 'hello' => 'Hello World', |
| 144 | + 'welcome' => 'Welcome', |
| 145 | +]); |
| 146 | +``` |
| 147 | + |
| 148 | +This provides a lightweight way to test translation behavior without requiring the full Symfony Translation component. The testing implementation is included in the dev dependencies. |
0 commit comments