Making it Translatable – Internationalize / Localize WP Themes Guide – Part 2

As I said there’s not only one method to internationalize a software but WordPress uses the GetText method & it’s the one we should use for the theme.

So let’s start the internationalization then next attack the localization

Note: It is highly recommended to choose / make an English theme & internationalize it, much easier than to work with a non-English theme.

Internationalization needs an edition of the PHP files of the theme, those found in the theme’s directory. index.php, archive.php, 404.php, single.php are just examples & all themes don’t have the same number of files. You also may want to make a theme from scratch & make it internationalized.

First you’ll have to open each of the files in a basic text editor like Notepade, or a software like Crimson Editor, which is a notepad with tags highlights and many other useful features. NVU or Dreamweaver also would be fine.

Now that you opened one of the files, start looking for words & sentences that are supposed to change if the theme language changed. You should as much as you can avoid to alter anything that looks like PHP tags, you only will modify text that will be printed in the pages generated when the site is opened in a web browser. Don’t forget to make a backup of the theme first.

How to modify?

First you will need to add a line to the functions.php file of your theme, if this file doesn’t exist you need to create it, add the line below in the beginning, & only one line-break between it & the other functions.php content, my site crashed because of an extra line-break :| :

<?php load_theme_textdomain('text_domain'); ?>

You see the “text_domain” above? It’s called the text domain name, you can type anything there, your theme’s name for example, but you should use the same name with all the terms & phrases you will internationalize (see below)

GetText has two functions: the "_e" & the "__" (two underscores) functions.

The "_e" function is used for text that was not previously inside a PHP function. Example:

In your 404.php you could find something like this:

<h2>The page you're looking for doesn't exist</h2>

That’s what will be printed as the title of the 404 page, you’ll need to internationalize the phrase “The page you’re looking for doesn’t exist”, to do so you’ll wrap it with some PHP tags including the "_e" function, the result will be this:

<h2><?php _e("The page you're looking for doesn't exist", "text_domain"); ?></h2>

Notice again the text domain name (text_domain) above, remember that it should be the same as in the functions.php file.

Now the "__" function is used to make translatable a text that is already wrapped with PHP tags, in fact I didn’t experiment much the way it works in some cases, I mostly used the "_e" till now. To give you an example:

<h2><?php the_content('Read more...'); ?></h2>

We want to make “Read more…” translatable, so we change the code above this way:

<h2><?php the_content(__('Read more...', "text_domain")); ?></h2>

Again replace “text_domain”, with the text domain name you put in functions.php.

Note: The text inside the "_e" and "__" functions must be in English.

You should open all the PHP files of the theme & internationalize using the two functions, when finished it’s the end of the internationalization phase.

Note: You may find that some parts of the PHP files are already internationalized but without a text domain name added, you could add the text domain name to them, but I think that most of time it’s safe to leave them as they are, because those parts would be translated using the localization files of the core of WordPress & don’t need the files we’ll create to localize the theme (more details in the next part).

Next Part: Translation with POEdit.

11 Responses to “Making it Translatable – Internationalize / Localize WP Themes Guide – Part 2”

  1. Internationalize & Localize a Wordpress Theme - Guide for Dummies - Part 1: Introduction | Gettin' Geek Says:

    [...] « Disable Ctrl+W (Close Tab Shortcut) / Restore a Closed Tab – Firefox 3 Making it Translatable – Internationalize / Localize WP Themes Guide – Part 2 [...]

  2. Translation with POEdit - Internationalize / Localize WP Themes Guide - Part 3 | Gettin' Geek Says:

    [...] that you made your theme translatable, let’s do the necessary to translate [...]

  3. Martti Says:

    I am having trouble localizing Carrington Mobile. That is: a plugin and a theme. I managed to translate Carrington Mobile theme and plugin. At least almost. There are still some words here and there which didn’t appear on the poEdit.

    For example:
    “about”, “more ?”, “Return to the Mobile Edition”, “Categories: Uncategorized”
    are still untranslated. Any suggestions?

    It appears there are two project names: “carrington” and “carrington-mobile”. So, I should make two sets of .po and .mo files. Am I missing something? Where should I put the different files or should I somehow merge them? And how is this possible?

    http://wordpress.org/extend/plugins/wordpress-mobile-edition/
    http://wordpress.org/extend/themes/carrington-mobile

  4. MuMu Says:

    Hi, honestly it’s being a while since I last worked on internationalization / localization.

    My guess is that those terms are not internationalized properly or not internationalized at all. You might need to go back to the php files & check them.

    For the projects, each one should have its own mo & po.

    Good luck! :)

  5. How to make a translatable WordPress theme Says:

    [...] Making it Translatable – Internationalize / Localize WP Themes [...]

  6. Como hacer un theme WordPress traducible | Ayuda WordPress Says:

    [...] Making it Translatable – Internationalize / Localize WP Themes [...]

  7. ???? » ????????WordPress?? Says:

    [...] Making it Translatable – Internationalize / Localize WP Themes [...]

  8. Anointed Support » Blog Archive » Multilingual support Says:

    [...] Making it Translatable – Internationalize / Localize WP Themes [...]

  9. ajian Says:

    I see the Note:”more details in the next part”,But,Where is the” next part”?

    Best reguard
    Ajian 2009.12.25

  10. MuMu Says:

    Hello,

    The link was in the 2nd comment, I added one in the end of the post. Sorry for the confusion.

  11. Tutorial para criar seu tema com internacionalização | joaobarroca.Net Says:

    [...] Parte II http://gettingeek.com/making-it-translatable-internationalize-localize-wp-themes-guide-part-2-71.htm… [...]

Leave a Reply