This blog will introduce the multi-language production of WordPress
You first need to create a languages folder in the Wp-content directory to hold the language pack files.
Then in the template directory of the functions.php, and then add the function in the code to load the folder, the sample code is as follows:
Add_action ('after_setup_theme'alanhou_setup'); function Alanhou_setup () { load_theme_textdomain ('alanhou', Get_template_ Directory. ' /languages ' );}
The above code indicates that the. mo file is loaded in the language file, which is the/wp-content/languages/folder, when the topic loads.
load_theme_textdomain (theme localization)
Loads the translated subject string.
If there is a. mo file under the local theme root directory, the file is included in the translated string of $domain.
The. Mo file must be named according to local settings.
<?php Load_theme_textdomain ($domain, $path)?>
Parameters
-
$domain
-
(string) (required) a unique identifier used to retrieve the translated string
-
default value: None
-
$path
-
(unknown) (optional) The directory where the language pack. Mo file is located (no trailing slash)
-
Default value: False
The value returned
-
(BOOL)
-
returns TRUE if Textdomain can load normally, and FALSE if
There are two ways to use multi-language settings, one is to set it directly, and the other is to set it by clicking the link.
Way One,
Add_action (' After_setup_theme ', ' my_theme_setup '); function My_theme_setup () { load_theme_textdomain (' My_ Theme ', Get_template_directory (). '/languages ');}
Two, if you want to call the different languages by the parameters in the URL, such as Www.example.com/?l=zh_CN call Zh_cn.mo, then you can refer to the following example:
Change the local language//Must call Add_filter (' locale ', ' my_theme_localized ') in front of Load_theme_textdomain (); function my_theme_localized ( $locale) {if (Isset ($_get[' l ')) {return $_get[' L '];} return $locale;} Set the theme language path//language pack should access the my_theme/languages/directory//Wordpress own language pack placed in the wp-content/languages/directory Load_theme_textdomain (' My_ Theme ', TemplatePath. '/languages ');
How does WordPress know which language file to read? is actually in its wp-config.php file,
Add your language and language environment code to define
. If you want to translate your theme into German, you can do this:
Define (' Wplang ', ' de_de ');
how it works.
when you usually make a theme, you simply need to encode any subject text, such as 404.php The 404 error message in the file , or a label such as "Comments:" or "Author:". If the user's WordPress is German, these text fragments will still be displayed in English. the solution to this problem is to use one of the four WordPress functions to return or echo the statements, and these four functions are designed to reference the correct text of the language file. Once the text is wrapped in these functions, you can create a file that contains all the translations that were referenced each time the theme was loaded. There are three translation files that we use:
- . Pot (Portable object template) -This is a template file that contains a reference to each text string that needs to be translated in the theme. This file does not contain any translations. This is a plain text file.
- . PO (Portable object) -consisting of a . pot file, the. PO contains all the string references and their translations to a particular language. This is also a plain text file that can be edited.
- . Mo (machine Object) -binary version of the. po file. by using machine code, files can be used faster than their plaintext selections.
Poedit is a great program that lets you create your. pot files and also provides an easy-to-use graphical user interface that you can use to make your. po files, and more importantly. Mo files.
first, you need to download Poedit, where you can find Windows,mac and Linux: http :
www.poedit.net/download.php
once the Poedit installation is complete, you can create your. pot file. To do this, go to File > new directory. You will see a dialog box where you need to enter some basic information.
After the above steps do not unexpectedly you will be in your WordPress dashboard backstage setting inside Select website language to see just add language, select on can.
Finally, you use the function _e () wrapped in your code to implement multiple languages for the translated entry.
<?php _e (' No posts found ', ' inthy.com ');?>
Reference website: https://jinwensay.wordpress.com/2009/12/02/make-your-wordpress-theme-translatable/
WordPress Multi-language support (localization)