In this article I will explain everything about making your theme multi-language compatible, multi-language plugins, wordpress multi-language source code and much more. This post is helpful if you are a wordpress developer or else just a wordpress user.
Multi-Language WordPress Installers
WordPress source files come in many different language. The default one which you find in wordpress.org is US English. You can download spanish, french, chinese etc versions of wordpress installer. This versions have their admin panel with that particular language and also allow you to write post and pages content in that particular language.
In the international(multi-language) versions of wordpress you will find two things common:
- .mo file: You will find a file in wp-content/languages directory with a .mo extension. It contains the US english translated strings into the international language.
- define (‘WPLANG’, ”): You will find a define function in wp-config.php file with first argument set to WPLANG and second argument set to the desired language for which you have to .mo file. Its a way to ask wordpress to change its language from US english to the specified language and also to use the .mo file.
These two things change the whole wordpress installation language. Your theme and plugins will now know which language users are desiring for wordpress installation.
Making your WordPress theme multi-language
There are strings which are static in your theme. For example: placeholder of search box is assigned to “Search Here”. WordPress doesn’t know how to translate these strings. You need to define a different .mo file with the translation to all these strings.
There are three steps to build a multi-language compatible theme. Steps are:
- .mo file(optional): You need to generate .mo files for your theme representing different languages. And then ask wordpress to load the desired .mo file using load_theme_textdomain function. Its like you can create a options page and let user choose what language is desired. For every different language you must have a different .mo file.
If you are a theme developer then it is optional to generate this .mo files. You can leave this task to the theme user to generate these .mo files using some software or wordpress plugins. Its obviously difficult to learn many language to produce translation of your theme strings and also you don’t know what language your theme user wants.
Wrap translatable text(required): All these static strings must be wrapped around translatable functions like _e, __, _n, _x, _ex or _nx. When you load a .mo file for your theme these wrapped strings are matched and translated. Therefore its must for every theme developer to wrap all static strings around one of these functions. Even if you are not providing .mo files with your theme you must wrap the strings so that plugins can find the static strings and produce translated .mo files.
If you are a plugin developer then also you must wrap all static strings in your plugin using one of these functions.
- charset and lang attributes(required): Make sure meta tag charset attribute and html tag lang attribute are generated dynamically. lang attribute must be equal to the value of WPLANG and charset must be equal to the charset provided my .mo files or default(wordpress installing language charset). Populate charset using bloginfo(“charset”) function and lang attribute using language_attributes().
Generating .mo files
While installing a international version of wordpress you will get the .mo files attached. But themes and plugins mostly don’t come with .mo files. As a wordpress, theme and plugin user you need to generate .mo files for your theme.
There are many ways to generate it. I usually do it using Codestyling plugin. Its scans for translatable strings and uses google translater to produce .mo files for many different languages which makes the theme and plugins translatable. This plugin loads the .mo files using load_plugin_textdomain.
For this plugin to work your plugin and theme strings must be wrapped with translatable strings.
All the things we discussed till now allows us to create a wordpress site of anyone particular language. What if we want the reader to choose the language on frontend. Everything we discussed till now is based on the language choosen by the site admin which makes the site capable of only one language.
There is a another popular plugin called as WPML which also does the same thing as codestyling plugin. It has a extra feature that is it allows you to have multi-language versions posts and pages and and lets the reader select language on frontend widget. This extra features are achieved outside the core wordpress functionality.