January 13, 2009

Do you know how many people use themes who do not post in English? Me neither, no idea, but I know that most themes are in English and only English. A premium theme needs to be different, it needs to think through the needs of the user and that means providing localisation. You will be amazed at how easy it us.

Before I begin here is a quick poll. I want to demonstrate, if I can, how many people there are out there who do not use English for either their blog or for the admin area of their WordPress installation:

So, how do you localise a theme? Well WordPress does all the hard work for you. As long as you decide to localise before you start then actually there is almost no work for you to do at all.

Choose a text domain

First you need to choose a text domain. This is a name that will let WordPress know which set of dictionary to use. It can be anything, as long as it is unique. I would suggest using the name of your theme. For this example I will use “Fun with WordPress”.

Tell WordPress where to find the dictionary

Next you need to tell WordPress the name of your dictionary using the load_theme_textdomain function. This should be placed at the top of your functions.php file so it loads everytime WordPress does.

[php]
load_theme_textdomain('Fun with WordPress');

You can include a second parameter in that function which is a path to the directory where the translations will be held. By default it will assume your themes directory.

Start theming

That is almost all the code you need to write. All you need to do now is to mark up your English text so that it gets translated. To do that you use two functions:

[php]
_('This is English text','Fun with WordPress');

and:

[php]
_e('This is English text','Fun with WordPress');

They both tell WordPress to perform a translation (if desired and possible) on the text in the first parameter, using the dictionary in the second parameter. The difference between the two functions is that the first does the translation and returns the result, so it can be used in code, and the second echos the result, so it can be used for normal text. Here’s an example that provides the “read more” text for posts that use the more tag.

[php]

Using this, if someone using your theme uses the more tag to show only part of the content on the home page then they can choose a translation for the text that links to the rest of the post.

Create your .po file

A .po file is the file that will contain all your English words so people can translate them. You don’t need to go through the theme and find all the words there are tools that will do that for you. I Can Localize has a free tool that will even take a zip file to do your whole theme at once.

Promote your post

This is essential. Promote the fact that your theme is localised. There are many people out there who look for themes just to provide the translations. It doesn’t matter if you can’t translate it yourself as long as you have laid the groundwork.

But wait, is it really that easy?

I’ve painted this as being really easy because it is. But of course the devil is always in the details. Premium themes are about attention to detail and so is good localisation. Take the example I showed you above. What happens if you want the text to contain the post name. You can’t simple attach it to the end as you might do with English because other languages might not use the same text order. The sprintf function is a must:

[php]
sprintf( _( 'Read more of %s' , 'Fun with WordPress' ) , the_title('', '', false) );

Now the translation can specify where the title should go as well.

One of the best resources I have come across for dealing with all these issues, including word order, dates, javascript alerts, etc, is called Localizing WordPress Themes and Plugins at I Can Localize. Last time I produced a theme I used it extensively.

If you are producing a new theme I encourage you localise it. Not least because it is the right thing to do, but also (as I hope my poll shows) there are lot of people out there with an interest.



Wordpress title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1736s, 1s ago, in 0.03s.
 __
(__)
   `
 SE7EN (Nice from Thailand)

My blog is in Thai and English but I prefer my WordPress installation and theme in English

 __
(__)
   `
 Simon

A big Thanks, Gracias & Danke, Andrew! Will definitely use it.

 __
(__)
   `
 Stefano, www.wordpressmania.it

Totally agree!
For all non-english bloggers the fact that a theme is i18n ready is definitively a needed feature!
At least the premium themes need to be i18n, but very few are!

Even all the free themes listed at wordpress.org sholud be i18n ready, as WordPress is…

 __
(__)
   `
 Ryan

Thanks Andrew.

I've been wondering how to do this for a while for both themes and plugins.

 __
(__)
   `
 Christoph

I demand every theme to be i18n ready, seriously.
While writing the sourcecode it's very easy to internationalize the theme. Putting i18n in afterwards is a huge timefactor – I did it once for Neil Mertons Web2.0* theme and it took me a whole day to test and bugfix (too bad Neil disappeared from the internet – and so did his theme).

i18n is as easy as making widget-ready sidebars. Too bad most theme authors do not care about widgets and hardcode everything inside their sidebar…


0.01s