4

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")

  • Just don't do it ;). unless you have some very good reason you should keep difirent languages in different wordpress installs (actually different or on a network). You might not care if it is a personal blog, but everything which aggregates posts like search might give an "interesting" results. – Mark Kaplun Apr 3 '16 at 2:56
  • @MarkKaplun It is my personal blog, which I run also on a raspberry pi behind my home internet connection (and cloudflare free account) ;). The issue is I write blogs in the language based on what I see relevant. And most readers on one language doesn't understand the other. But what do you mean by aggregating posts? and why would language change affect that? – Fahad Alduraibi Apr 4 '16 at 14:40
  • arabic is rtl, english is ltr. IfI search for "wordpress" which has results both in english and arabic, what will I get, ltr or rtl? Which widgets will I see? etc... – Mark Kaplun Apr 4 '16 at 15:48
  • depends on what you searched for, if your search was in english then the search engine will only show you the posts that are in english and visa versa. Since the titles of my arabic posts are also in arabic. This is my blog fadvisor.net/blog check it out to see what I mean – Fahad Alduraibi Apr 4 '16 at 15:54
  • Wordpress is more than a decade old, I find it hard to believe that there still is no simple plugin to set the locale per post. – Rolf Jun 14 '18 at 14:30
2

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:

https://wordpress.org/plugins/per-post-language/

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.