PHPackages                             pms-nz/object-translation-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. pms-nz/object-translation-bundle

ActiveSymfony-bundle[Validation &amp; Sanitization](/categories/validation)

pms-nz/object-translation-bundle
================================

Translate entities using Doctrine listeners

v1.0.3(4mo ago)08MITPHPPHP &gt;=8.2CI failing

Since Jan 1Pushed 4mo agoCompare

[ Source](https://github.com/pms-nz/object-translation-bundle)[ Packagist](https://packagist.org/packages/pms-nz/object-translation-bundle)[ RSS](/packages/pms-nz-object-translation-bundle/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (4)Dependencies (11)Versions (5)Used By (0)

pms-nz/object-translation-bundle
================================

[](#pms-nzobject-translation-bundle)

This bundle, based on symfonycasts\\object-translation-bundle, provides a simple way to translate Doctrine entities in Symfony applications.

The major changes are:

- The translations are done automatically through Doctrine event listeners. There is no need to use a `translate` method.
- Translation fallbacks for a locale can be chained. For example `'es' -> 'it' -> 'en'`.

Installation
------------

[](#installation)

### Install the bundle via Composer:

[](#install-the-bundle-via-composer)

```
composer require pms-nz/object-translation-bundle
```

### Enable the bundle in your `config/bundles.php` file:

[](#enable-the-bundle-in-your-configbundlesphp-file)

Note

This step is not required if you are using Symfony Flex.

```
return [
    // ...
    ObjectTranslationBundle::class => ['all' => true],
];
```

### Create the translation entity in your app:

[](#create-the-translation-entity-in-your-app)

Note

You may give your class a name other than "Translation".

```
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use PmsNz\ObjectTranslationBundle\Model\AbstractTranslation;

#[ORM\Entity]
class Translation extends AbstractTranslation
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    public int $id;
}
```

### Configure the entity in your `config/packages/object_translation.yaml` file:

[](#configure-the-entity-in-your-configpackagesobject_translationyaml-file)

Note

If your translation entity has another class name, use that.

```
pms-nz_object_translation:
    translation_class: App\Entity\Translation
```

### Create and run the migration to add the translation table:

[](#create-and-run-the-migration-to-add-the-translation-table)

```
symfony console make:migration
symfony console doctrine:migrations:migrate
```

Marking Entities as Translatable
--------------------------------

[](#marking-entities-as-translatable)

To mark an entity as translatable, use the `Translatable` attribute on the entity class and the `TranslatableProperty` attribute on the fields you want to translate.

```
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use PmsNz\ObjectTranslationBundle\Mapping\Translatable;
use PmsNz\ObjectTranslationBundle\Mapping\TranslatableProperty;

#[ORM\Entity]
#[Translatable('product')]
class Product
{
    // ...

    #[ORM\Column(type: 'string', length: 255)]
    #[TranslatableProperty]
    public string $name;

    #[ORM\Column(type: 'text')]
    #[TranslatableProperty]
    public string $description;
}
```

Usage
-----

[](#usage)

The translator listens to Doctrine events and automatically translates when a translatable entity is loaded.

Managing Translations
---------------------

[](#managing-translations)

The database table that extends `ĀbstractTranslation` has the following structure:

- `id`: Primary key (added by you)
- `object_type`: The *alias* defined in the `Translatable` attribute (e.g., `product`)
- `object_id`: The ID of the translated entity
- `locale`: The locale of the translation (e.g., `fr`)
- `field`: The entity property name being translated (e.g., `description`)
- `value`: The translated value

Each row represents a single property translation for a specific entity in a specific locale.

You can manage these translations yourself but two console commands are provided to help:

### `object-translation:export`

[](#object-translationexport)

`` This command exports all entity translations, in your default locale, to a CSV file.

```
symfony console object-translation:export translations.csv
```

This will create a `translations.csv` file at the root of your project with the following structure:

```
type,id,field,value
```

You can then take this file to translation service for translation. Be sure to keep the `type`, `id`, and `field` columns intact. The `value` column is what needs to be translated into the desired language.

You may also add a `--locale` argument. In this case extra columns will be added according to the fallbacks for the value provided for the `--locale` argument.

For instance, if the fallbacks for the 'es' locale is as follows:

```
pms-nz_object_translation:
    translation_class: App\Entity\Translation
    fallbacks:
        es: [it, fr]
```

The column headers will be

```
type,id,field,value,en,fr,it,es
```

with the `fr`, `it` and `es` columns providing the translations given in the `translation_class` for the `fr`, `it` and `es` locales respectively.

Translation Caching
-------------------

[](#translation-caching)

For performance, translations are cached. By default, they use your `cache.app` pool and have no expiration time. This can be configured:

```
pms-nz_object_translation:
    cache:
        pool: 'cache.object_translation' # a custom pool name
        ttl: 3600 # expire after one hour
```

### Translation Tags

[](#translation-tags)

If your cache pool supports *cache tagging*, tags are added to the cache keys. Two keys are added:

- `object-translation`: All translations are tagged with this key.
- `object-translation-{type}`: Where `{type}` is the translatable alias (e.g., `product`).

You can invalidate these tags by using the `cache:pool:invalidate-tags` command:

```
# invalidate all object translation caches
symfony console cache:pool:invalidate-tags object-translation

# invalidate only the translation cache for "product" entities
symfony console cache:pool:invalidate-tags object-translation-product
```

Translation Fallback Chains
---------------------------

[](#translation-fallback-chains)

This logic allows you to support regional dialects or specific user groups without duplicating your entire translation library.

When using a base language (e.g., Spanish) across different groups, you can store only the unique variations rather than the entire language set. This is achieved by "chaining" the translation lookup:

1. **Specific Variant:** The system first looks for the translation in the group-specific language (e.g., `es-gp1`).
2. **Next Language:** If the translation is missing, it falls back to the next language (`es`) in the chain. This step is repeated until a translation is found.
3. **Default Value:** If no translation is found in the chain, the system reverts to the global default language.

**Example Chain:** `'es-gp1' -> 'es' -> Default`

```
pms-nz_object_translation:
    fallbacks:
        es-dl1: [es]
        es-gp1: [es-dl1, es]
```

Note that these fallbacks are indexed by the locale which begins the fallback chain.

Full Default Configuration
--------------------------

[](#full-default-configuration)

```
pms-nz_object_translation:

    # The class name of your Translation entity.
    translation_class:    ~ # Required, Example: App\Entity\Translation

    # Cache settings for object translations.
    cache:
        enabled:              true

        # The cache pool to use for storing object translations.
        pool:                 null

        # The time-to-live for cached translations, in seconds. null for no expiration.
        ttl:                  null

    # Fallbacks for object translations.
    fallbacks:

        # Prototype
        name:                 ~
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance76

Regular maintenance activity

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

5

Last Release

136d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e6ab23fd5f94f83fe7dfe77c5586486667b0fbea9223a9acc87a9413a5461ace?d=identicon)[pms-nz](/maintainers/pms-nz)

---

Top Contributors

[![pms-nz](https://avatars.githubusercontent.com/u/52359388?v=4)](https://github.com/pms-nz "pms-nz (11 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pms-nz-object-translation-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/pms-nz-object-translation-bundle/health.svg)](https://phpackages.com/packages/pms-nz-object-translation-bundle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k16.7M310](/packages/easycorp-easyadmin-bundle)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[kimai/kimai

Kimai - Time Tracking

4.6k7.4k1](/packages/kimai-kimai)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
