po files and mo files - translation of wordpress theme and plugin
For WordPress theme authors and plugin authors, translating their own themes and plugins cannot avoid po files and mo files.
po file is the translation file of the theme and plugin. However, WordPress does not use this file because WordPress uses mo file.
The mo file is a binary file generated from the po file.
Therefore, to translate your own themes and plugins, you need to edit the po file and then convert the po file into a mo file.
Steps to translate your own themes and plugins:
1. Create a "languages" folder in the root directory of the theme or plugin.
2. Create a po file, for example: zh-CN.po
3. Edit this po file. The file content structure is as follows:
msgid "Title"
msgid "Title"
msgid "Time"
msgid "Time"
Note: msgid is the text in the code. msgstr is the translated text.
4. After translation, convert the po file into a mo file
There is an online conversion tool: https://po2mo.net/
5. Upload all the languages folder, po files and mo files to the server.
6. Add to the functions file of theme or plugin:
function the_languages_setup()
{
//~ Load localized language file
load_theme_textdomain('textdomain', get_template_directory() . '/languages');
}
add_action('after_setup_theme', 'the_languages_setup');
In this way, in the code, "Title" displayed by _e('Title','textdomain') or __('Title','textdomain') will be translated into "Title".