How to create a .po language translation


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 (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!

Copy from http://www.wdmac.com/how-to-create-a-po-language-translation

Advertisements
By dbglory Posted in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s