Useful fuction get_bloginfo() for WordPress multilingual site

Using WorPress, it's possible to create easily multilingual site with its plugins such as WPML or Polylang.

Using the original index.php file

If you want to use different index.php files for different languages, you might be able to use different pages for different language versions. For example, you can just create pages templates for each language, by copying the content of the index.php file and pasting it into page-english, page-chinese, page-korean, and so so.  And you need to change the top section of each page templates to the following format:

php
/*
Template Name: the name of template (for example, Front Page for English)
*/

get_header(); ?>

Once you created page templates for each language using the original index.php, you  need to create pages for each language and then specify the template for them. (For example, you can can create an English page and then specify "Front Page for English" as a template for this page.)

Using the get_bloginfo() function

However, it might be difficult to control multilingual version using the above method in some cases. For example, if you want to add a different "category title" depending on language in the category page:
Category per language in wordpress
You might think to use the is_category() function to control each language version. However, this method may be complicated if there are many categories or they are changed frequently.

You also do not want to change the original index.php file. In this case you can use the get_bloginfo() function. Various parameters can be used in this function. The parameter we want is "language." It's possible to control contents or elements based on language using get_bloginfo('language'):

<?php if( get_bloginfo('language') == 'en-US' ) : ?>

// Code for English version

elseif( get_bloginfo('language') == 'ko-KR' ) : ?>

// Code for Korean



// Code for other language version

endif; ?>

If you want to check the language codes (e.g. en-US, ko-KR), you can just use the following code as a test:

echo get_bloginfo('language');

Then, you can check the language code for each language used in your site.

In addition, if you add get_bloginfo('language') within elements (e.g. class), you can easily control the layout for each language with css.

Please refer to this WordPress Codex page for more about get_bloginfo().


Leave a Comment