Multilanguage URLs

I am trying to achieve multilanguage behaviour for my website. I’m very close but not quite there and feel like I’m running in circles. That’s why I decide to ask for your help.

I’d like to use URLs like mysite.com/fr/some_page to take me to what would normally be mysite.com/some_page which would then be displayed in French.

I’ve actually gotten this to work by enabling pretty permalinks and using a rewrite rule:

add_rewrite_rule('^([a-zA-Z][a-zA-Z])/(.+)', 'index.php?pagename=$matches[2]&lang=$matches[1]', 'top');

and adding the lang variable to query_vars:

function add_query_vars( $vars )
{
    $vars[] = 'lang';
    return $vars;
}
add_filter( 'query_vars', 'add_query_vars' );

But this doesn’t work on the home page. In fact, the entire $wp_query->query_vars is made of empty values. Not even passing the query parameter in the URL works: mysite.com/?lang=fr (as I read somewhere this is because it tries to match a post when it sees a ?)

I’ve even tried to add a new rewrite rule, specifically for the home page:

add_rewrite_rule('^([a-zA-Z][a-zA-Z])/?$', 'index.php?pagename=home_slug&lang=$matches[1]', 'top');

To no avail.

Does anyone know how I can do this? If not, my only option is to painstakingly try to follow through the code to understand how wordpress handles the urls, but I’m afraid it won’t be an easy task.

Forgot to mention that I’m specifically interested in pages not posts. My site is made of pages.

0

Leave a Comment