Wordpress Multisite And Localization
At work we’re tring to create a multisite, internationalized, multi-language Wordpress installation for a client, and I immediately ran into issues getting i18n working out of the box on network sites with a custom theme. Wordpress has good(ish) documentation about i18n and gettext but it’s missing a lot of detailed instructions on how to get your multisite installation set up with multilanguage support in your custom theme. So I thought I’d make an attempt at providing them here.
Install Wordpress Multisite And Set Up Your Language Sites
I won’t go into detail here on how to get WP MS up and running – you can find that here – but install, set up, and create your sites.
What To Put In functions.php
First off, you need to instruct your theme to use your translations. Create a directory called “languages” (or whatever you want to call it) in the root of your theme directory. Then, in functions.php, insert the following code, replacing “your_theme_directory” and “languages” with your theme name and translations folder, respectively:
add_action( 'after_setup_theme', 'theme_textdomain_setup' );
function theme_textdomain_setup() {
load_theme_textdomain('your_theme_directory', get_template_directory() . "/languages");
}
This code instructs the theme to add an action on the after_theme_setup hook. That action loads the theme’s translation files into the domain “your_theme_name”
Use A Plugin To Manage Your Strings
PoEdit is a mess to use, but I found a Wordpress plugin called CodeStyling Localization which scans your theme files for translatable strings and loads them into an administrative interface for you to edit, It seems to work really well so far, although I needed to do some setup to get it running. Basically, you need to;
Wrap your theme’s translatable strings in __ notation and specify a text domain, which should be the your theme’s directory name:
<?php echo __('Your string here', 'your_theme'); ?>
<?php _e('Your string here', 'your_theme') ?>
Also, make sure you’re not getting errors in the plugin itself. I got textdomain errors for a while before I realized I needed to add the domain to every string and to the function in functions.php. You may also get errors until you hit “rescan” and generate a .mo file from your strings.
You Need The Wordpress Core Language Files
Next, download the version of wordpress you need for localization. For the french version of the site, for example, it’s at http://fr.wordpress.org. Copy the .po and .mo files from wp-config/languages into your_theme/wp-content/languages (create this directory if it doesn’t exist). This enables Wordpress to use the language in the network site you’re setting up.
You Need To Create .po And .mo Files For Your Theme
Go into the plugin and edit your strings. I won’to go into detail here on how to use the plugin, since its webpage explains it pretty well. Generate your .mo files once you’re done editing and navigate to the Network Admin – NOT the regular WP admin, the site settings under “My Sites” in the Admin bar – for your site.
You Need To Set The Site’s WP_LANG
Under the Settings tab on this page, down at the bottom, there is a variable called WP_LANG - set this to the country_language code you want to use for the site. For example, French (for France) is fr_FR, just like the .mo and .po files you downloaded from wordpress. Hit save.
Note that if you did not move the .po and .mo files into wp-content/languages this option will not be saved. I was mystified as to why the option would come up blank after saving, until I realized that Wordpress needs translation files before you try setting the language for a network site. It’s a shame there’s no error messaging around this – it would have saved me a bunch of time.
All Done
Once you do all of this, the administration panel and site should appear in your desired language. Repeat this process for the languages you want to use and the sites you want to use them on.
So far I have not tried to set a different language for the admin panel as the frontend of the site, but it should be possible, since there are plugins that (apparently) do this. Hopefully this will all be helpful for other developers trying to get multisite Wordpress working with translations.