[Resolved] translation of single strings, accessing .po files

Home Forums Support translation of single strings, accessing .po files

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1479093
    Malte

    Hi there,

    I wanna translate single strings without the use of a plugin.
    I figured I need to access the .po file, edit it with poedit and upload it.

    I found online several instructions on how to do this. Most of them include five steps:
    1) access the default .po file (through wp-content/languages or wp-content/themes/theme/lang
    2) translate the strings in poedit
    3) save the file as pt_de.po (for german)
    4) upload the file (pt_de.po along with pt_de.mo)
    5) change DEFINE(“WPLANG”,””) in wp-config.php to DEFINE(“WPLANG”,”pt_de”)

    That seemed pretty straightforward. However, I was not able to locate the .po file, not even the language folder. Also, I read that since WP 4.0 the DEFINE(“WPLANG”,””) does not exist anymore.

    So my question is, how to I access the .po file, translate it, where do I upload it and do I still need to change the wp-config file?

    Cheers,
    malte

    #1479143
    Elvin
    Staff
    Customer Support

    Hi,

    Strange. The theme itself uses whatever is set/used by WordPress so it doesn’t have its own /languages folder inside the theme folder.

    That said, you should be able to find them on /wp-content/languages if you don’t find the .po/.mo files for a certain language, its most likely due to them not being added yet.

    If I may suggest, if its a simple translation of a certain text, perhaps a php snippet is enough.

    Example:

    add_filter( 'gettext', function( $text ) {
        if ( 'Text to be translated here' === $text ) {
            return 'Translated Text';
        }
    
        return $text;
    } );

    Read more here
    https://make.wordpress.org/polyglots/handbook/plugin-theme-authors-guide/gettext/
    https://developer.wordpress.org/reference/hooks/gettext/

    A wise man once said:
    "Have you cleared your cache?"

    #1480569
    Malte

    Hi there,

    hmm, I don’t have any /languages folder. Super strange. Should this folder always be there?

    Your snippet works like a charm. I’ll use it, thanks!

    If possible, could you also explain to me how to translate strings in which information such as (1) a date, (2) a number or/and (3) the author is included? Again without the use of a plugin.

    1) The date in the comment section of a blog post.
    Right now it’s like this: 26/09/2020 at 12:16
    I want to change it to somethling like this: 26/09/2020 um 12:16 Uhr

    2) The “x comments” on the blog page if someone has already commented.
    Right now its like this: x comments (It says “leave a comment” if nobody has commented yet. This, however, can easily be changed by the snippet you provided)
    I want to change the “x comments” to something like this: Kommentare (x)

    3) “Reply to the_author” in the comment section when clicking on “reply”
    Right now its like “Reply to the_author” and I wanna change the “to”

    thanks for your time and help!
    cheers

    #1481072
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    No, GeneratePress doesn’t have a /languages folder.

    The best thing you can do is translate your language here: https://translate.wordpress.org/projects/wp-themes/generatepress/

    Otherwise, you can do this:

    1.

    add_filter( 'gettext_with_context_generatepress', function( $text ) {
        if ( '%1$s at %2$s' === $text ) {
            return '%1$s um %2$s';
        }
    
        return $text;
    } );

    2. You can use the same kind of filter to filter those: https://github.com/tomusborne/generatepress/blob/3.0.1/inc/structure/post-meta.php#L278

    3. This is a core WP string, you can use the same filter. This is the string: __( 'Reply to %s' )

    Hope this helps!

    #1481827
    Malte

    Hi there,

    I don’t have this folder either: /wp-content/languages
    which surprises me since I thought there has to be a default .po file.

    I tried your translation suggestion first. In the end, however, the entire website gets translated (including the interface for GP customization and the WP interface). However, I would like to have everything that is only visible to me in English (makes torubleshooting much easier) and everything that is visible to others in German.

    Thanks for the code, it worked!

    For those who are interested, here are the filters with which it should be possible to translate (I think all) the strings related to the comments:

    /* Translate plain strings ('Post Comment' is used as an example) */
    add_filter( 'gettext', function( $text ) {
        if ( 'Post Comment' === $text ) {
            return 'XX';
        }
    
        return $text;
    } );
    
    /* Translate "at" in dates */
    add_filter( 'gettext_with_context_generatepress', function( $text ) {
        if ( '%1$s at %2$s' === $text ) {
            return '%1$s XX %2$s';
        }
    
        return $text;
    } );
    
    /* Translate "1 Comment" */
    add_filter( 'gettext', function( $text ) {
        if ( '1 Comment' === $text ) {
            return  '1 XX' ;
        }
    
        return $text;
    } );
    
    /* Translate "X Comments" */
    add_filter( 'gettext', function( $text ) {
        if ( '% Comments' === $text ) {
            return  '% XX' ;
        }
    
        return $text;
    } );
    
    /* I guess the filter for "1 Comment" and "X Comments" could be combined */
    
    /* Translate "Reply to" */
    add_filter( 'gettext', function( $text ) {
        if ( _( 'Reply to %s' ) === $text ) {
            return _( 'XX %s' );
        }
    
        return $text;
    } );
    
    /* Translate "X thoughts on topic" 
    see here: https://generatepress.com/forums/topic/how-to-change-x-thoughts-on-post-title-in-comment-section/ */
    
    /* Translate "Save my name, email, and website.." 
    see here: https://generatepress.com/forums/topic/edit-comment-cookies-text-save-my-name-email-and-website/ 
    and here: https://generatepress.com/forums/topic/comment-form-required-fields-to-anonymous-posting/ */

    The only missing thing is how to change the text for: class=”wp-die-message”:
    Error: Please enter a valid email address.
    Error: Please fill the required fields (name, email)
    and the “<< back”

    I’d appreciate a hint, but if it is too complicated, it is not so important.

    Thanks for the help! This forum is amazing 🙂

    #1482513
    Tom
    Lead Developer
    Lead Developer

    You should be able to find those strings in WordPress core: https://github.com/WordPress/WordPress/blob/4419f9b889685867ab2427d06b1247588af4e29e/wp-includes/comment.php#L3508

    If you have no languages, try going to “Dashboard > Updates” and look for the “Update translations” button at the bottom.

    #1485453
    Malte

    Hi there,

    thanks for the reply!
    For anyone interested, here’s the code:

    add_filter( 'gettext', function( $text ) {
        if ( '<strong>Error</strong>: Please enter a valid email address.' === $text ) {
            return 'Put here whatever  you like';
        }
    
        return $text;
    } );
    
    add_filter( 'gettext', function( $text ) {
        if ( '<strong>Error</strong>: Please fill the required fields (name, email).' === $text ) {
            return 'Put here whatever  you like';
        }
    
        return $text;
    } );
    
    /* Changing the 404 Page see here: https://generatepress.com/forums/topic/change-the-text-404-error/ */
    

    The filters work great and I’ll use them. Thank you!

    I anyways tried again your initial suggestion to use the GP.po file for translation. However, I’m still having the problem of the non exisiting /languages folder (there is also no update translation button). Though, I may have to say that I installed WP in English and never installed any other language package. I guess the /languages folder is only created when another languages than English is installed (at least I saw the /languages folder, when downloading the German WP file for testing purposes).

    That said, I created the /languages and /themes folders myself and followed the instructions given here:
    Though, nothing changed. I then tried to download the .mo file and add it as well. Again, nothing changed. Lastly I tried to create the /plugins folder and added the gp-premium-de_DE. Again, nothing changed.

    My last guess would be that I have to install the German WP translation (Settings -> General -> Site Language -> select and install German) before the GP translation works. Is this really required? Would be so much easier to just access the default .po file (as described in my initial post) and change the 1 % of strings I want to change. What am I missing here? :/

    #1485715
    Tom
    Lead Developer
    Lead Developer

    I think that may be required – as the language files would only be necessary if the site was using that language (or ever had). Have you given it a shot yet?

    #1494061
    Malte

    Hi there,

    yes. So here’s the deal.

    The /languages folder is missing if WordPress is installed in English.
    Only when a language other than English is installed (Settings -> General -> Site Language -> select and install the language), the /languages folder gets created. When pressing “Update translations” (Dashboard > Updates), all of the .po and .mo files for plugins and themes are downloaded automatically and put in the respective /plugins and /themes folder within the /languages folder.

    So I guess the .po files can now easily be downloaded, edited with Poedit and uploaded along with the newly created .mo file to replace the unwanted files.

    Thanks for all the support. It not only helped me to solve the problem, but also gave me valuable insights into the handling of translations in WordPress.

    By the way: I am currently testing GenerateBlocks and it is amazing! I love it and will use it instead of elementor. One feature I think would be usefull is to have reusable blocks with unspecified content in them. For instance: Creating a colored container (infobox) with a paragraph in it. While this can be saved as a reusable block and added to another page, the content (the text of the paragraph) cannot be changed without converting the reusable block to a regular block (at least I have not found that option). However this means that this box will no longer be affected by any changes to the layout of the reusable block. Thus, it would be great to just tell the reusable block that there is a paragraph in it, but the text can be anything. This way the layout (e.g. background color of the box) can be changed for the reusable block and all other blocks originating from this block are also changed (even if there is always a different text in them).
    Kinda complicated to explain. I hope you get the idea.

    Keep up the good work,
    cheers!

    #1494688
    Tom
    Lead Developer
    Lead Developer

    Glad you got it sorted!

    Awesome! Happy you’re enjoying it. We’re working on a feature just like that for our pro version 🙂

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.