Site logo

[Resolved] How to translate fieldnames in the blog comment form

Home Forums Support [Resolved] How to translate fieldnames in the blog comment form

Home Forums Support How to translate fieldnames in the blog comment form

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2451807
    Vladimir

    Hi,

    Can you please send me instructions on where do I change (I want to translate) these 5 field names and 1 button for the blog comment form:
    https://share.getcloudapp.com/6quNL462

    I am using your premade theme READ.

    Thank you,

    Vlad

    #2452236
    Fernando
    Customer Support

    Hi Vladimir,

    You’ll need a couple of Filter hooks to alter those text.

    Let’s start with “Leave a Comment”.

    Add this PHP:

    add_filter( 'generate_leave_comment','tu_custom_leave_comment' );
    function tu_custom_leave_comment() {
        return 'Join the conversation';
    }

    Replace Join the conversation with your translated text.

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    Then for “Post Comment”. Add this:

    add_filter( 'generate_post_comment ','tu_custom_post_comment' );
    function tu_custom_post_comment() {
        return 'Post Comment customize';
    }

    Replace Post Comment customize with your translated text.

    Now, for the Post Comment Form fields, add this:

    add_filter( 'comment_form_default_fields',function($fields){ 
    	$commenter = wp_get_current_commenter();
    	$required = get_option( 'require_name_email' );
    
    	$fields['author'] = sprintf(
    		'<label for="author" class="screen-reader-text">%1$s</label><input placeholder="%1$s%3$s" id="author" name="author" type="text" value="%2$s" size="30" />',
    		esc_html__( 'Nombre', 'generatepress' ),
    		esc_attr( $commenter['comment_author'] ),
    		$required ? ' *' : ''
    	);
    
    	$fields['email'] = sprintf(
    		'<label for="email" class="screen-reader-text">%1$s</label><input placeholder="%1$s%3$s" id="email" name="email" type="email" value="%2$s" size="30" />',
    		esc_html__( 'Correo Eléctronico', 'generatepress' ),
    		esc_attr( $commenter['comment_author_email'] ),
    		$required ? ' *' : ''
    	);
    
    	$fields['url'] = sprintf(
    		'<label for="url" class="screen-reader-text">%1$s</label><input placeholder="%1$s" id="url" name="url" type="url" value="%2$s" size="30" />',
    		esc_html__( 'Website', 'generatepress' ),
    		esc_attr( $commenter['comment_author_url'] )
    	);
    
    $consent   = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
    
        $fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' . '<label for="wp-comment-cookies-consent">TEXT FOR CHECKBOX HERE</label></p>';
    
    	return $fields;
    }, 15, 1);

    Replace Nombre, Correo Eléctronico, and Website with your translated texts.

    Let us know how it goes.

    #2452345
    Vladimir

    This is great code and this worked well as you can see here. There is just one more field that I pointed out in the screenshot when I submitted the ticket that you didn’t do the code for and this is the “checkbox” on the bottom which says:

    Save my name, email, and website in this browser for the next time I comment.

    Can you please send me code for that.

    #2452355
    Fernando
    Customer Support

    Yes, I can give you the code for that.

    I updated the third code above, can you test that instead?:

    Replace TEXT FOR CHECKBOX HERE with your translated text as well.

    Let us know how it goes.

    #2452359
    Vladimir

    This works great. I just implemented it. On the form conformation page, I just see another text. It says: “Your comment is awaiting moderation.” Would you mind sending me code for that also and we are done?

    This is amazing how well you write this code…I wish I had this skill.

    #2452367
    Fernando
    Customer Support

    Yes, I can send code for that:

    add_filter( 'gettext', function( $text ) {
        if ( 'Your comment is awaiting moderation.' === $text ) {
            return 'Translated Text abc';
        }
    
        return $text;
    } );

    Replace Translated Text abc with your translated text.

    There’s no filter for that.

    Take note that you can submit a translation for your language in WordPress here: https://translate.wordpress.org/projects/wp-themes/generatepress/

    #2452377
    Vladimir

    Thank you! Closing ticket.

    #2452385
    Fernando
    Customer Support

    You’re welcome, Vladimir!

    #2455414
    Vladimir

    Fernando,

    Sorry to open this ticket again. I forgot to translate these 3 elements that are visible when user is logged into the website and goes to leave the comment: https://jmp.sh/eEDTrq4c

    Can you please send me code for the text I highlight above the comment form and for “Post Comment” button on the bottom.

    Thank you so much and sorry for reopening the ticket again.

    Here is the screenshot: https://jmp.sh/eEDTrq4c

    #2455444
    Fernando
    Customer Support

    I see. Can you try modifying the code here: https://generatepress.com/forums/topic/how-to-translate-fieldnames-in-the-blog-comment-form/#post-2452367

    To this:

    add_filter( 'gettext', function( $text ) {
        if ( 'Your comment is awaiting moderation.' === $text ) {
            return 'Translated Text abc';
        } else if ( 'Komentari' === $text ) {
            return 'Translated Text abc';
        } else if ( 'Logged in as' === $text ) {
            return 'Translated Text abc';
        } else if ( 'Edit your profile' === $text ) {
            return 'Translated Text abc';
        } else if ( 'Log out' === $text ) {
            return 'Translated Text abc';
        } else if ( 'Required fields are marked' === $text ) {
            return 'Translated Text abc';
        } else if ( 'Post Comment' === $text ) {
            return 'Translated Text abc';
        }
    
        return $text;
    } );

    Replace Translated Text abc for all translations.

    #2455451
    Vladimir

    I entered the text you gave me as:

    add_filter( ‘gettext’, function( $text ) {
    if ( ‘Your comment is awaiting moderation.’ === $text ) {
    return ‘Vas komentar ceka odobravanje’;
    } else if ( ‘Komentari’ === $text ) {
    return ‘Translated Text abc’;
    } else if ( ‘Logged in as’ === $text ) {
    return ‘Logirani ste kao’;
    } else if ( ‘Edit your profile’ === $text ) {
    return ‘Modifikujte svoj profil’;
    } else if ( ‘Log out’ === $text ) {
    return ‘Izlogirajte se’;
    } else if ( ‘Required fields are marked’ === $text ) {
    return ‘Nuzna polja su oznacena’;
    } else if ( ‘Post Comment’ === $text ) {
    return ‘Komentar’;
    }

    return $text;
    } );

    and the text you gave me successfully translated button on the form, but it did not work for this one line which is in English:
    https://jmp.sh/a4hAGqvz

    I do see you gave me code for “Logged in as” and “Edit your profile. Log out? Required fields are marked” but for some reason your code is not affecting that line and not translating it. See screenshot: https://jmp.sh/a4hAGqvz

    #2455531
    Fernando
    Customer Support

    I see. If it’s not translating, you may need to submit a translation here: https://translate.wordpress.org/projects/wp-themes/generatepress/

    Or, you may need to use a translation plugin.

    Those texts are from WordPress, and aren’t controlled by GeneratePress. I can’t see a specific filter to alter their text as well.

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