How to create a .po language translation Published on April 13, 2010

Reduzir tamanho de letra Aumentar tamanho de letra Impressão optimizada do artigo Enviar por email

This article is about creating language translations for word press developments, both for plugins and themes.

However, the described .po creation method using poedit is “roughly” universal, thus this post can be used as an universal tutorial about creating .po library (catalog) files for any purpose, not just WP

I found there is a lot information on the web about editing .po files but very little about creating your own .po from start. This was the main purpose of this article.

Internationalizing Your work

Once you have the programming for your theme, plugin or widget done, another consideration is internationalization.

Internationalization is the process of setting up software so that it can be localized; localization is the process of translating text displayed by the software into different languages. WordPress is used all around the world, so it has internationalization and localization built into its structure, including localization of Plugins.

Please note that language files for Plugins IS NOT automatically loaded. This is what you need to add to the Plugin or Theme’s code to make sure the language file(s) are loaded:

For plugins:

- Add this code to your plugin:

add_action( 'init', 'your-unique-function' );

function your-unique-function() {
load_plugin_textdomain( 'your-unique-name', false, 'your-unique-name/languages' );
}

For themes:

- Add this code to your theme’s functions.php:

load_theme_textdomain( 'your-unique-name', TEMPLATEPATH.'/languages' );


$locale = get_locale();
$locale_file = TEMPLATEPATH."/languages/$locale.php";
if ( is_readable($locale_file) )
require_once($locale_file);

General Usage:

add this to your php files that need translation:
To fetch a string simply use __('String name','your-unique-name'); to return the translation or _e('String name','your-unique-name'); to echo the translation.

POEDIT – How to create the .po translation files:

1. Download and install poedit (http://www.poedit.net/download.php)

2. Open POEDIT: On the top menu click: file > new catalog (terms may vary, depending on the language of your POEDIT)

3. On the new opened dialog box, enter the name of your project, the team’s (your) name, your e-mail, the language, and the encoding. DO NOT enter anything else, especially the country, or your plugin will by country dependent instead of language dependent.

4. Still on the same dialog box, click the tab “Paths” (the 2nd tab), and enter the path where your php files are, so the library know where to scan for translations. Then in the icon buttons click “new entry” (wait for the tooltip to know for sure), enter dot “.” (without the parenthesis “” of course), and click in the empty space in the dialog box, so the system assume it.

5. Still on the same dialog box. click the tab “Keywords) (the 3rd tab), and in the icon buttons click “new entry” (wait for the tooltip to know for sure). Then enter two underscores “__” and click in the empty space in the dialog box, so the system assume it. Do the same again but this time enter “_e”

6. Click OK and you’re done!

The typical question now is:

- So how do I actually enter the strings to be translated?

- You don’t!
- If you have done the above steps correctly, POEDIT will scan your PHP files searching for the _e('String name','your-unique-name'); and the __('String name','your-unique-name'); and automatically adds the to the original strings to the library. You just need to translate them!

You can force the scan by clicking “Update” in the main menu, but again it will only work if the configs are ok, and if you have at least one string to be translated in you php files. test it with something like: _e('hello','your-unique-name');

Its really easier than you’d wonder at first, but it is not obvious!
The fist time I tried to create my own .po library it took me several attempts and the reading of a lot of tutorials to find that, by exclusion, this had to be the way.

Have fun!

15 comments on “How to create a .po language translation”

  1. [...] How to create a .po language translation | Web Design & Development Tutorials. Word Press develo… [...]

  2. Navin Poeran says:

    Hi,
    Thanks for this great tutorial.

    I’m using WPML, but am kinda stuck.

    In what file should I enter the code you proved? The code that needs to be added to the plugin.

    Thanks a lot.

  3. [...] this article: How to create a .po language translation | Web Design & Development Tutorials. Word Press develo… Tags: consideration, language, languages, [...]

  4. [...] this article: How to create a .po language translation | Web Design & Development Tutorials. Word Press develo… Tags: consideration, language, languages, [...]

  5. kimil says:

    hi!
    I was wondering how to generate the .pot file if the theme I choose doesn’t have any.
    Because if I have no original .pot file, how can I translate it ?

  6. Jeff Rose says:

    Thanks for this tutorial. I saw it a while back and lost it.

    This worked perfectly for my plugin. Wow, I’ve got a lot of phrases in there.

  7. Jason says:

    Great article. Really helped get a grip on first time using Poedit. Thanks.

  8. Florian says:

    Thanks for this tutorial. It brings to me the power of internalize my projects !
    Continue !
    Bye

  9. at.w3bMaster says:

    This was the first and only complete and logic tutorial which helped understand how translations work, and how to add more of them… i was going crazy trying to figure this out

    Thank you very much.

    • WDMAC says:

      Hi at.w3bMaster

      I wrote this tutorial based on my own doubts and problems when I first approached this subject.
      There wasn’t much information available, and the only that existed seemed to be missing “chapter 1”…
      All the existing tutorials made a hole lot of assumptions about your previous knowledge on this subject, wich is terribly wrong when writing a tutorial.

      Glad to be of help!

      MAC :)

  10. Linesh Jose says:

    I had some problems initially, now it’s working. Thanks Mac for this help :)

  11. wayn3 says:

    Hi!

    If you’d like to try out a web-based .po file editor to speed up your language localization process, poeditor.com/ is a great new tool this. I warmly recommend it!

  12. Marc says:

    Thanks. It was helpfull.

Go ahead! Leave your comment on this subject!

Fields marked with (*) are mandatory!

Spam Protection by WP-SpamFree

Resources
Goodies & Infos
Earn Extra Money!

Get an extra income with your vacation photos!

TIP: go to Shutterstock, submit your photos and make an extra income every month. - I do!

MAC

 

Categories

 

External Resources
Twitter Updates
    © 2010 - WDMAC A Blog about Web Design & Development. WordPress development. Tutorials - "Proudly powered by WordPress!"