How to keep plugin translations after updates?

bbPress‘ language folder (wp-content/plugins/bbpress/bbp-languages) has that warning:

/**
 * Do not put custom translations here. They will be deleted on bbPress updates.
 *
 * Keep custom bbPress translations in /wp-content/languages/
 */

Actually, this is not a new problem and yes, they are right. This is a big problem if you are using WordPress plugins with a non-English language.

Read More

Basically, I translated bbPress and created .po and .mo files. Files are working if I put them in their normal wp-content/plugins/bbpress/bbp-languages folder. But as the above warning says, they will delete on update. But the problem is translations are not working if I put them in the wp-content/languages/ folder as suggested in bbPress.

I think there must be a hook or something I can activate it but what is the best solution for it? Simply I want to keep plugin language files in wp-content/languages/.

Related posts

Leave a Reply

4 comments

  1. You have to replace the call to BBpress’ language file.

    A good place to do this is a language specific file in your general languages directory. For Turkish it would probably be a file named tr_TR.php. This will be loaded automatically and only if it matches the language of your blog. It will not be overwritten.

    BBPress doesn’t use the function load_plugin_textdomain, it uses load_textdomain instead. Here you can find a filter:

    $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
    

    So in your language php file just add a filter to change the path:

    function load_bbpress_tr_mofile( $mofile, $domain )
    {
        if ( 'bbpress' == $domain )
        {
            // replace this. :)
            return 'FULL_PATH_TO_YOUR_FILE';
        }
        return $mofile;
    }
    add_filter( 'load_textdomain_mofile', 'load_bbpress_tr_mofile', 10, 2 );
    
  2. What you have to do is to create a simple plugin that will manage the text_domain of the installed plugins.
    If you don’t want to have a global solution you can add the code to your functions.php in your theme.

    The idea is to instruct wordpress on where to find translations.
    Internally in all plugins this is done using something similar to

    load_plugin_textdomain( 'regenerate-thumbnails', false, '/regenerate-thumbnails/localization' );
    

    this is the function that you will use. As you can see here, the last argument is used to set the relative to the plugin path, where the translation files reside.

    You can do what you want by putting one line for each plugin you want to alter their languages folder as follows.

    load_plugin_textdomain('bbpress', false, '../../languages/bbpress');
    

    this will instruct wordpress to load your custom translation files from a folder next to plugins folder that has a folder named bbpress that has the translation files using the EXACT same naming as each plugin uses.

    Your method, that instructs the textdomains for all plugins, should run in the init phase as follows

    function set_myplugins_languages() {
         .... your code here.....
    }
    add_action('init', 'set_myplugins_languages');
    

    (Don’t forget to mark this as answer if you found it useful)

  3. In my case creating permanent custom translation for the plugin requires following steps (examples for ACF plugin translation):

    1. create wp-content/languages/plugins/ directory
    2. copy selected translation file .po from the plugin lang/ folder (or any other with translations) to created wp-content/languages/plugins/ (e.g from lang/acf-pl_PL.po to wp-content/languages/plugins/acf-pl_PL.po)
    3. customize the translation with e.g. Poedit or any other program for editing .po files and then generate .mo and save in wp-content/languages/plugins/ (e.g. wp-content/languages/plugins/acf-pl_PL.mo)

    This way the translation should be preserved even after plugin update.

    I’m not sure if this strategy will work for all the plugins but this it’s definitely worth trying.

  4. In order for this to work you need to have the WordPress translation files in the WP_LANG_DIR already, and have the global locale set to that language. If you’re only including translations for bbPress without changing the locale or having WordPress’s core translation files present, nothing will translate despite you loading them.

    If you wish to override the locale for bbPress alone, you need to add a filter to ‘bbpress_locale’ which allows you to override the bbPress mo file.

    While it’s true that bbPress 2.0 uses the load_textdomain() function, load_plugin_textdomain() is just a wrapper for load_textdomain() anyways. The bbPress 2.0 method is more robust and allows for global translation file placement, so you don’t lose them during plugin update.