- Bootstrapping Zend_Translate with a LangSelector Plugin
- How to make POEdit detect source strings in Zend Framework
- Configuring Poedit for Zend Framework Projects
- Bootstrap Zend_Translate
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.
Personalize:
Your name & email – Fill these in
Editor:
You can leave all the options as their defaults, including the Line endings format [Unix]
Translation Memory:
Leave this as is for now.
Parsers:
Select PHP and click Edit.
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.
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
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;
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.
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 sv_SE.mo 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!
Hi.
Very nice, but I already knew that
I use factory methods to create form elements:
…
->addElement(
‘text’,
‘username’,
array(
‘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?
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 🙂
yes its great, ‘value’ => _(‘Submit’), used in form factory and it was scanned by parser in poedit.
While probably not exactly what you want php2po, which is part of the Translate Toolkit:
http://translate.sourceforge.net/wiki/toolkit/php2po
Can convert common PHP files used for localisation into PO files.
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?!
Hi, there is a discussion about this subject Here
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’, ‘Pronađeno je %s auto oglasa’, $count)), $count);
turns to
$translate->translatePlural(‘Pronađen je %s auto oglas’, ‘Pronađ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.
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!
I couldn’t get Poedit 1.4.6 working on Windows 7 until I duplicated the base path into the path list. See http://stackoverflow.com/questions/3709146/using-poedit-with-xampp.
Remember to also include application within the path list!
Really nice post!
I am looking forward to read all your articles on about the Zend Framework.
Thanks for you.I read this post and use poedit and gettext adapter in my project .Thanks
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?
@Stamrecht make sure all the parameters are as typed above also check out the comments for some hints. It does work 🙂
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!
Very true 🙂
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.
Thanks Sandy!
Sie können PO-Dateien auch mit poeditor.com lokalisieren.
is it better to insert the path and its new iteme (applcation) at end step other it dint recoganize the key words
Just what I was looking for. Saved me a lot of time, thanks
Saved my day. Works fine with #ZF2 too – of course!
This saved me lot of time. Before I read this article it took me a long time then to figure this one out.
Thanks