I’ve internationalised my plugin and am now looking at how best to load the plugin’s translated strings. I’m including myplugin/languages/myplugin.pot with my plugin but am not planning to distribute .po or .mo files.
My question:
Is it my job as a plugin author to use a function such as load_plugin_textdomain()
to load translated strings or is this the job of my plugin’s end user?
The reason why I’m asking:
If I were to use load_plugin_textdomain()
, passing a third argument like in the example below will load myplugin{$locale}.mo from myplugin/languages. However, the plugin end-user will need to supply this myplugin{$locale}.mo file but, of course, when I issue a plugin update, that myplugin{$locale}.mo file will be overwritten.
function myplugin_load_textdomain() {
load_plugin_textdomain( 'myplugin', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'myplugin_load_textdomain' );
Ref: https://codex.wordpress.org/Function_Reference/load_plugin_textdomain
EDIT – apparently this is all wrong so please disregard.
You can use the WP_LANG_DIR
constant to load translations from the update-safe languages directory first, and fall back to your plugin languages directory for any plugin-supplied translations. The default location is wp-content/languages
, and users can set WP_LANG_DIR
themselves in wp-config.php
. When you load translations from multiple sources, the first found instance will be used, so user translations will always override any plugin-supplied translations, and allow users to do partial translations without translating all strings.
function your_plugin_load_plugin_textdomain(){
$domain = 'your-plugin';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
// wp-content/languages/your-plugin/your-plugin-de_DE.mo
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . "https://wordpress.stackexchange.com/" . $domain . '-' . $locale . '.mo' );
// wp-content/plugins/your-plugin/languages/your-plugin-de_DE.mo
load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'init', 'your_plugin_load_plugin_textdomain' );