Search code examples
phpwordpressqtranslate

How to set a custom title per language?


I am running Qtranslate for my website see: http://www.businessinstitute.nl/

I am very impressed by this plugin. Now my only problem is that I cannot set a custom title and description for my homepage per language. I can only setup one line for the title in the general settings for my website.

I have installed Qtranslate META where I can set a custom title and description per page, but my only problem is the homepage.


Solution

  • As you tagged PHP in your question, here is a pure PHP way to address the "problem":

    1. You can make a static declaration in an array of all the different translations for your website "title" as it's assumed this wont change that often, if ever...
    2. Check to see if the user's browser is sending an accept_language
    3. Present the specific title in that user's language if it exists in your array
    4. If it doesn't exist, present a default language...

    Here is a quick implementation that is not dependent on 3rd party software / plugin:

     $website_titles = array ('en' => 'my cool website', 
                              'fr' => 'mon site cool',
                              'zn' => '我的酷网站');
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && isset($website_titles[$_SERVER['HTTP_ACCEPT_LANGUAGE']])) {
         echo '<title>' . $website_titles[$_SERVER['HTTP_ACCEPT_LANGUAGE']] . '</title>';
     } else { 
         // print default title
         echo '<title>my cool website</title>';
     }