Search code examples
wordpresslanguage-translationpoedit

WordPresas - Add string translations to an existing .po


I'm using a theme in which the Italian translation is partial. With an editor I added the new lines and generated the .mo with Poedit. My doubt is about when I will download an update and when my translations will probably be overwritten.

Is there a way to avoid having to resynchronize new translations (the code uses domains in _e() functions)?


Solution

  • You can place your "custom" .po and .mo files into /wp-content/languages/plugins and it will override the plugin's original .po .mo files. So, even though you update the plugin, your custom translation files will be used.

    For example, if you translate Akismet plugin to Italian, your files in /wp-content/languages/plugins will be

    akismet-it_IT.mo
    akismet-it_IT.po
    

    EDIT:

    Alternatively, you can add this code to your child theme's functions.php in order to do string translation. You can append this code as many as you want to translate the words. I added two lines (hello2 and hello3) at the end as an example.

    // Translate Text
    add_filter('gettext', 'translate_text'); 
    add_filter('ngettext', 'translate_text');
    
    function translate_text($translated) { 
    $translated = str_ireplace('Hello', 'Ciao', $translated); 
    $translated = str_ireplace('Hello2', 'Ciao2', $translated); 
    $translated = str_ireplace('Hello3', 'Ciao3', $translated);  
    
    return $translated; 
    }