https://github.com/paysera/lib-gedmo-translatable-integration-bundle
This library provides means to easily integrate Gedmo Translatable doctrine extension into your project.
https://github.com/paysera/lib-gedmo-translatable-integration-bundle
Last synced: 3 months ago
JSON representation
This library provides means to easily integrate Gedmo Translatable doctrine extension into your project.
- Host: GitHub
- URL: https://github.com/paysera/lib-gedmo-translatable-integration-bundle
- Owner: paysera
- Created: 2020-09-07T08:21:50.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-11-04T14:50:23.000Z (over 4 years ago)
- Last Synced: 2025-01-03T16:36:02.518Z (5 months ago)
- Language: PHP
- Size: 44.9 KB
- Stars: 0
- Watchers: 9
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# lib-gedmo-translatable-integration-bundle
This library provides means to easily integrate [Gedmo Translatable](https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/translatable.md) doctrine extension into your project.
## Why?
Managing entities translations sometimes can become an overhead, that adds unnecessary cognitive-load for developers. This
library helps you by loading all translations and translating the entity automatically, without the necessity to do it manually.## Installation
Append `new Paysera\Bundle\GedmoTranslatableIntegrationBundle\PayseraGedmoTranslatableIntegrationBundle()` to your project
Symfony kernel bundles.Append `config.yml`:
```yaml
paysera_gedmo_translatable_integration:
default_locale: ''
````default_locale` - this is your application default locale.
## Usage
#### Define translatable entities
```php
addTranslations('name', [
'lt' => 'Translation for LT',
'en' => 'Translation for EN',
]);
// Done. Bundle will seamlessly make sure, that this entity will be translated using Gedmo extension.
}
/**
* @param Post $post
* @return Translation[]
*/
public function getTranslations(Post $post): array
{
return $post->getTranslations('name');
// Done. Bundle makes sure that translations are lazy-loaded, when accessed. This is done without necessity to
// manually fetch from repositories.
}
}
```Without bundle:
```php
$translation) {
foreach ($translation as $locale => $value) {
$this->translationRepository->translate($post, $field, $locale, $value);
}
}
}
public function getTranslations(Post $post)
{
// Not lazy-loaded. Now you have to fetch all translations and set them somewhere, but not sure if they will be
// accessed.
// Or implement custom repository method by extending the default Gedmo repository.
$result = [];
$translations = $this->translationRepository->findTranslations($post);
foreach ($translations as $locale => $translation) {
foreach ($translation as $property => $value) {
if ($property === 'name') {
$result[$locale] = $value;
}
}
}
return $result;
}
}
```#### Modifying queries to join the translations table
In cases, where you might want to join the translations table to select for translations as well, you can use the
`\Paysera\Bundle\GedmoTranslatableIntegrationBundle\Service\QueryBuilderTranslationSearchModifier`.```php
postRepository->createQueryBuilderFromFilter($filter);
// Modify the query builder to join the translations table and also select posts that match the translation.
$this->queryBuilderTranslationSearchModifier->modifyQueryBuilder(
$queryBuilder,
(new TranslationSearchConfiguration())
->setAlias('p')
->setClassName(Post::class)
->setField('name')
->setLocale('lt')
->setValue(sprintf('%%%s%%', addcslashes($filter->getName(), '%_')))
);return $queryBuilder->getQuery()->getResult();
}
}
```