Results 1 to 7 of 7

Thread: Use translations in view

  1. #1
    Bjorn121 is offline Junior Member
    Join Date
    May 2009
    Posts
    10

    Default Use translations in view

    I've setup everything and it works, but I have some questions left.

    My languages are in a folder /languages/LANG/resources.ini. It's an ini file (I'm normally a Java developer ), which is a bit easier to work with. (It doesn't matter that much either).

    What I do now is this:

    Controller:

    Code:
    $this->view->trans = $translate;
    Page:

    Code:
    <?php echo $this->trans->_('register.form.firstname') ?>
    Problems with this:

    You have to repeat it in every Controller-method. You can do it in 1 method, but still it's quite tedious. And is it possible to shorten the command, to echo the text from the ini file?

    Is it possible to create those translations somewhere central (base action or controller), so all pages and controllers can access it?

  2. #2
    Tekerson is offline Senior Member
    Join Date
    Jul 2008
    Posts
    288

    Default

    You might want to have a look at the view helper that already exists Zend Framework: Documentation
    Brenton Alker
    PHP Developer - Brisbane, Australia

    blog.tekerson.com | twitter.com/tekerson | brenton.mp

  3. #3
    whisher is offline Member
    Join Date
    May 2009
    Location
    Kakiland
    Posts
    32

    Default

    Hi.
    I'm managing with the same issue so
    ...

    I set up this code

    [PHP]
    class Zend_View_Helper_I18n {
    public function i18n($key)
    {
    $fc = Zend_Controller_Front::getInstance();
    $lang= $fc->getRequest()->getParam('lang');
    //include file
    $adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
    $translate = new Zend_View_Helper_Translate($adapter);

    return $translate->translate($key);
    }
    }
    [/PHP]

    or in the bootstrap (In this case I'm stuck
    How can I catch the lang and manage the include)
    but imho a better way you set an only instance of Zend_Translate / Zend_View_Helper_Translate
    [PHP]
    protected function _initDoctype()
    {
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->doctype('XHTML1_STRICT');
    $adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
    $translate = new Zend_View_Helper_Translate($adapter);
    $view->prova= "prova";
    $view->i18n= $translate;
    }
    [/PHP]

    Just looking for advice.
    Bye.

    [EDIT]
    [PHP]
    class Zend_View_Helper_I18n {
    protected $adapter= null;
    protected $translate= null;
    public function i18n($key)
    {
    if($this->adapter === null && $this->translate === null){
    $fc = Zend_Controller_Front::getInstance();
    $lang= $fc->getRequest()->getParam('lang');
    //include file
    $this->adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
    $this->translate = new Zend_View_Helper_Translate($this->adapter);
    }
    return $this->translate->translate($key);
    }
    }
    [/PHP]
    Far better
    Imho a good and quick way to use Zend_Translate.
    Last edited by whisher; 06-13-2009 at 07:06 PM.

  4. #4
    Bjorn121 is offline Junior Member
    Join Date
    May 2009
    Posts
    10

    Default

    Nvm, see post below
    Last edited by Bjorn121; 07-11-2009 at 01:38 PM.

  5. #5
    Bjorn121 is offline Junior Member
    Join Date
    May 2009
    Posts
    10

    Default

    Is it necessary to register this in EVERY controller? I added this to my Index controller:

    Code:
    $translate = new Zend_Translate('ini', '../app_frontend/languages/nl/resources.ini', 'auto', array('scan' => Zend_Translate::LOCALE_FILENAME));
    $translate->addTranslation('../app_frontend/languages/en/resources.ini', 'en');
    $translate->addTranslation('../app_frontend/languages/fr/resources.ini', 'fr');
    
    Zend_Registry::set('Zend_Translate', $translate);
    If you add it to Zend registry, one might think that that's sufficiƫnt. Apparently not . When I access my register page (which is handled by RegisterController), the texts appear as keys, not as translations.

    Is there a way of adding it to 1 central place and make sure that all pages can access the translate object? Because I think it's rather tedious to add it to every controller.

  6. #6
    Tekerson is offline Senior Member
    Join Date
    Jul 2008
    Posts
    288

    Default

    Since you're adding it to the global registry anyway, you can do this in your bootstrap.
    Either in a bootstrap _initTranslate() (or similar) method for >1.8 or in your index.php for <1.8.
    Brenton Alker
    PHP Developer - Brisbane, Australia

    blog.tekerson.com | twitter.com/tekerson | brenton.mp

  7. #7
    thomas is offline Senior Member
    Join Date
    Aug 2008
    Posts
    174

    Default

    First:
    Initiate Zend_Translate in bootstrap and add it to the registry using the key 'Zend_Translate'.

    Second:
    When Zend_Translate is within the registry then there is no need to initiate the translate view helper... all done automatically.

    @Wisher:
    Your code is a big overhead. Use bootstrap and add to registry. No need for view helper. It fetches from registry automatically.

    Also it is no good way to overwrite the existing view helper. Own classes should extend existing classes and not overwrite them. They should also not use the Zend namespace.

    @Bjorn:
    Look at the manual... there is no need to use scanning when you don't scan. Also there is no need to say locale is in the filename when you add the files manually and there is no locale in the filename.
    Greetings
    Thomas Weidner
    I18N Team Leader, Zend Framework
    http://www.thomasweidner.com

Similar Threads

  1. Fake missing translations get logged for Zend_Validators
    By midix in forum Internationalization (i18n) & Localization (l10n)
    Replies: 2
    Last Post: 07-29-2010, 08:51 AM
  2. Are view variables available in placeholder view helpers?
    By sawatdee in forum Model-View-Controller (MVC)
    Replies: 7
    Last Post: 04-09-2010, 11:52 AM
  3. Zend Translate - log missing translations
    By Nastareger in forum Internationalization (i18n) & Localization (l10n)
    Replies: 7
    Last Post: 03-06-2009, 09:37 PM
  4. Problems with cookies and translations
    By zaphod in forum General Q&A on Zend Framework
    Replies: 3
    Last Post: 02-16-2009, 06:37 AM
  5. access control & translations used in a modular directory structure
    By ignace in forum General Q&A on Zend Framework
    Replies: 0
    Last Post: 01-30-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •