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

Command translations? #7

Open
trasher opened this issue Dec 18, 2015 · 6 comments
Open

Command translations? #7

trasher opened this issue Dec 18, 2015 · 6 comments

Comments

@trasher
Copy link

trasher commented Dec 18, 2015

Hello,

I'm trying to get gettext translations from symfony commands (in twig templates that send mails, for instance). Mails are correctly translated when sent from the main app, but remains in english when sent from a console command.

Looking at this issue, i've found that we should use the following to get that working with symfony's translations:

$this->getContainer()->get('translator')->setLocale('fr');

What should I do with gettextbundle to get that work as expected?

Thank you :)

@trasher
Copy link
Author

trasher commented Jan 20, 2016

Hi,

ANy hints on how I could resolve that issue? Thank you :)

@trasher
Copy link
Author

trasher commented Jan 20, 2016

I guess I've found a part of a solution:

diff -up vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Resources/config/services.yml.patch vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Resources/config/services.yml
--- vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Resources/config/services.yml.patch 2016-01-20 09:17:13.780380229 +0100
+++ vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Resources/config/services.yml   2016-01-20 09:18:34.319413250 +0100
@@ -12,3 +12,4 @@ services:
     arguments: [%gettext.locale_shortcuts%,@router, %kernel.root_dir%]
     tags:
       - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
+      - { name: kernel.event_listener, event: console.command, method: onConsoleCommand }
diff -up vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Routing/GettextLocaleListener.php.patch vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Routing/GettextLocaleListener.php
--- vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Routing/GettextLocaleListener.php.patch 2016-01-20 09:17:40.197063060 +0100
+++ vendor/leaseweb/gettext-translation-bundle/Lsw/GettextTranslationBundle/Routing/GettextLocaleListener.php   2016-01-20 09:53:39.274297866 +0100
@@ -5,6 +5,7 @@ use Symfony\Component\Routing\RouterInte
 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 use Symfony\Component\Routing\Exception\InvalidParameterException;
+use Symfony\Component\Console\Event\ConsoleCommandEvent;

 /**
  * Gettext locale listener
@@ -61,6 +62,28 @@ class GettextLocaleListener
       }

       $this->setupGetText();
+  }
+
+  /**
+   * Binds the default domain to the combined translations on Kernel Request
+   *
+   * @param GetResponseEvent $event event
+   *
+   * @throws InvalidParameterException
+   */
+  public function onConsoleCommand(ConsoleCommandEvent $event)
+  {
+      $currentLocale = $this->getCurrentLocale();
+      //$requestedLocale = $this->getRequestedLocale(); //hardcoded for now since I do not know how to retrieve correct one :(
+      $requestedLocale = 'fr_FR';
+
+      if ($requestedLocale && $currentLocale != $requestedLocale) {
+          if (!setlocale(LC_MESSAGES, $requestedLocale.'.UTF-8', $requestedLocale.'.utf8', $requestedLocale.'.utf-8', $requestedLocale.'UTF8')) {
+              throw new InvalidParameterException("Requested locale '$requestedLocale' could not be set. Is this locale installed? Hint: Execute 'locale -a' on the Linux command line to list installed locales.");
+          }
+      }
+
+      $this->setupGetText();
   }

   private function getCurrentLocale()

With this patch, I'm able to get my french translations from a console command. But I've hardcoded the locale; I do not know how to retrieve it unfortunately...

@foxsdev
Copy link

foxsdev commented Jan 20, 2016

Seems like a good lead to allow translation easily.
It's just a shame for hardcoded part... And obviously without a request, you can't define the locale to use... so you have to define a "default_locale" to use, maybe in you app parameters ?

@trasher
Copy link
Author

trasher commented Jan 21, 2016

Well, there already is a parameter for the default locale; but I do not know how to get it from the Listener class... :)

@wujku
Copy link

wujku commented Oct 3, 2016

Any ideas to solve this issue? @trasher

@trasher
Copy link
Author

trasher commented Oct 4, 2016

@wujku no sorry... I've used the "hardoced" solution I've found, but I'm no longer using the bundle for now.

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

3 participants