Hi I was translating one of my plugins in "CodeStyling Localization". when I update my plugin my translation erased. Is there anyway to find it ? And How can I backup my translation (I don't want to waste my time next time :))

  • If you don't have a backup of your translation, yes, unfortunately you lost it. And if you don't have any, use this plugin (BackWpUp) or similar together with DropBox. – brasofilo May 17 '12 at 22:11

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.

  • Thanks a lot dear Geert . I translate other plugins with poedit and I upload them manually and I have no problems yet . Is it for some of the plugins ? – mahdiar May 17 '12 at 9:21
  • All I can say is that if you put your own translation files for a plugin in that plugin's directory, you lose them when upgrading the plugin. – Geert May 17 '12 at 11:30
  • Wonderful code, Geert, much appreciated! ps: the article too – brasofilo May 17 '12 at 22:17
  • You suggest here and also in your blog to use effectively wp-content/languages/[my-plugin] as the "update safe" directory. However if say you use the Loco plugin and WooCommerce itself puts the translations into wp-content/languages/plugins has this changed? – icc97 Feb 8 '16 at 9:28
  • This has indeed changed in later wordpress versions. Now you use wp-content/languages/plugins. See also this screenshot – ejazz Mar 22 '16 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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