Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translatable options for ArrayChoiceList not being translated (moving from ChoiceList) #469

Closed
Mopster opened this issue Dec 5, 2017 · 1 comment

Comments

@Mopster
Copy link

Mopster commented Dec 5, 2017

Q A
Bundle version dev-master (08e8f23)
Symfony version 2.8.31
PHP version 7.1.12

Expected behavior

Using the deprecated ChoiceList the options are translatable, when moving the our choice lists to the new ArrayChoiceList, the options are no longer translatable.

Current code

use Symfony\Component\Form\ChoiceList\ArrayChoiceList;

use JMS\TranslationBundle\Model\Message;

abstract class TranslatableArrayChoiceList extends ArrayChoiceList
{
    // @todo Move to a new domain
    const TRANSLATION_DOMAIN = 'messages';

    public static function getTranslatableChoices()
    {
        throw new \RuntimeException('No translation choices found');
    }

    /**
     * Extractable status translation labels.
     */
    public static function getTranslationMessages()
    {
        $messages = array();
        foreach (static::getTranslatableChoices() as $choice) {
            $message = new Message($choice, static::TRANSLATION_DOMAIN);
            $message->setMeaning('Item for a dropdown select list.');  // Extra metadata for the translator
            $messages[] = $message;
        }
        return $messages;
    }
}
use JMS\TranslationBundle\Translation\TranslationContainerInterface;

class EmailPrivacyChoiceList extends TranslatableArrayChoiceList implements TranslationContainerInterface
{
    public function __construct()
    {
        parent::__construct(self::getTranslatableChoices());
    }

    public static function getTranslatableChoices()
    {
        return [
            'emails with this contact are not private' => false,
            'emails with this contact are private' => true
        ];
    }

Previous code

use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;

abstract class TranslatableChoiceList extends ChoiceList
{
    const TRANSLATION_DOMAIN = 'messages';

    public static function getTranslatableChoices()
    {
        throw new \RuntimeException('No translation choices found');
    }

    /**
     * Extractable status translation labels.
     */
    public static function getTranslationMessages()
    {
        $messages = array();
        foreach (static::getTranslatableChoices() as $choice) {
            $message = new Message($choice, static::TRANSLATION_DOMAIN);
            $message->setMeaning('Item for a dropdown select list.');  // Extra metadata for the translator
            $messages[] = $message;
        }
        return $messages;
    }
}
use JMS\TranslationBundle\Translation\TranslationContainerInterface;

class EmailPrivacyChoiceList extends TranslatableChoiceList implements TranslationContainerInterface
{
    public function __construct()
    {
        parent::__construct([false, true], self::getTranslatableChoices());
    }

    public static function getTranslatableChoices()
    {
        return [
            'emails with this contact are not private',
            'emails with this contact are private'
        ];
    }
}
@Mopster
Copy link
Author

Mopster commented Aug 15, 2018

Had another go at this and this time I seem to got something right, no longer an issue. I used the choice_loader option by implementing ChoiceLoaderInterface on the TranslatableArrayChoiceList (with some other small code changes).

@Mopster Mopster closed this as completed Aug 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant