T
T
TrueDrago2012-11-27 14:49:13
symfony
TrueDrago, 2012-11-27 14:49:13

Question about Custom Doctrine Sluggable Transliterator in Symfony 2?

In a couple of projects, I came across the fact that the extension for Doctrine Sluggable behavior performs transliteration in a skewed way.
For example, "e" = "ie", "b" = "-", and so on.
In this regard, I want to create a custom transliterator, but it's not entirely clear how to do it correctly in order to take into account the Symphony architecture? (I have not figured out such subtleties yet) There is a very abstract example on the github:

<?php<br><br>
$callable = array('My\Class', 'transliterationMethod');<br>
$sluggableListener->setTransliterator($callable);<br><br>
// or use a closure<br><br>
$callable = function($text, $separatorUsed, $objectBeingSlugged) {<br>
    // ...<br>
    return $transliteratedText;<br>
};<br>
$sluggableListener->setTransliterator($callable);<br>

Are there any nifty custom transliterator assignments built into Symphony?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
I
Inori, 2012-11-27
@TrueDrago

You need to override the Sluggable listener and add an argument to it with a call to your transliterator.
Something like:

<service id="stof_doctrine_extensions.listener.sluggable" class="%stof_doctrine_extensions.listener.sluggable.class%" public="false">
    <call method="setAnnotationReader">
        <argument type="service" id="annotation_reader" />
    </call>
    <call method="setTransliterator">
         <argument type="collection">
             <argument>\path\to\your\Urlizer</argument>
             <argument>transliterate</argument>
         </argument>
     </call>
</service>

C
chetzof, 2012-12-09
@chetzof

I did so. gist.github.com/4244940 I
pulled out the transliteration table from one wordpress plugin.
In my opinion, Inori has a shorter version.

I
Igor Chernyshev, 2012-11-27
@Vir

And why didn't you like the option from the example? I didn’t work with sluggable (by the way, I will one of these days), so I don’t know any elegant options.

D
Davert, 2012-11-28
@Davert

Is forking and patching not an option?
It is unlikely that anyone will need: "e" = "ie", "b" = "-"

K
karser, 2014-12-09
@karser

In the piggy bank: you can attach the transliterator in the compiler

namespace YourBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AddTransliterator implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $def = $container->findDefinition('stof_doctrine_extensions.listener.sluggable');
        $def->addMethodCall('setTransliterator', );
    }
}

I
igormukhin, 2015-04-28
@igormukhin

If you need to translate from Russian into English first:

# services.yml
    app.transliterator:
        class: AppBundle\Gedmo\Sluggable\SluggableTransliterator
        arguments:
            - @remedge.yandex_translate.manager

    gedmo.listener.sluggable:
        class: Gedmo\Sluggable\SluggableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]
            - [ setTransliterator, [ [@app.transliterator, 'translate'] ] ]

# AppBundle/Gedmo/Sluggable/SluggableTransliterator.php

namespace AppBundle\Gedmo\Sluggable;

use Gedmo\Sluggable\Util\Urlizer;
use Remedge\YandexTranslateBundle\Manager\TranslateManager;

class SluggableTransliterator extends Urlizer
{
    /** @var TranslateManager */
    private $translator;

    /**
     * @param TranslateManager $translator
     */
    public function __construct(TranslateManager $translator)
    {
        $this->translator = $translator;
    }

    public function translate($text, $separator = '-')
    {
        $text = $this->translator->translate($text, 'ru-en');
        return self::urlize($text, $separator);
    }
}

It is important not to redefine the transliterate function in this case, because it's static - it's better to choose a different name, like translate as in the example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question