https://github.com/sonofwinter/translationbundle
A Symfony 4/5 bundle to translate an entity
https://github.com/sonofwinter/translationbundle
php7 php8 symfony-bundle symfony4 symfony5 translation
Last synced: 7 months ago
JSON representation
A Symfony 4/5 bundle to translate an entity
- Host: GitHub
- URL: https://github.com/sonofwinter/translationbundle
- Owner: SonOfWinter
- License: mit
- Created: 2018-05-13T17:25:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-20T13:05:47.000Z (over 2 years ago)
- Last Synced: 2025-02-09T12:43:09.559Z (about 1 year ago)
- Topics: php7, php8, symfony-bundle, symfony4, symfony5, translation
- Language: PHP
- Homepage:
- Size: 80.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SOWTranslationBundle
This Bundle provides a translator for Symfony entities.
## Prerequisites
- PHP 8.2 or higher
- Symfony 7.0 or higher
- Composer
## Installation
Open a command console, enter your project directory and execute:
```bash
$ composer require sonofwinter/translation-bundle
```
## Configuration
### Bundle Registration
Register the bundle in your `config/bundles.php`:
```php
return [
// ...
SOW\TranslationBundle\SOWTranslationBundle::class => ['all' => true],
];
```
### Available Locales
You can override the default available locales by setting the `sow_translation.available_locales` parameter:
```yaml
parameters:
sow_translation.available_locales: ['en', 'fr', 'es', 'de', 'it']
```
### Custom Translation Entity
By default, a Translation entity class is provided, but you can create your own translation entity class that extends AbstractTranslation.
To use it, set the `sow_translation.translation_class_name` parameter:
```yaml
parameters:
sow_translation.translation_class_name: App\Entity\YourTranslationClass
```
## Usage
### Setting Up Translatable Entities
Your translated entities must implement the `Translatable` interface.
Then define translated properties in your entity using either annotations or attributes.
#### Using Attributes (PHP 8.0+)
```php
use SOW\TranslationBundle\Attribute\Translation;
class MyClass {
#[Translation(key: "firstname")]
private string $firstname = '';
#[Translation(key: "lastname", setter: "setOtherName")]
private string $lastname = '';
}
```
### Configuration Notes
- The `key` property can be used to specify a different name for the translation key. If not provided, the property name is used.
- The `setter` property allows you to specify a custom setter method. If the setter doesn't exist, a `TranslatableConfigurationException` will be thrown.
## Translation Methods
### Translating Entities
```php
// Translate an entity to a specific language
$translator->translate($entity, 'en');
// Translate an entity to multiple languages
$translator->translateForLangs($entity, ['en', 'fr', 'de']);
```
### Setting Translations
```php
// Set a single translation
$translator->setTranslationForLangAndValue($entity, 'en', 'firstname', 'John');
// Set multiple values for one language
$translator->setTranslationForLangAndValues($entity, 'en', [
'firstname' => 'John',
'lastname' => 'Doe'
]);
// Set multiple translations for multiple languages
$translator->setTranslations($entity, [
'en' => [
'firstname' => 'John',
'lastname' => 'Doe'
],
'fr' => [
'firstname' => 'Jean',
'lastname' => 'Dupont'
]
]);
```
### Removing Translations
```php
// Remove a specific translation
$translator->removeByObjectKeyAndLang($entity, 'firstname', 'en');
// Remove all translations for an entity
$translator->removeAllForTranslatable($entity);
// Remove all translations for a specific key
$translator->removeAllByKey('firstname');
```