Few words about translate WordPress
Problem: How we can translate WordPress themes or plugins?
Solution: We don't translate source code - it's not professionally. We are use special WordPress functions __() and __e(). At the start we should male sure, that our theme or plugins contain this functions in the right places. There are many ways make multilingual WordPress, as a rule this task resolve using plugins, but in our example we'll look at not a standard way where for each additional language it's certain folder with WordPress in the root WordPress folder, that contain main language e.g. :
/
/zh-hans
/ja
...
Also, in our case current language in depend URL param after main domain e.g. domain-name/zh-hans for Chinese or domain-name/ja for Japanese.
Step 1: In the wp-content folder, make folder languages and make into themes folder - wp-content/languages/themes
Step 2: Make file with name - theme_name-locale.po e.g. mythemename-zh-CN.po for Chinese version
Step 3: Add next content to the file:
# Translation of WordPress - 5.2 - Development in Chinese (China)
# This file is distributed under the same license as the WordPress - 4.9.x - Development package.
msgid ""
msgstr ""
"PO-Revision-Date: 2019-05-15 08:00:00+0000\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"X-Generator: \n"
"Language: zh_CN\n"
"Project-Id-Version: WordPress - 5.2 - Development\n"
#. translators: Translate this to the correct language tag for your locale, see
#. https://www.w3.org/International/articles/language-tags/ for reference. Do
#. not translate into your own language.
#: blocks/team/team-list/loop.php:8
msgid "Full Bio"
msgstr "完整简历"
#: blocks/posts/posts-list/filter.php:17
#: blocks/posts/posts-list/filter.php:29
msgid "All"
msgstr "所有资源"
...
it's manual way create *.po file. Also, we can use plugin as LocoTranslate or editors as Poedit for automatic way. Manual way is very useful when should translate few strings and you know where they location.
Step 4: Convert our file from .po to .mo e.g. there. It's very simple remember - .po for the people, .mo for the machines. After convert move the file to wp-content/languages/themes/ near test-zh-CN.po i.e. we has 2 files test-zh-CN.po and test-zh-CN.mo.
Step 5: We should set current locale. It can be done 2 ways:
First way. Set current locale in admin Dashboard - Settings - General, but dashboard will be has on current language. This way may be not comfortable if content manager, admin, other users from team, who work on site don't know current language e.g. Chinese.
Second way. Set current locale in functions.php, here are using function is_admin() we can set translate or don't translate admin Dashboard:
<?php
add_filter( 'locale', 'translate_localize_theme' );
function translate_localize_theme( $locale ) {
if ( stristr( $_SERVER['REQUEST_URI'], 'zh-hans' ) && ! is_admin() ) {
return 'zh_CN';
}
}
in this case if URL contain sub-string zh-hans and current page not admin Dashboard, WordPress set Chinese locale and do translate content of the site according our test-zh-CN.mo.