zend framework 2 Set TextDomain in onBootstrap

1.5k views Asked by At

I followed the instructions of this link successfully, now my web is multilanguage without requiring put "locale" in the "traslate()" calls. But I have to put the TextDomain each time that I call it.

    $this->traslate("Hello", __NAMESPACE__) //where __NAMESPACE__ is the text domain.

I would like set TextDomain in the onBootstrap method instead of put it in each call of the the "traslate()" helper. I have tried with setTextDomain method, but it doesn't exist. Somebody know how do it?

The onBootStrap Code is following:

   .....//Code for define $locale.
   $sm = $e->getApplication()->getServiceManager();
   $translator = $sm->get('translator');
   $translator->setLocale($locale);
   $traslator->SetTextDomain($textdomain);   //This line not work!!!!!
2

There are 2 answers

0
josepmra On BEST ANSWER

Okey. We have advanced one step.

The first solution works ok (the view solution), now my web page traduce texts only using this helper parameters, being Locale and TextDomain defined by the config:

$this->translate('HELLO');

But the second solution not works. I don't understand because the same plugin is accepted in the view and not in the onBootstrap when the name is the same.

I rewrite my onBootstrap code bellow:

    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $pm = $translator->getPluginManager();   //until here works ok.
    $pm->get('translate');   //this throws an error message how if 'translate' not found.
0
Sam On

Didn't see this right the first time. Going by DASPRIDS Presentation about ZF2 I18N the correct function to call is:

$this->plugin('translate')->setTranslatorTextDomain('module-b');

Though if i see this correctly, that's from within the view Scripts. Getting the Translator from ServiceManager however - i haven't tested this - but try the following:

$translator->getPluginManager()->get('translate')->setTranslatorTextDomain('foo');