Wordpress-rebuild translation file (PO file) for theme and plugin

Wordpress-rebuild translation file (PO file) for theme and plugin

Posted: 10 years ago in  PHP | Wordpress |


Multilingual Website Multilingual contents is a need for e-commerce website, especially in multinational company interesting on cross-border commerce. Understanding that need, most of wordpress themes are translated-ready.


Multilingual Website
Multilingual contents is a need for e-commerce website, especially in multinational company interesting on cross-border commerce. Understanding that need, most of wordpress themes are translated-ready.

However, in case you add in elements, modify the template, extend and create the child themes, how to make new sentences translated as others ?

Where we find the translation file
In each theme/plugin, there is folder named "lang" or "languages" where we find the tools for translation. We will modify the po files (aka matching table of vocabulary) and create mo file (binary file of po file). The filename comprised of languageIdentifier_countryCode.

Every new words/sentences created in template using wordpress function __("stringToBeTranslated") or _e("stringToBeTranslated") would be imported into PO file of every language. And to make it automatically, here is the way to do:

Step 1:
We are using wordpress i18n tool, check it out using SVN

sudo svn co http://i18n.svn.wordpress.org/tools/trunk/ /usr/lib/wpi18n

Tips: You do not have to put in /usr/lib/wpi18n, but if you put elsewhere, make sure to set the $WP_I18N_LIB environment variable in your .bashrc or .bash_profile file:

export WP_I18N_LIB="/path/to/i18n/lib"

Step 2:
Createmakepot script (usually put in /usr/local/bin)

cd /usr/local/bin sudo nano makepot

Input:

#!/bin/bash

if [ ! -d "$WP_I18N_LIB" ]; then
    WP_I18N_LIB="/usr/lib/wpi18n";
fi

if [ -z "$1" ]; then
    projectType="wp-plugin"
else
    projectType="$1"
fi

if [ -z "$2" ]; then
    textdomain=""
else
    textdomain=$2
fi

if [ -z "$3" ]; then
    projectDir=`pwd`
else
    projectDir="$3"
fi

pluginBasename=$(basename "$projectDir")

if [[ ! $textdomain ]]; then
    textdomain="$pluginBasename"
fi

if [ -d "$projectDir/languages" ]; then
    php "$WP_I18N_LIB/makepot.php" $projectType $projectDir "$projectDir/languages/$textdomain.pot"
elif [ -d "$projectDir/lang" ]; then
    php "$WP_I18N_LIB/makepot.php" $projectType $projectDir "$projectDir/lang/$textdomain.pot"
else
    php "$WP_I18N_LIB/makepot.php" $projectType $projectDir "$textdomain.pot"
fi

Make it executable

chmod +x makepot

Step 3:
Go to themes or plugins folder (it is in wp-conten) and run:

  • The script will use the name of the plugin directory as the .pot file's name, unless a textdomain is given.
  • In that case the file will be called textdomain.pot.
  • It will look first for a directory called languages, then for lang and put the file in whichever it finds. If neither of those directories exists, it will put the .pot file in the base plugin directory.

Simplest syntax

makepot

It use the project type by default, we can specify a different project type as an argument.

makepot wp-theme

You can also specify the text-domain as optional second argument

makepot wp-theme textdomain

Last optional third argument is the plugin or theme directory to output files

makepot wp-plugin textdomain path/to/plugin

Step 4:

Create MO file in themes/themename/lang: it will be used by wordpress to lookup translation of string,. Go to themes/themename/lang and run

msgfmt -o filename.mo filename.po

Hint: if msgfmt is not available on your pc, just install from gettext package and you are good to go

sudo apt-get install gettext

Next time, to change the translation, modify the PO file and rebuild MO file.