qtranslate + jQuery Missing Link

Danny Wahl's picture Danny Wahl  •   •  JavaScript

If you look at the qtranslate forums and various theme forums there’s a variety of people saying this link or that link doesn’t translate. The answer is usually to go edit the theme or plugin to call qtranslate, but there’s one that’s never been answered, and that is the built-in WordPress Menu.

Fortunately that’s something that we can solve with only a few lines of jQuery. Why jQuery? Well because that’s what the menu is built on, so you have to be using it already! Anyways, here’s the amazingly short code:

jQuery.each(['CSS SELECTORS, COMMA SEPARATED'], function(index, value) {
    jQuery(value).attr(‘href’, function(i, val) {
        return val + jQuery(‘html’).attr(‘lang’).split(‘-’)[0]

Ta-da. Simply what this code does is read the lang value from the HTML tag and append the first two characters to the href value of the selectors in the array. So en-US becomes en/ zh-CN becomes zh/ etc… I must say, that having never used jQuery before it was strikingly easy! Especially compared to other JS libraries like oh say YUI.

Share this post