These are some notes for how to set up your custom plugin and/or theme to be translation friendly. As a WordPress developer, if your custom theme has English strings that are hard coded or stored in the code base, you can set them up so they can be localized and translatable.
Ultimately the files involved in the this process are:
.pot – this file is the template that translators can use to make a .po file
.po – this file is what ties the English text to the language of desired translation.
.mo – this is the .po file that’s been compiled so WordPress can use/ execute it.
Properly escape the string.
In your code base, instead of just echoing out the string, you’ll want to escape it in a translation friendly way. This can be done like so..echo __("View more articles", "my-text-domain");
The first parameter is the string and the second is the text domain. This adds the string to that text domain and it will be later reference with the .pot
(Portable Object File) file.
Organize the text domain.
You’ll notice that second parameter when we escape a string is the text domain. You’ll want to organize all your strings in your theme or plugin into a text domain. You will use this text domain when you name your .pot
file.
Create a .pot file
All localized versions of your custom theme or plugin will be created using a .pot
file. This file is usually consists of the following:
– The string to translate (msgid)
– An empty string value. (msgstr)
– A comment to note the location of the string in the code base
An example might look like so…#:/my-theme/blocks/articles.php
msgid "View more articles"
msgstr ""
When you are ready to save your .pot
file, be sure to incorporate the text domain as a part of the name, WordPress is expecting this. my-text-domain.pot
Create your localized .po file
This is the file that will contain the one to one translations for each string for the desired language. So if you are creating a language and localization for es_US
which is Spanish localized to the United States, then this file will contain your English and Spanish strings. This .po
file will have a list of code blocks like so..
#:/my-theme/blocks/articles.php
msgid "View more articles"
msgstr "Ver más artículos"
It’s important to save the file with the following title format my-text-domain-es_US.po
Place the translation files.
WordPress expects the translation files to be placed in the /languages/
directory in the root folder. You can also create /themes/
and /plugins/
inside the /languages/
folder for further organization.
Compile the .mo file
The .mo
file is the machine code version of your .po
file and is the actual file that gets executed. In order compile a .mo
file, follow these steps.
- – From the command line, navigate to the folder with the .po file.
- – Install
gettext
if you don’t have it. (for example,brew install gettext
) - – From the command line, run
msgfmt -o es_US.mo es_US.po
- – Verify that this produces a file called es_US.mo, in this example.
Testing
There are some browser extensions you can use to switch your localization and language. You can even use Chrome Developer tools to test this too.
Comment section