Blog

A Nifty Way to Include a Foreign Language Section to Your WordPress Website (Part 1)

3

Today I want to write about a topic not so much concentrating on SEO as on expanding the horizons of a WordPress site. More particularly, there are some cases in which one would want to have sections of their WordPress site in more than one language, for example when they want to target an international audience for their products or services. I am going to discuss an alternative way of accomplishing this that I think can be useful in some cases.Multilingual Sites Are Common Nowadays

1.Standard way
2.Alternative way
3.Should there be a follow-up post?

The most standard and thorough way to implement a translation of a section or a whole of a website into another language is by creating a WordPress multisite (a name that is considerably less confusing than the former “MultiUser”). As explained in the article cited above, this is a way to (virtually) add independent sites to your existing WordPress website as subdomains (e.g. subdomain.mysite.com) or subdirectories (e.g. mysite.com/subdirectory).

This is particularly useful for our end goal as we can create a separate subdirectory or subdomain for each language that we want to have a section translated in. Be careful though – additional research is required when choosing between subdirectory vs subdomain. For example, if you have an SSL certificate which doesn’t cover subdomains, you might want to go with the subdirectory option. After having correctly set up WP Multisite, you can (and that’s what you have been driving at) set a different language in each sub-area (notice – not by manually adding a .mo file and modifying the wp-config.php file as usual, but by going to “Settings->General subpanel” in your Dashboard). In fact, this is one of the suggested tactics in WP’s multilingual manual:
“A separate site is created for each language you want to translate into (e.g. in a WordPress Multisite installation).”

The pros of the standard way are obvious – it is a comprehensive solution for having your website translated into any number of languages. The cons however, although trivial and technical, become just as apparent when one reads WP’s manual for creating a multisite – you simply might not match the prerequisites for setting a multisite up. Since prerequisites are different for the subdirectory and subdomain implementations, I decided not discussing in detail the principle choice of subdirectory vs subdomain above – you might not have a choice at all. There are quite a few prerequisites that can trip you along the way, but most notably:
1.You might not be able to set up wildcard subdomains (required for subdomain installation) if you are using shared hosting.
2.You might have your WP site up for more than 1 month (which is the case with most WP sites anyway) – in this case you are not going to be able to install a multisite as a subdirectory implementation because of a bug in the current version of WordPress.

The alternative that I am going to suggest has the advantages that it is much easier and should always work. On the downside, since your translated section stays within the same site, and each WordPress site has only one blog, you can only use this alternative to translate pages, not posts. So if that suits your needs (or if you cannot meet the prerequisites for a multisite installation), please read on.

1.With the simple installation of the Custom Permalinks Plugin you can for all intents and purposes mimic subdirectories on your website. Without this plugin, WordPress does not allow you to alter the permalink of each individual page such that it includes an extra “/”. For example, if you name your page “Test”, you can alter its permalink to mysite.com/langtest, but not mysite.com/lang/test. The plugin linked to above solves this problem and we can successfully create “subdirectories” such as /de, /it, /ru, etc.

2.We need to make sure that all content on each page in the new language section of the website is in the appropriate language – including header, footer and sidebars. Let’s imagine that we are translating a portion or all of an English pages on a WP site into Italian. For this purpose:

create a new page template, it_page.php. You can do that by simply going to wp-content/themes/your_current_theme from the FTP account of your hosting and creating the file “it_page.php” there. Then it will automatically appear in Appearance->Editor in your WordPress Dashboard.

You can copy over the code from page.php into it_page.php, replacing anything that would have content in English – such as, typically, get_header() and get_footer(). Replace these with get_header(‘’italian”) and get_footer(“italian”). Why that works will be explained in the next step. It goes without saying, be careful of any content in the original language that is hardcoded into the template or other function calls that will produce output in the original language. Replace those, too.

*Do not worry about any function calls to widget areas that have English content, we will deal with these later.

Create files (through the FTP again) header-italian.php and footer-italian.php. As explained in the WordPress function reference repository, naming a template “header-{name}.php” will make it recognizable under “name” for the get_header($name) function. Same rule applies for get_footer().

Copy over from the default headers and footers into the Italian ones and make sure that all content is in Italian.

Give names to your page.php and it_page.php templates so that WP can programically recognize which one is which.
-At the top of page.php include:
update_option(‘current_page_template’,'page’);
-At the top of it_page.php include:
update_option(‘current_page_template’,it_page’);
-Then in functions.php you can write a function like:
function is_italian_page()
{
if(strcmp(get_option(‘current_page_template’),’it_page’)==0)
{
return true;
}
else
{
return false;
}
}

Install the “Executable PHP Widget” Plugin. With its help, you can execute php code from within widget areas by replacing your text widgets with “PHP code” widgets. Transfer all your content in the widget areas that are in text widgets and should be translated into “PHP code” widgets. Then, use the is_italian_page() function to determine whether to serve English or Italian content with the widget.

Please, leave comments and share your opinions whether you find this post useful. If you do, I will share 2 more interesting tricks in a follow up post:
1.How to create your own smart flags that link translated pages. By “smart” I mean that a flag (Italian or English if we follow our example) appears (let’s say in the header) only if a page has a translation. Also, the flag links to the translated version of the page in the corresponding language (as opposed to transferring the user to the home page of the website in the corresponding language).
2.How to solve a known (and very annoying!) bug in WordPress with percent-encoding URLs with non-latin characters in them (which you might want to use if you are translating your website say in Russian).

  1. Leah
    Leah11-28-2011

    Great tutorial! Recently, one of my clients wanted to do just this – with the exception of having separate domain names for each translation (.co.uk, .eu, .de, etc) instead of putting them into subdirectories. For his purposes, he wanted to appear to be an international corporation with European contacts, not just an American firm. A $30 plugin called Professional Domain Mapping did the trick (not everything in WordPress is free yet.)

    I am very interested to see your followup post on Smart Flags so please post away!

    • Vladislav Todorov
      Vladislav Todorov11-28-2011

      Thank you, for the comment, Leah! And for the info:) And, wow, 30$ plugin sounds like a hell of a price:p Have a good day!

Leave a Reply to Vladislav Todorov Click here to cancel reply.


2 * = ten

Optilocal, Ltd., Professional SEO&SEM | 415-800-4888 (USA) | | Sitemap