support

Localize Your WordPress themes and plugins with GetText

Localize Your WordPress themes and plugins using GetText with some easy steps. If you want your blog to be in a number of languages, the your theme and texts in plugins should be localized, as well as the post contents. We’ll show you how you can achieve this with the GetText package.

Localizing your plugins allows you to reach the widest audience possible.It only takes a little effort to make your plugins & themes translation ready and is easier to do.

GetText and WordPress

GetText is a a text translation utility. It’s free and comes built on any web server. WordPress makes comprehensive use of it and all WordPress localization is based on GetText.

There are a few steps to localization with GetText:

  • Wrap each text in a GetText call
  • Create a list of texts
  • Translate that list to different languages
  • Tell WordPress to use the translation

 

Once these are done, the same page can be displayed in different languages. The PHP code will display the strings in the selected language, while the formating of the page remains the same.

WordPress uses these  three types of files used in the Gettext framework:

POT (Portable Object Template) files: The first step in the translation process is using a program to search through the WordPress source code to pick out text passed into a __() or __e() function, generating a POT file. This file will contain all the text available for translation.

PO (Portable Object) files: The second step involves translating the text in a POT file into the target language, saving both English and translator messages in a PO file.

MO (Machine Object) files: In the last step, the PO file is converted into a machine readable format.

Adding a Text Domain to Theme

In order to prep your theme for localization, you need to include the following function in your theme’s functions.php file.

load_theme_textdomain( 'my_theme', templatepath.'/languages' );

Change “my_theme” to the name of your theme and “templatepath” to the location in your themes folder where you want to save your translation files.

Adding a Text Domain to Plugins:

Add the following function to your plugin’s main file.
The load_plugin_textdomain function should be called in the WordPress init hook, like so:

function ap_action_init()
{
// Localization
load_plugin_textdomain('my_theme', false, dirname(plugin_basename(__FILE__)));
}

// Add actions
add_action(‘init’, ‘ap_action_init’);

 

Localize Your Theme or Plugins

The next step is editing the text strings in each of your theme files and turning them into functions so they are translatable. To do this, you just need to wrap them in a __() function call.
echo 'Hello world!';
becomes
__('Hello world!', 'yourtheme');

If your code echoes the string to the browser, use the _e() function instead, so
echo 'Hello world!';
becomes
_e('Hello world!', 'yourtheme');
You can also wrap regular text. For example
Hello world!
would become
_e('Hello world!','yourtheme');

Creating a POT File

Now that your theme or Plugin files are ready, the next step is to create a POT file. POT files are often created by theme or plugin authors and delivered along with themes or plugins so translators can translate files using Poedit.

There are a few steps involved here. For this tutorial we’ll use Poedit, an easy to use open source program available for Mac OS X, Windows and UNIX/Linux.

1. Install Poedit.

2. Open Poedit and go to File > New Catalog.

3. Fill in your project’s information in the Translation properties tab of the Catalog Properties window.

4. Next, go to the Sources paths tab where you’ll need to enter the path for the folder Poedit will use to search for source files containing translatable text.It will be your “language” folder of your plugin or theme.

5. Click on the Sources keywords tab. Here, we need to define the functions we used to localize the text in our theme files, __() and _e().

6. After you click “OK” you’ll be asked to name and save your POT file.Name your file after you theme or plugin and give it a .pot extension and save it in a folder named “languages” in your theme or plugin directory.

7. When you click “OK” Poedit will scan the folders you specified in the Sources paths tab and will list the localized text in your theme’s files.

8. Last step – save your POT file.

Now your theme is translation ready!

Check out the GNU `gettext’ utilities to find your language and country codes.
Get Poedit.

© 2008-2021 Copyright Startbit IT Solutions Pvt. Ltd.(Formerly known as Vivacity InfoTech Pvt. Ltd.) | All Rights Reserved.
Loading...