A Guide for Creating Translatable WordPress Themes

  • Lucy
  • 0
  • Jul 19, 2015
  • Web Development

English is the most illustrated language, used over the Internet, but still there are thousands of other languages available in the World. It is important to add all languages over the internet.

Over the past few years, the demand for transferable themes and plugins is increasing. Therefore, it is necessary to create transferable WordPress themes to facilitate the Worldwide WP users.

If you want to release a theme publicly, then you should consider the localization. Translations are usually carried about by making the use of special code in your WP theme. This file encompasses the actual translations and you can develop one for any language.

How to Translate WordPress?

Terms like Internationalization and localization are used to translate WordPress in any other languages for web users from assorted locales who use disparate dialects and different languages other than English.

Process of Making a Translatable WordPress Theme

Through this blog, you will learn how to transfer the WordPress theme for any language. You need to follow the following steps:

Add the needed functions

You can paste the following lines of coding on your functions.php file.

load_theme_textdomain( 'your_text_domain', TEMPLATEPATH.'/languages' ); $locale = get_locale(); $locale_file = TEMPLATEPATH."/languages/$locale.php"; if ( is_readable($locale_file) ) { require_once($locale_file); }
load_theme_textdomain( 'your_text_domain', TEMPLATEPATH.'/languages' );

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

In the first line, you can see the load_theme_textdomain() function. It enables you to load a Text Domain. It gives you the liberty to choose any name but remember it should be unique and different.

Internationalize your WP theme

Use the php gettext functions to translate WordPress theme. GetText has two functions: _e and __. The _e function is used to print simple text, while __ function is used when the text to be showcased is already covered in PHP tags. Refer to below code block for example:

_e("The page you're looking for doesn't exist", "your_text_domain"); the_content(__('Read more...', "your_text_domain"));
_e("The page you're looking for doesn't exist", "your_text_domain");

 the_content(__('Read more...', "your_text_domain"));

The worst part is that you need to replace each string by the required function. And this process can take a lot of time as it completely depends on how many strings have been used in the theme.

Creating .po file

You can easily translate your WP theme into any language, but to display text in a foreign language, you need to add a .po file. This file stands for Portable Object that incorporates a string, and its translation in another language.

The best part is that you need to find your theme files for all strings that need to be translated. With the help of icanlocalize.com, you can scan all PHP files and create .po files. Then, it will extract all strings enveloped in __("txt", "domain") and _e("txt", "domain") calls.

With the help of POEdit, you can edit the PO files. It is free software which is designed for this particular task.
You have to translate each text string and after this, you can save the .po file.

Implementation

The last step is to define your WP locale. And, you can do it by getting your language and country code.

For example:

If your language is French and France is your residential Country, then your code will be fr_FR. With the support of GNU get text manual that contains pages, will help you find both the Country and language code.

After getting language codes open your wp-config.php file and hunt for the WPLANG constant. If it is available then replace the current code by yours and if it doesn’t exists, then paste the following line with your code.

define ('WPLANG', 'fr_FR');
define ('WPLANG', 'fr_FR');

That was quick and easy, do share your views for the same.

Related Posts

Written by Lucy

Lucy Barret is a Sr. Wordpress Developer at WPGeeks - a HTML to Wordpress company. She handles all the major projects of HTML to Wordpress conversion with proven track records of achieving more than 94% client retention.

View all posts by: