2017 © Pedro Peláez
 

symfony-bundle translation-bundle

This Bundle provides a translator from Entity

image

sonofwinter/translation-bundle

This Bundle provides a translator from Entity

  • Friday, July 27, 2018
  • by SonOfWinter
  • Repository
  • 1 Watchers
  • 0 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 0 % Grown

The README.md

SOWTranslationBundle

This Bundle provides a translator for Symfony entities., (*1)

Prerequisites

  • PHP 8.2 or higher
  • Symfony 7.0 or higher
  • Composer

Installation

Open a command console, enter your project directory and execute:, (*2)

$ composer require sonofwinter/translation-bundle

Configuration

Bundle Registration

Register the bundle in your config/bundles.php:, (*3)

return [
    // ...
    SOW\TranslationBundle\SOWTranslationBundle::class => ['all' => true],
];

Available Locales

You can override the default available locales by setting the sow_translation.available_locales parameter:, (*4)

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:, (*5)

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., (*6)

Using Attributes (PHP 8.0+)

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

// 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

// 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

// 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');

The Versions

27/07 2018

dev-master

9999999-dev

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

27/07 2018

v0.2.6

0.2.6.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

26/07 2018

v0.2.5

0.2.5.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

26/07 2018

v0.2.4

0.2.4.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

26/07 2018

v0.2.3

0.2.3.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

23/07 2018

v0.2.2

0.2.2.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

23/07 2018

v0.2.1

0.2.1.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

20/07 2018

v0.2.0

0.2.0.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

12/07 2018

v0.1.4

0.1.4.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

09/07 2018

v0.1.3

0.1.3.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

09/07 2018

v0.1.2

0.1.2.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

09/07 2018

v0.1.1

0.1.1.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc

09/07 2018

v0.1.0

0.1.0.0

This Bundle provides a translator from Entity

  Sources   Download

MIT

The Requires

 

The Development Requires

by Thomas Leduc