How to Selectively Change the WordPress Language and Locale

Posted on Updated on

The WordPress language and local setting is a site wide variable that is meant to help search engines and other service providers to figure out the primary language of the page. You might think that we can determine the page language automatically from the text inside it, and you would be mostly correct. However, there are a few benefits to explicitly declaring the language.

One big benefit is styling. Different languages might have different white space and line requirements, as well as different numeric separators, quotations sytles etc. All these need to be customized based on the declared language of the page. Similarly, certain fonts and nuances are expected to change from region to region. These include issues like hyphenations for example. Even within a particular page, we can have text in a different language and the browser can decide whether or not to offer translations services inside the page.

Language declaration also has a role to play in accessibility services such as audio page readers. All in all, this can be a pretty important part of your site configuration – and it costs nothing to declare it correctly.

WPLANG option in wp-config.php is Depracated

As of WordPress version 4.0, the “WPLANG” option in wp-config.php is now deprecated. You can still set it, but it will be quietly ignored in favor of the following method.

Declaring the Language in the WordPress XHTML

If you view the source HTML of any WordPress page, you should see something like this at the very top:

In the above example, the very first line with the <html> tag uses the “lang” and “xml:lang” attribute to declare the language of the page. This is a site wide setting that you can change under the “Settings->General” menu of the WordPress dashboard.

The drop down box changes the language of your entire site. In this example, my default language is English (US) – even though I haven’t explicitly set it as such. And here is the reason why.


Setting the “WPLANG” Option in the Database

When you make changes to the language of the website using the drop down box above, it sets the “WPLANG” variable in the “wp_options” table of the main WordPress database. Here’s a screenshot of the WPLANG row in my WordPress table via phpMyAdmin:

You can see that the option hasn’t been explicitly set. And yet, the reason why my site uses en_US is because of this piece of code in the “get_locale” WordPress function:

You can see, that if no language has been explicitly set, the code returns “en_US” by default.

Changing the Language via Code

WordPress provides us with a filter called “locale”, using which we can specify the locale of a WordPress page. For example, let’s say that I want to set the locale of all “single” posts and pages to French. I can use the following piece of code:

function change_wordpress_locale( $lang ) {
  if (is_single())
    return 'fr_FR';
add_filter( 'locale', 'change_wordpress_locale' );

Here, I set the locale to “fr_FR”. You can get a full list of the locale codes to use on this website. Using this filter, you can modify the function to cater to any condition you need. You can set a separate locale for specific categories, for custom post types, or even specific pages and posts if you want.

If you don’t know how to add code to your WordPress website as above, check out my earlier tutorial on the same. The above code will change the language to French on all single pages as shown here:’

The remaining pages will remain untouched and will continue to specify the language as en_US as before. This is a neat and elegant way to programmatically set the language in WordPress for specific pages!

Leave a Reply

Your email address will not be published. Required fields are marked *