5

Is it possible to override single plugin translation in function.php file and how that can be done?

3
  • Would you add the example code containing the string that you're trying to translate? This does sound doable. Mar 3, 2018 at 20:42
  • Haven't any code, just asking if it is possible. Mar 3, 2018 at 21:22
  • Okay, I've posted an example of a typical scenario. Mar 3, 2018 at 21:25

3 Answers 3

8

Here's an example where a string from a certain text domain is translated using the gettext filter:

/**
 * Translate a certain string from a particular text domain.
 *
 * @param string $translation  Translated text.
 * @param string $text         Text to translate.
 * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
 *
 * @return string
 */
add_filter( 'gettext', 'wpse_translate_string', 10, 3 );
function wpse_translate_string( $translation, $text, $domain ) {
    if ( 'plugin_text_domain' === $domain ) {
        if ( 'Original string...' === $text ) {
            $translation = 'New string!';
        }
    }

    return $translation;
}
4
  • I think this is the most elegant version, because when editing the po files and compile to mo, after an update the custom added or changed translations are gone. Sadly this will hook in every gettext() call. Feb 26, 2020 at 9:52
  • I experienced heavy performance problems using this hook in combination with WPML. Any other approaches that are update save that are performing better? Mar 31, 2020 at 13:45
  • 1
    @NickWeisser I'm not familiar with using WPML so I can't speak to that. Maybe try reaching out using their support channels for best practices with their plugin. Generally, it's important to bail early or use a simple check like in the example above before doing any processing. If the plugin or theme isn't providing a filter or option to change the string in question, then options are limited. I suppose you could do some kind of hacky JS solution, but I wouldn't go that route. Mar 31, 2020 at 14:11
  • 1
    @DaveRomsey thanks for your feedback, appreciate it. Apr 1, 2020 at 9:43
0

You can even override single messages if you go directly to your source code.

function foo(){
    // old message is commented, so you can go back anytime.
    //echo wp_send_json(array('status' => 'error', 'error_message' => esc_html__('Error Message', 'osetin')));
    // message is overriden 
    echo wp_send_json(array('status' => 'error', 'error_message' => esc_html__('Overriden message with any text you want', 'osetin')));
}
1
  • This is no good idea when the source is a plugin or not self-maintained theme. After an update your custom changes are gone and you have to apply them by yourself, again. Feb 26, 2020 at 9:53
0

The best approach is overriding translation file .po

add_filter( 'load_textdomain_mofile', 'load_custom_plugin_translation_file', 10, 2 );
function load_custom_plugin_translation_file( $mofile, $domain ) {
    if ('woocommerce' === $domain) {
        $mofile = get_stylesheet_directory() . '/languages/woocommerce-' . get_locale() . '.mo';
    }
    return $mofile;
}

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.