How to save a translation of a plugin in “CodeStyling Localization”?

You should move the po- and mo-file with the translation of your plugin outside your plugin’s directory. Whenever you update your plugin, your plugin files are replaced causing any file that is not part of the default plugin package to be deleted. (If you are translating your own plugin, you could as well add the translation files directly to your plugin repository.)

How to move custom translation files outside of the plugin’s directory? In your plugin code, add a method like this to the init hook:

public function load_plugin_textdomain()
{
    $domain = 'my-plugin';
    $locale = apply_filters('plugin_locale', get_locale(), $domain);

    load_textdomain($domain, WP_LANG_DIR.'/my-plugin/'.$domain.'-'.$locale.'.mo');
    load_plugin_textdomain($domain, FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
}

The code above first looks for a translation file in WP_LANG_DIR, to be found in wp-content/languages/ by default. Putting your custom translation files in there is safe for upgrades.

Also, by using the the approriate hook and functions to load the translations for your plugin, you allow users to hook into the language loading process for your plugin, giving them much flexibility to load language file(s) from anywhere they want.

I’ve written a more in-depth article about this on my blog.

Leave a Comment