Switching language WordPress with language button in navigation

I have created a WordPress theme that needs to be in German and English. In the navigation I have a button which should change the language depending on what language they are currently using. (If they are viewing the english page it should display “Deutsch” and vice versa.)

I’m using qTranslate Plus and have chosen to display the language code in the url (/de or /en). My main problem lies when I’m on a custom page (WordPress page with a template) (/custom-page) and change the language it doesn’t change to german although in the source code the link displays /de/custom-page. It completely ignores the language code and takes me to /custom-page again. So it seems to be stripping the language code out.

Read More

Is this standard WordPress behaviour and if so, how can I disable it?
Also is there a better, more reliable way to change the language? Would storing the language in a session work?

Note: Both “Hide untranslated content” and “Hide URL language information for default language.” are unchecked.

Thanks in advance,
Peter

Related posts

Leave a Reply

3 comments

  1. I had a similar issue on a site I was working on. I installed this extension which seemed to resolve some issues with URLs

    https://wordpress.org/plugins/qtranslate-slug/

    alternatively, you could target the button with javascript/jQuery using conditional statements to detect the html lang attr. Something like this:

       var lang = document.documentElement.lang;
     if (lang == 'en-US'){
        $('button').html('your button text')
     }
    
  2. qTranslate is not supported well. If you just started implementing it I think you should give other plugins a shot. Here are few replacements for qTranslate:
    mqTranslate and zTranslate. Both are qTranslate forks so you shouldn’t loose anything.

    As for your question you can simply hide current language with css using lang selector:

    :lang(current languagecode) {
    display: none;
    /*the other way to hide things*/
    text-indent: 9999px;
    overflow: hidden;
    }

    Hope this helps.

    Best Regards.

  3. I noticed on 2 of my wordpress installations (3.8.1 updated to 4.0), that mqtranslate (same code) does not translate the content when using url-based routing, similar to your problem.

    i installed “qtranslate plus” just now on a fresh wp 4.0 and it worked as expected with url-based routing (But seems to mess up the date format in the post header).

    So i think the problem could be the version number not working well together with qtranslate.
    maybe the preg_match-code in ppqtranslate_core.php line 173 does not give the right result.

    workaround:
    i stopped digging at this point and switched to domain-based routing (en.site.com for english and site.com for german). this workes flawlessly. if you can do it, just try. you need to

    1. route the subdomain *.site.com to your server ip in your domain providers admin panel (or en.site.com for a single language, if your provider does not permit the wildcard *)
    2. set up a subdomain on your webserver (en.site.com) to route in the same directory as the original site. just and add the following code (edit domain name and path to webroot) to your httpd.conf (or apache2.conf on some servers)

    Add to httpd.conf:

    <VirtualHost *:80>
    ServerName "en.site.com"
    ServerAdmin webmaster@localhost
    DocumentRoot /the/path/to/your/html/
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /the/path/to/your/html/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    and restart webserver with

    /etc/init.d/httpd restart
    

    after this domain-based translation should work.