Configuring Poedit for Zend Framework Projects

This entry is part [part not set] of 4 in the series Working with Zend_Translate and Poedit

There are a few steps you need to take to configure poedit to work with a Zend Framework project properly. I will take you through the configuration process step by step, and in the end you should have a working installation.

In this tutorial we are on Windows, but the process is the same on Mac & Linux based systems, and poedit even looks much the same on all platforms.

Install poedit and start it, if it’s the first time you run it you should now see a Preferences dialog.

Your name & email – Fill these in
You can leave all the options as their defaults, including the Line endings format [Unix]
Translation Memory:
Leave this as is for now.
Select PHP and click Edit.

PHP Language settings

Make sure your dialog matches the one above exactly!

Now click OK twice and you are done with the preferences.

The main poedit window will now come up,  click File -> New Catalog, you should now see a settings window.

Project Info:

Fill in your Project name and version and the rest of the fields making sure you select Charset and Source code charset to UTF8 and selecting the language and country of the translation you are going to create, in my case Language: Swedish and Country: SWEDEN.

Project Info

Now select the Paths tab, and add your projects base path. In my case C:\Zend\Apache2\htdocs\testbench then click the New item tool and add; application

Project Base and application path

Now select the Keywords tab and click the New item tool and add;

  • translate
  • _
  • setLabel
  • setValue
  • setMessage
  • setLegend
  • _refresh
  • append
  • prepend

(Note: If you have any other keywords that come to mind, feel free to comment and I’ll add them to this tutorial)

Now you click OK and the Save as dialog comes up move to your project application directory and select or create the languages directory the path should look something like C:\Zend\Apache2\htdocs\testbench\application\languages and save the file as sv_SE.po (replace this with the language/locale code that you have choosen.)

Now your source code will be scanned after the keywords you specified earlier and the Update Summary dialog will be showing all the strings it detected;

Update Summary

In this example the strings where caught from;

$this->headTitle()->prepend($this->translate('TestBench Application -'));
<?php echo $this->translate("Welcome %s, your last login was %s",$this->user['name'],$this->user['active']); ?>

in my layouts/scripts/layout.phtml file.

When you click OK on the Update Summary Dialog you will be taken to the main poedit window where you can translate the strings.

Main window

As you can see it’s very easy to work with simply enter your translations in the bottom text box.

Now after you are done you simply click File -> Save and two files will be written to your languages directory, in my case sv_SE.po and where the .mo file is the compiled version that Zend_Translate uses.

Now if you add new strings to your source code you simply load poedit and open your sv_SE.po file and select Catalog -> Update from sources and it will again show you the Update Summary dialog with all new string as well as changed strings and removed (Obsolete) strings.

There are a ton of good Zend_Translate references out there, google is your friend!

Hope this helps, enjoy!

Series Navigation

26 Replies to “Configuring Poedit for Zend Framework Projects”

  1. Hi.

    Very nice, but I already knew that

    I use factory methods to create form elements:

    ‘label’ => ‘Username:’

    Is there a way to specify regex to match ‘label’ => ‘?’ in poedit or sth like that? Do you have any suggestions how to translate therse?

    1. Hi,
      Since Zend_Form is Zend_Translate aware via setDefaultTranslator method you can simply wrapp the ‘Username’ part in _(‘Username’) and it will be picked up by poedit and Zend_Form will go Aha!, tests it out, I think you will be happy 🙂

      1. yes its great, ‘value’ => _(‘Submit’), used in form factory and it was scanned by parser in poedit.

  2. What is with plural forms, I can’t force poedit to parse from sources cases like this:

    $translate->translate(array(‘Pronađen je %s auto oglas’, ‘Pronađeno je %s auto oglasa’, $count)), $count);

    so they always go to obsolete strings?!

    1. A bit late answer I guess, but to get Poedit to see plurals you need a new helper function that takes three arguments rather than an array.

      $translate->translate(array(‘Pronađen je %s auto oglas’, ‘Pron­ađeno je %s auto oglasa’, $count)), $count);

      turns to

      $translate->translatePlural(‘Pronađen je %s auto oglas’, ‘Pron­ađeno je %s auto oglasa’, $count);

      As the first one, from what I remember, works for the actual translation (but just doesn’t get picked up by Poedit) you just need a dummy function to pass through the arguments and then call the actual translate() function with an array.

  3. Couldn’t get Poedit 1.4.6 to work properly on win7 32bit with your instructions. It just wouldn’t scan my application folder.
    So I opened xgettext.exe in cmd with -h command, and found that -L php is incorrect as it should be PHP (capitals). Now it scans my folder correctly. Don’t know exactly about other versions, but this might help someone and save them some precious hours!

  4. Really nice post!
    I am looking forward to read all your articles on about the Zend Framework.

  5. I do exactly as you say but I get the messages:

    no files found in: application
    Poedit did not find any files in scanned directories
    entries in the catalog are probably incorrect
    updating the catalog failed….

    Any suggestion?

    If no strings can be found, can I enter them manually?

    1. Thx Danny, it now works. Just for anyone making the same mistake:

      under edit>preferences>parsers>php>edit it should be:

      *.php; *.phtml

      I used a comma as a separator. You can stare at it for hours and still not see it!

  6. You saved my day Stamrecht BV. I set it up a year ago and it took me a long time then to figure this one out. And today again it took me a few hours. I can’t believe it. I am stupid apperently. Even the CAPTCHA for submitting this comment takes me a while.

  7. is it better to insert the path and its new iteme (applcation) at end step other it dint recoganize the key words

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.