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.
"_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.
"__" 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
"__" 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.