Symfony Translation → disable translation engine in presenter

2 months ago

steelbull
Member | 207
+
0
-

I want to create a possibility in application to disable the Kdyby\Translation engine for make possible to see the translation shortcut string.

Does exist any way to disable translation somewhere in presenter?

Thx.

2 months ago

Gappa
Member | 94
+
0
-

Maybe turning off the whole extension in the config for a while? That should work AFAIK.

2 months ago

ali
Member | 313
+
0
-

May be good idea to can set factory in config.
https://github.com/…xtension.php#L109

2 months ago

steelbull
Member | 207
+
0
-

ali wrote:

May be good idea to can set factory in config.
https://github.com/…xtension.php#L109

…but how can It help me in my case for temporary disable the translation?

2 months ago

steelbull
Member | 207
+
0
-

Gappa wrote:

Maybe turning off the whole extension in the config for a while? That should work AFAIK.

But I need to turn off the translation only temporary and only for one logged in user. For example, I want to create the on/off translation button, because some users contribute on translation and in case of mistake they need to know the right translation shortcut.

2 months ago

steelbull
Member | 207
+
0
-

…any other idea?

2 months ago

David Matějka
Moderator | 5960
+
0
-

you can write a custom translator decorator (also implementing ITranslator interface), which optionally returns given translation string without passing it to a inner translator.

2 months ago

steelbull
Member | 207
+
0
-

I created something like this, but I don't know, how to correctly use it in presenter, because when I will register it into the neon like through the implement routine, then it will inject the factory into the presenter, not instance of SwitchableTranslator.

<?php

use Kdyby\Translation\ITranslator;
use Kdyby\Translation\Phrase;
use Kdyby\Translation\TemplateHelpers;
use Kdyby\Translation\Translator;
use Latte\Runtime\IHtmlString as LatteHtmlString;
use Nette\Utils\IHtmlString as NetteHtmlString;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Loader\LoaderInterface;

/**
 * Class SwitchableTranslator
 */
class SwitchableTranslator implements ITranslator
{
    /** @var Translator */
    private $translator;

    /** @var bool */
    public static $switch = true;

    /**
     * SwitchableTranslator constructor.
     * @param Translator $translator
     */
    public function __construct(Translator $translator)
    {
        $this->translator = $translator;
    }

    /**
     * switchOn method.
     */
    public function switchOn()
    {
        self::$switch = true;
    }

    /**
     * switchOff method.
     */
    public function switchOff()
    {
        self::$switch = false;
    }

    /**
     * @var string
     */
    private $defaultLocale;

    /**
     * @var string|NULL
     */
    private $localeWhitelist;

    /**
     * @param LoggerInterface|NULL $logger
     */
    public function injectPsrLogger(LoggerInterface $logger = NULL)
    {
        $this->translator->injectPsrLogger($logger);
    }

    /**
     * Translates the given string.
     *
     * @param string|Phrase|mixed $message The message id
     * @param int|array|NULL $count The number to use to find the indice of the message
     * @param string|array|NULL $parameters An array of parameters for the message
     * @param string|NULL $domain The domain for the message
     * @param string|NULL $locale The locale
     * @return string|NetteHtmlString|LatteHtmlString
     * @throws InvalidArgumentException
     */
    public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL)
    {
        return self::$switch ? $this->translator->translate($message, $count, $parameters, $domain, $locale) : $message;
    }

    /**
     * {@inheritdoc}
     */
    public function trans($message, array $parameters = [], $domain = NULL, $locale = NULL)
    {
        return self::$switch ? $this->translator->trans($message, $parameters, $domain, $locale) : $message;
    }

    /**
     * {@inheritdoc}
     */
    public function transChoice($message, $number, array $parameters = [], $domain = NULL, $locale = NULL)
    {
        return $this->translator->transChoice($message, $number, $parameters, $domain, $locale);
    }

    /**
     * @param string $format
     * @param LoaderInterface $loader
     */
    public function addLoader($format, LoaderInterface $loader)
    {
        $this->translator->addLoader($format, $loader);
    }

    /**
     * @param array $whitelist
     */
    public function setLocaleWhitelist(array $whitelist = NULL)
    {
        $this->translator->setLocaleWhitelist($whitelist);
    }

    /**
     * {@inheritdoc}
     */
    public function addResource($format, $resource, $locale, $domain = NULL)
    {
        $this->translator->addResource($format, $resource, $locale, $domain);
    }

    /**
     * {@inheritdoc}
     */
    public function setFallbackLocales(array $locales)
    {
        $this->translator->setFallbackLocales($locales);
    }

    /**
     * Returns array of locales from given resources
     *
     * @return array
     */
    public function getAvailableLocales()
    {
        return $this->translator->getAvailableLocales();
    }

    /**
     * Sets the current locale.
     *
     * @param string|NULL $locale The locale
     *
     * @throws InvalidArgumentException If the locale contains invalid characters
     */
    public function setLocale($locale)
    {
        $this->translator->setLocale($locale);
    }

    /**
     * Returns the current locale.
     *
     * @return string|NULL The locale
     */
    public function getLocale()
    {
        return $this->translator->getLocale();
    }

    /**
     * @return string
     */
    public function getDefaultLocale()
    {
        return $this->defaultLocale;
    }

    /**
     * @param string $locale
     * @return Translator
     */
    public function setDefaultLocale($locale)
    {
        $this->defaultLocale = $locale;
        return $this->translator->setDefaultLocale($locale);
    }

    /**
     * @param string $messagePrefix
     * @return ITranslator
     */
    public function domain($messagePrefix)
    {
        return $this->translator->domain($messagePrefix);
    }

    /**
     * @return TemplateHelpers
     */
    public function createTemplateHelpers()
    {
        return new TemplateHelpers($this);
    }

    /**
     * @param array|NULL $whitelist
     * @return null|string
     */
    public static function buildWhitelistRegexp($whitelist)
    {
        return ($whitelist !== NULL) ? '~^(' . implode('|', $whitelist) . ')~i' : NULL;
    }
}


/**
 * Interface ISwitchableTranslatorFactory
 */
interface ISwitchableTranslatorFactory
{
    /**
     * create method.
     * @return SwitchableTranslator
     */
    public function create();
}

2 months ago

steelbull
Member | 207
+
0
-

The solution is modify the neon by following way:

- SwitchableTranslator(@Kdyby\Translation\Translator)

But, another question: if I wrap the translated string into the <span>, for example <span title=“translation.shortcut”>Translated text</span>, does exist any way to do not escape all translated texts inside latte?

Thx.