I have a bi-lingual blog with English as the main language and Arabic. Currently I use the post “Custom Field” to indicate that the post is Arabic and I use that in my child theme to change the direction to RTL and the text translation to Arabic, however, I do that manually which means I fully customize the theme with strings in both languages and pick the correct one based on the custom field value and also set the direction for the html tags for the post title, body and comments. It is working, however, changing the theme later means I need to redo everything. I am also trying to use a comment plugin “wpDiscuz” but there is no easy way to control the language other than editing the plugin itself which I don’t want to do since I want to be able to update it easily and not redo the changes after every update.

Since WP already supports Arabic language and has the translations as .mo files with the correct rtl css, and also the new plugin supports .mo translations. I thought I will utilize that and find a away to change wordpress language to Arabic only for certain posts. However, I am still not bale to do it.

I created a plugin with single file functions.php and this is the content:

function my_reset_locale($locale) {
    $locale="ar";
    return $locale;
}
add_filter('locale','my_reset_locale',10);

This one changes it for everything (front page and all single posts), i tried reading the post ID

global $post;
$postId = isset($post) ? $post->ID : '';

But the first 4 calls to my function happens before $post->ID is set and if I wait until it is set it becomes too late as the language got loaded already and changing $locale after that does nothing.

So what can I do to get information about the post early and before the language is read (i think it is “load_textdomain” that is called to select the translation based on the locale”)

1 Answer
1

I couldn’t find an answer so I ended up providing the solution.

It wasn’t simple given that I am not an expert with wordpress nor with php but WordPress documentation is great so here is the solution:

// Set the post language when loading up the page based on the store meta
function ppl_set_post_language() {
    $postID = url_to_postid( $_SERVER["REQUEST_URI"] );
    if ($postID > 0) {
        $postLanguage = esc_attr( get_post_meta( $postID, '_ppl_post_language', true ) );
        if ( ! empty( $postLanguage ) ) {
            global $locale;
            $locale = $postLanguage;
        }
    }
}
// Any call to 'url_to_postid' earlier then 'setup_theme' will generate a fatal error.
add_action('setup_theme', 'ppl_set_post_language');

When editing the post or page, store the language code (e.g. en_US or ar) in the post meta. This part is easy, however, the problem is when displaying the page when do you run the code that check the langauage? If you run it too early you won’t have the post ID since it is not yet loaded by wordpress, and if you wait and run the code late it will be too late since wordpress will load the default language. The sweet spot I found is at action setup_theme since at that step the translations are not loaded yet and we can call url_to_postid to get the post ID which we use to retrieve the post meta.

After doing this I thought why not create a WordPress plugin that others can benefit from and here it is:

Per Post Language

Leave a Reply

Your email address will not be published. Required fields are marked *