Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mariusvetrici/1a15713f34a2e2d92f86 to your computer and use it in GitHub Desktop.
Save mariusvetrici/1a15713f34a2e2d92f86 to your computer and use it in GitHub Desktop.
Translate Plugin From Child Theme - ever needed to translate some strings from a plugin so as to be able to further upgrade the main plugin without loosing your translations?
<?php
add_action( 'after_setup_theme', 'load_custom_translations');
function load_custom_translations(){
load_child_theme_textdomain( 'woocommerce-bookings', get_stylesheet_directory() . '/translations' );
load_child_theme_textdomain( 'woocommerce-social-login', get_stylesheet_directory() . '/translations' );
}
add_filter( 'load_textdomain_mofile', 'filter_custom_translation_paths', 10, 2);
/**
* Update the path for loading custom translations for plugins
* @param $mofile Original mo file;
* @param $domain The plugin domain
*
* @return mixed The new filtered path
*/
function filter_custom_translation_paths($mofile, $domain) {
// If the mofile to be translated is not our default child theme file
if (false === strpos($mofile, '/wp-content/themes/my_theme/translations/ro_RO.mo')) {
return $mofile;
}
else {
return trailingslashit(dirname( $mofile )) . $domain . '-' . basename( $mofile );
}
}
@rslonik
Copy link

rslonik commented Nov 4, 2016

This helped me. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment