4

I'm using Codestyling Localization and I want to translate a plugin and I want the translation files to be saved in the translation directory in the theme I'm building and not in the plugins directory.

Is this possible?

I have tried load_plugin_textdomain and load_textdomain but that doesn't seem to work.

// Add localization
load_theme_textdomain( 'my_theme', $translation_directory );

$domain = 'other_plugin';
$locale = apply_filters('plugin_locale', get_locale(), $domain);
load_textdomain( $domain, WP_LANG_DIR.'/'.$domain.'/languages/'.$domain.'-'.$locale.'.mo');
load_plugin_textdomain( $domain, FALSE, $translation_directory );
3

First of all, I'd strongly recommend against storing translations for a plug-in in your theme. It has no reason to be there, and you'll loose the translations when you change/update the theme.

The correct place to store translations is wp-content/languages/plugins/.

This code does that, and targets the domain 'other_plugin':

function wpse159536_override_mofile_path( $mofile, $domain ){
    if( 'other_plugin' == $domain ){
         $mofile = WP_LANG_DIR . '/plugins/' . basename( $mofile );
    }
    return $mofile;
}
add_filter( 'load_textdomain_mofile', 'wpse159536_override_mofile_path', 10, 2 );

Of course, an even better solution would be to send the translation file to the developer to include in the next release (if this is appropriate).

2
  • Yes placing the files in wp-content is better. This code only changes the url for the file when it's loaded right? Do you know if it's possible to get this to work with Codestyling so things can be translated there? – Patrik Aug 29 '14 at 16:56
  • It changes where WordPress looks for the translation. I'm not sure how Codestyling Localisation works, but if you can download a copy of the .mo file, then you can put it in the appropriate directory. – Stephen Harris Aug 29 '14 at 18:40

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.