0

The following code works fine in a controller,how global?

XXXController.php

$lang = 'nl';
$loc = $this->getServiceLocator();
$translator = $this->get('translator');
$translator->addTranslationFile("phparray",'./module/admin/language/lang.array.'.$lang.'.php');    
$loc->get('ViewHelperManager')->get('translate')->setTranslator($translator);

View.phtml

echo $this->translate('homepage');

The awnser:

module.config.php

'translator' => array(
    'locale' => 'it_IT',
    'translation_files' => array(
    array(
        'type' => 'phpArray',
        'filename' => './module/admin/language/lang.array.nl.php',

    ),
    ),
    'translation_file_patterns' => array(
    array(
        'type' => 'gettext',
        'base_dir' => __DIR__ . '/../language/mydomain',
        'pattern' => '%s.mo',
    ),
    ),
),    
Bas
  • 137
  • 11

2 Answers2

1

Set translator in your module config as in Zend Skeleton Application

venca
  • 1,196
  • 5
  • 18
  • What and now? Thats all. When you setup translator just grab mvctranslator service from servicemanager and use it. – venca Jun 07 '15 at 13:49
  • My question was how to do this global, so i want to set it once and use echo $this->translate('xxxx'); in every view. How can i do this? – Bas Jun 07 '15 at 13:52
  • if you have registered mvctranslator in module config, then its passed to translate view helper. – venca Jun 07 '15 at 15:51
  • Ok, but how looks the code? Do you have an example? – Bas Jun 08 '15 at 03:24
  • I think we have misunderstood in configuration. just put to your module config: `return array( 'translator' => array( 'translation_files' => array( array( 'type' => 'phpArray', 'filename' => './module/admin/language/lang.array.nl.php', 'locale' => 'nl', ), ), ), ); ` – venca Jun 08 '15 at 07:34
  • Show me your config (pastebin, gist...) – venca Jun 08 '15 at 08:24
  • This post helps http://stackoverflow.com/questions/25889545/zf2-transalator-print-msgid-not-translated-text Thnks! – Bas Jun 08 '15 at 08:25
0

The awnser!

'translator' => array(
    'locale' => 'it_IT',
    'translation_files' => array(
    array(
        'type' => 'phpArray',
        'filename' => './module/admin/language/lang.array.nl.php',

    ),
    ),
    'translation_file_patterns' => array(
    array(
        'type' => 'gettext',
        'base_dir' => __DIR__ . '/../language/mydomain',
        'pattern' => '%s.mo',
    ),
    ),
),    
Bas
  • 137
  • 11