3

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

2 Answers 2

0

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 . '/' . $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' );
3
  • If I have no translated strings to distribute with my plugin, is it necessary to make the call to load_plugin_textdomain()? Aside from myplugin.pot, there's nothing in myplugin/languages. Commented Mar 5, 2016 at 17:39
  • it's not required if there's nothing there to load.
    – Milo
    Commented Mar 5, 2016 at 19:30
  • How this work with translations packs and GlotPress? Why do you use load_textdomain with WP_LANG_DIR and not load_plugin_textdomain() also?
    – cybmeta
    Commented Mar 9, 2016 at 7:58
1

You need to:

  • Call to load_plugin_textdomain(). (See How to translate your plugin).
  • Supply the language catalogue myplugin.pot and all the localized translation mo files you want to distribute
  • If the user want to override some translation file, he/she should use any of the available filters to do it, for example gettext filter, the user should not to modify the translation file directly.
  • But really, you are required only to set any string into a gettext function with a textdomain matching the slug of the plugin. You are not required to include any language file, specially if you submit the plugin to the plugin directory because it will be imported into GlotPress in translate.wordpress.org where anyone can translate your plugin to any language and the system will generate a language pack that WordPress will download if necessary while the plugin installation. When a language pack is upadated, the admin will be noticed about the update, just like updates for plugins and themes work but separated from the plugin update.
  • Do not load languages files from WP_LANG_DIR directly in your plugin as overriden system like it is suggested in other answers. It can break the load of language packs from translate.wordpress.org if the mo files share the same folder under WP_LANG_DIR. Additionally, loading multiple mo files, one overriden another, can slow down your site.
3
  • Regarding the third bullet, why is supplying the default myplugin.mo file necessary? Commented Mar 5, 2016 at 17:36
  • It is not necessary, I was wrong. Answer edited.
    – cybmeta
    Commented Mar 5, 2016 at 19:19
  • Answer updated.
    – cybmeta
    Commented Mar 9, 2016 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.