» » » Adding Translation Files To Your Child Theme

Adding Translation Files To Your Child Theme

posted in: WordPress | 0
Facebook
Google+
https://applerinquest.com/adding-translation-files-to-your-child-theme">
LinkedIn
Follow by Email

Last week, I received the urgent project from my client. The site needed to be online within one day. The client requested only two pages which were the front page and contact page. Also, I needed to add all content and design the template as well as the color theme for them. Finally, the site has only one language which is Thai Note that the method in this post can apply to any languages.

I decide to use the Virtue Free WordPress theme. It is a free theme and I have been using it for a few years without any issues. However, the theme doesn’t come with the Thai language pack so I need to create the Thai translation for this theme.

Keep in mind, you should create the child theme if you want to customize the theme or you plan for the customization in the future. So you don’t lose any customization if your theme is updated. If you don’t know how to create the child theme, please follow this link.

Okay, now you should have the parent theme and child theme already. Next, we will talk about how to create the translation to your child theme.

 

Translation Ready

Most of the themes are the translation ready which means you can use the theme in your own language if the theme provides your language translation or you can translate the theme into your own language if no translation for your language.

For example, for the Virtue free theme, if your site is a Spanish site when you change the site language in WordPress backend, you will see the frontend shows the Spanish language because the Virtue theme provides the Spanish translation which can be found in “themes/virtue/languages/es_ES.po” and “themes/virtue/languages/es_ES.mo”. If I change the site language to Thai, there is no Thai translation on the front end because there is no th.po and th.mo.

 

What is PO file and MO file

Now I mention the po and mo file above. So what are they? I explain in short below.

  • PO – Portable Object. This is the file that you receive back from the translator tools. It’s a text file that includes the original texts and the translations.
  • MO – Machine Object. The MO file includes the exact same contents as the PO file. The two files differ in their format. While a PO file is a text file and is easy for humans to read, MO files are compiled and are easy for computers to read.
a source from https://www.quora.com/What-are-PO-MO-files-and-what-are-they-for

 

The POT file

The themes with the translation ready come with the POT (Portable Object Template) file. This file contains the original strings (in English) in the theme. It can be found in the languages folder under the theme folder. In my case, it can be found in “themes/virtue/languages“. It names “virtue.pot“. So what is POT file? It is the Portable Object Template. This is the file that you get when you extract texts from the website. Normally, you send this file to your translators. Then those translators will send your the po and mo file for each language. For example, es_ES.po and es_ES.mo for Spanish.

a source from https://www.quora.com/What-are-PO-MO-files-and-what-are-they-for

 

Create the PO and MO files from POedit tool

Finally, it is time to create our own language translation. I often use the POedit tool to create the po and mo files from the POT file. Go ahead download and install in your computer. The link is here.

Once your install is done. Follow the steps below.

  • Open the POedit tool
  • Choose “Create new translation” option
  • Choose the POT file from the parent theme. In my case, it is “virtue.pot” where can be found at “themes/virtue/languages“.
  • You will see the popup asking you which language you want to translate. In my case, I choose “Thai” and the language code is “th” The language code is very important. It needs to match the language code that WordPress uses on the front end and back end. After you change the site language at the WordPress backend (Settings>General>Language), go to the frontend and use the web inspect tool from your browser and check the lang attribute at HTML tag. For example, <html lang=”th“>. Or you can print out the current language with this PHP command => get_bloginfo(‘language’).
  • Now you can start to add your translation in the translation box and save into your child theme at “themes/your-child-theme/languages“. The output after saving will be the po and mo files. In my case, it is th.po and th.mo.

 

Load our translation files in functions.php

From the last step, we create the po and mo files for our language. Next, we will load our translation in the child theme. Don’t forget to activate your child theme in Appearance menu at the WP backend. Then follow the step below.

  • Paste the PHP code below into the functions.php in your child theme. The path will be “themes/your-child-theme/functions.php“.
function child_theme_slug_setup() {
    load_child_theme_textdomain( 'parent-theme-slug', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'child_theme_slug_setup' );

child_theme_slug_steup is a function callback name. You can change the name whatever you want.

parent-theme-slug is your parent theme text domain. It can be found in “themes/your-parent-theme/style.css”. In my case, the parent text domain is “virtue”.

You should hook to after_setup_theme action.

 

See your translation on the front end

  • Next, go to the front end and refresh the site (F5), you should see your translation text now.

 

What else you should know

  • The languages folder is a folder default for WordPress. So WP knows where to find the translation files. But if you want to use the other folders, you can set with “Domain Path” in the style.css of your current theme. More detail is here.
  • The translation file is NOT overridden as same as the template. Meaning, for the child theme, you can copy the template from the parent theme and edit that template to fit your needs and store in the child theme. WP will look at the copied template in the child theme and show that template on the frontend. But for the translation file, if you copy the es_ES.po and es_ES.mo from the parent theme and add those files into your child theme, WP still look at the es_ES.po and es_ES.mo from the parent theme. So if the theme you use, don’t have 100% complete translation, you may help to add the translation to the theme for your language.

 

Useful Links

 

Tips

If you create the new plugin and want to use the translation file from the current theme, you can use the text domain from the current theme in the plugin. Then you can add the new plugin path in the POedit following the steps below.

  • add new plugin folder that refers to the theme text domain. Open the POedit then choose the Catalog menu and go to Sources paths tab. Add the new plugin folder path and click OK button.
  • after adding the new plugin folder, click on “Update from code” menu. You will see the new source code and ready for translation.
  • then changes what ever you want and save to update the PO file and modify the MO file.
Facebook
Google+
https://applerinquest.com/adding-translation-files-to-your-child-theme">
LinkedIn
Follow by Email