Quantcast
Channel: YinPress » Tips & Tricks
Viewing all articles
Browse latest Browse all 19

How to Internationalize a WordPress Theme

$
0
0

We’re living in a world where software made on one side of the world can be used on the opposite side. What this means for you, as WordPress developers, is that it’s important to plan to internationalize your theme from the beginning, so people all over the globe can use it.

As in most regards, WordPress has good support and easy to use infrastructure so you can internationalize you theme with minimum effort.

In this article we’re going to see how can you internationalize your WordPress theme and also how can you provide the translation files for different languages for your WordPress theme. Check out the huge list of languages WordPress supports.

Configuring WordPress for a different language

First lets check how to configure WordPress in a language other than English. To do so, you can download the .mo files for your language from one of the sites listed on the Codex. Then you can place the files for e.g.: – fr_FR.mo for French, de_DE.mo for German, etc, in the wp-content/languages folder of your WordPress installation. Then you can open wp-config.php in your WordPress installation directory and search for the line define('WPLANG', '’);

To change the language of your WordPress installation to French change this line to

define('WPLANG', 'fr_FR');

Once you have done that, you can visit your WordPress site and you should see the language of your site switched to French as seen below, where we see text which comes from the WordPress core.

WordPress in French

Loading the textdomain

Once you’ve changed the WordPress language, you need to specify where can WordPress find the translation files for your theme. You can do it with the help of the function load_theme_textdomain.

If you are making a main theme you can add the following code to your functions.php

add_action('after_setup_theme', 'setup_text_domain');

function setup_text_domain(){

load_theme_textdomain('mythemename', get_template_directory() . '/languages');

}

If you’re using a Child Theme, can add the following code to your functions.php

define('CHILDTEMPLATEPATH', dirname(__FILE__ ) );

add_action('after_setup_theme', 'setup_text_domain');

function setup_text_domain(){

load_theme_textdomain('mythemename', CHILDTEMPLATEPATH . '/languages');

}

In the above code we are using the after_setup_theme hook which is fired just after the theme has done its initialization. You can read more about after_setup_theme on the Codex.

In the function setup_text_domain, we call the WordPress function load_theme_textdomain which takes the domain name for your theme and the path of where to find translation files. The translation files should have the name as .mo. So for French, you should have the file exactly as fr_FR.mo. In the below we’ll see how can you generate translation files!

The Functions

With the text domain setup, we must identify the strings which will be seen by the user in our theme. Once we’ve identified all those strings we, can use one of WordPress’ translation functions.

_e

This function takes two arguments,

  1. the string to translate
  2. the name of the domain

It performs the translation if the translated string is found. In case it’s not found, it’ll return the original string passed as input, then echo on it. So this function should be used when you want to directly print translated strings on screen.

__ – hint: it’s a double underscore

This function also takes two arguments

  1. the string to translate
  2. the name of the domain

This function returns the translated string and does not print it on screen. This should be used when you want to pass translated string to a function.

Now all the strings you want to translate should be used with this function. For example, here’s the code for a sample 404 page

<?php get_header(); ?>

<div id="primary">
	<div id="content" role="main">
		<article id="post-0">
			<header>
				<h1><?php _e( 'Content not found', 'mythemename' ); ?></h1>
			</header>
			<div>
				<p>
					<?php

					$message = __( 'The content you are looking for is not present', 'mythemename' );

					echo $message;

					?>
				</p>
			</div><!-- .entry-content -->
		</article><!-- #post-0 -->
	</div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

You can see the strings are bound with the function _e or __, and the domain is passed to them. If you go to a post which is not present, you’ll see the strings as you have passed it, as you have still not provided the translation file.

Creating the .mo file using Podeit

The translation files are stored as .mo files and are non readable binary files. These can be generated using many programs but we’re going to use Poedit for this example, hop on over and download it.

Once installed, click on new catalog and the following will appear. In the “Project Info” tab fill in details about your project.

using poedit for theme internationalization

Then in your “Paths” tab, add the path of your theme directory. Poedit will scan all files in the directory you specify here.

specifying the theme directory in poedit

In the keywords tab you need to give the WordPress translation functions, so we specify __ and _e here.

specifying the WordPress translation functions in Poedit

Poedit will automatically assess files and find strings which you need to translate. Once that’s done you’ll have to provide the appropriate translation as shown below. Then you can save to generate the .mo files for your theme’s <your theme>/languages folder.

showing the translated strings in poedit

If you have provided the fr_FR.mo appropriately in the languages folder and now if you visit a post which is not present you should see the translated strings as follows

checking out our translated strings in WordPress

More Useful Internationalization Functions

In addition to the functions we’ve used, WordPress provides even more to help you develop themes and plugins with internationalization support.

  • unload_textdomain: This function can be used to unload an already loaded text domain.
  • get_locale: This function can be used to get the current locale of your WordPress system and process based on it.
  • _n: This function can be used for translating the singular and plural form of a string based a some condition.
  • load_plugin_textdomain: This function, like load_theme_textdomain will be used to load the domain for a plugin instead of a theme.

Conclusion

With the world becoming a local market, it’s necessary to plan and make your software for a wide range of customers. Thus, internationalization support in your theme should be planned from the very beginning. WordPress provides the infrastructure and functions to easily provide internationalization support, so there’s no excuse to not provide it.

In case of translating strings, keep in mind to translate according to the context of the message rather than a direct translation.

So have fun while providing internationalization support to your next WordPress theme and you’ll soon rule the world.

The post How to Internationalize a WordPress Theme appeared first on YinPress.


Viewing all articles
Browse latest Browse all 19

Trending Articles