2017 © Pedro Peláez
 

symfony-bundle entity-translation-bundle

Translation bundle to help with rendering pretty names for enum-like classes.

image

hostnet/entity-translation-bundle

Translation bundle to help with rendering pretty names for enum-like classes.

  • Thursday, April 26, 2018
  • by yannickl88
  • Repository
  • 7 Watchers
  • 4 Stars
  • 34,859 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 1 Open issues
  • 13 Versions
  • 6 % Grown

The README.md

entity-translation-bundle

This bundle allows the automation of translating an enum value to a human readable string, for example, displaying a status text instead of the enum-value of a status code., (*1)

In it's essence, it allows to map a value to a translation within the domain that is the class name. What this means in practice is that you can use it as follows:, (*2)

$status_text = $translator->trans(SetupStatus::DONE, [], SetupStatus::class);

Or in a twig template:, (*3)

{{ constant('AppBundle\\Entity\\SetupStatus::DONE') | trans([], 'AppBundle\\Entity\\SetupStatus') }}

Requirements

The entity translation bundle requires at least php 7.3 and the symfony translation component. For specific requirements, please check composer.json, (*4)

Installation

Installing is pretty straightforward, this package is available on packagist., (*5)

Example

$ composer require hostnet/entity-translation-bundle

Register The Bundle in your AppKernel

This bundle makes use of the translator which is registered by the framework bundle. So make sure you register this bundle after the FrameworkBundle., (*6)

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new AppBundle\AppBundle(),
            new Hostnet\Bundle\EntityTranslationBundle\HostnetEntityTranslationBundle(),
            // ...
        ];

        return $bundles;
    }
}

Usage

Simply add an enum.en.yml to the translations folder in the Resources folder of one of your bundles. This will contain the translations for a given enum. The translation keys are the fully qualified namespaces in lowercase and an _ between CamelCase words. So for instance the enum AppBundle\Entity\SetupStatus would become app_bundle.entity.setup_status., (*7)

Consider the following class:, (*8)

<?php
namespace AppBundle\Entity;

final class SetupStatus
{
    const PENDING           = 1;
    const DONE              = 2;
    const ERROR             = 3;
    const REVERTING_CHANGES = 4;
}

Your AppBundle/Resources/translations/enum.en.yml could look as followed:, (*9)

app_bundle:
    entity:
        setup_status:
            pending           : Installation Pending.
            done              : Installation Complete.
            error             : An error occured.
            reverting_changes : Reverting changes.

The translator will then pick up all enum classes defined in your translation file., (*10)

The Versions

10/02 2016

0.2.0

0.2.0.0

Translation bundle to help with rendering pretty names for enum-like classes.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/04 2015

0.1.0

0.1.0.0

Translation bundle to help with rendering pretty names for enum-like classes.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/04 2015

0.0.2

0.0.2.0

Translation bundle to help with rendering pretty names for enum-like classes.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/09 2014

0.0.1

0.0.1.0

Translation bundle to help with rendering pretty names for enum-like classes.

  Sources   Download

MIT

The Requires

 

The Development Requires