forked from DevinVinson/WordPress-Plugin-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Internationalization
Moisés Barrachina Planelles edited this page Oct 4, 2023
·
2 revisions
## Preparing the text
For specify a text that maybe needs translation, WordPress provides the functions:
* __('string', 'translation domain/plugin id'): for direct translation
* _e('string', 'translation domain/plugin id'): for direct translation and display the translated text
* esc_html_('string', 'translation domain/plugin id') for translation and escape the HTML characters
* esc_html_('string', 'translation domain/plugin id') for translation and escape the HTML characters
For more functions search on the WordPress documentation: [link here](https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/)
With that, WordPress will use a translation file if it exists and has that sentence, or a external plugin will be able to translate your plugin into the visitor language
BFT also provides the option of use its intermediary functions to avoid having to put the domain in every string prepared for a future translation. With ALL the WordPress translation functions
For that, call the function through '$this' and don't put the translation domain
* $this->__('string'): for direct translation
* $this->_e('string'): for direct translation and display the translated text
* $this->esc_html_('string') for translation and escape the HTML characters
* $this->esc_html_e('string') for translation, escape the HTML characters and display the resulting text
NOTE: the functions allow the domain field, if you put a domain then that domain will be use
The translation files are allocated in plugin_folder/languages, BFT automatically will set WordPress to search translations on that folder
The language files are:
* .pot: Portable Object Template, the master file with all the strings
* .po: Portable Object, the file with the strings translated to one language
* .mo: Portable Object, Machine Object, the compiled data of the .po file, WordPress use this file
Steps for translating a plugin:
- Create a new .pot file of your plugin
- Update/merge the original .pot with the new .pot file
- Delete the old .pot files and rename the new merged file if needed
- Prepare the .po language file 1. If it's a new translation language: duplicate the .pot file and change the name and extension to the designed language, like bft-pro - copy.pot to bft-pro-es.po or bft-pro-es_ES.po 2. If it's a existing translation language: update/merge the file with the new .pot file
- Translate the sentences of the .po file
- Create the .mo file from the .po file
To create and merge the files and translate the sentences you can use programs such as [Poedit](https://poedit.net/) or [EazyPo](http://www.eazypo.ca/)