0

I am using several language files(.po, .mo) for multi-lingual translation purposes. The sources root for these files is specified as the root of my theme directory, and the translations work fine for .php files located inside this directory tree. But how do I do the same for content included in the WordPress pages?

I got around the problem of including PHP code inside the page by using the Insert PHP plugin. So for example I can do this,

[insert_php]
    _e('Hello', 'my_domain');
[/insert_php] 

Since the content of WordPress pages are saved in the database under the wp_posts table, I cannot add that as a source for the .po file. So how can I implement translation for the content directly included in WordPress pages?

0

Rather than use a plugin that uses the eval() function, you could write a simple shortcode to handle translations. something like this:

The issue you would face is that the strings wouldn't be picked up automatically by a tool like POEdit, you would need to manually add the strings to the translation PO files.

/**
 * [__]My string to translate[/__]
 * 
 * @param $atts
 * @param $content
 * @return string|void
 */
function my_translate_shortcode( $atts ,$content ){

    $atts = shortcode_atts( [
        'textdomain' => 'mytextdomain'
    ], $atts );


    return __( $content, $atts['textdomain'] );

}
add_shortcode( '__',  'my_translate_shortcode' );

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.