load_plugin_textdomain()
Loads the .mo translation file from the specified folder. Does not work with MU plugins.
The .mo file must be named: TRANSLATION_DOMAIN-LOCALE.mo
where locale is the language code (see get_locale()). For example, if TRANSLATION_DOMAIN = my-plugin
and the Russian language has been selected LOCALE = ru_RU, then the files should be named: my-plugin-ru_RU.mo
and my-plugin-ru_RU.po
.
It is recommended to call the function through the hook plugins_loaded
.
Since version 4.6. the function first tries to load the .mo file from the WP_LANG_DIR/plugins/
folder usually it's /wp-content/language/plugins
.
To load the translation of the theme, use load_theme_textdomain().
Hooks from the function
Return
true|false
. True when textdomain is successfully loaded, false otherwise.
Usage
load_plugin_textdomain( $domain, $deprecated, $plugin_rel_path );
- $domain(строка) (required)
- A unique identifier to get the translation string.
- $deprecated(string)
- Deprecated argument, works up to version 2.7. Path similar to ABSPATH, to .mo file.
Default: false - $plugin_rel_path(string)
The path to the .mo directory of the file is relative to
WP_PLUGIN_DIR
.If the path is not specified, it will be the root directory of the plugins
WP_PLUGIN_DIR
. I.e. the path to the file will be as follows: WP_PLUGIN_DIR/domain-ru_RU.moDefault: false
Examples
#1 Registering a translation file for the plugin
It is assumed that this code will be located in the main plugin file or in a file that is in the plugin's root directory. If this is not the case, __FILE__
must be replaced by the appropriate path.
Translation file must lie in the directory of the plugin and must be named: my-plugin-ru_RU.mo
.
add_action( 'plugins_loaded', 'myplugin_init' ); function myplugin_init(){ load_plugin_textdomain( 'my-plugin', false, dirname( plugin_basename( __FILE__ ) ) ); }
#2 If we want to put the translation file .mo in the languages subfolder:
add_action( 'plugins_loaded', 'myplugin_init' ); function myplugin_init() { load_plugin_textdomain( 'my-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); }
#3 Register a translation file for the MU plugin
WP has a special function load_muplugin_textdomain().
Changelog
Since 1.5.0 | Introduced. |
Since 4.6.0 | The function now tries to load the .mo file from the languages directory first. |