How to use esc_attr__() function properly to translate a variable that contains string?

If you have static text with dynamic content then you can use.


printf( esc_attr___('static text goes here with %s', 'text-domain' ), $title );

If you have only $title then no need to translate it.

Just escape it.


echo esc_attr( $title );


Note esc_attr, esc_attr__ and esc_attr_e used for escaping dynamic values from HTML element attributes.

E.g.


`<div class="<?php echo esc_attr( $class ); ?>">`

And esc_html, esc_html__ and esc_html_e used for escaping dynamic values from HTML content.

E.g.

<div> <?php echo esc_html( $title ); ?> </div>