Dynamic English/French links

J

Jim Royal

Living in Montreal, I am required to produce web content in both
official languages. So, my English sites contain links to French
content, and vice versa.

For database-driven sites, pulling up the content in the right language
is merely part of the query. But for static sites, all I've been able
to do is provide a link to the main page in the alternate language, but
that's a bit awkward for the site visitor.

Is there a way to pick up the URL for the current page, rewrite it on
the fly, and call up the equivalent page in the alternate language? For
example, to change this:

/en/services/general.shtml

into this:

/fr/services/general.shtml

Javascript? PHP?

Any suggestions are welcome.
 
P

Philip Ronan

Jim said:
Living in Montreal, I am required to produce web content in both
official languages. So, my English sites contain links to French
content, and vice versa.

For database-driven sites, pulling up the content in the right language
is merely part of the query. But for static sites, all I've been able
to do is provide a link to the main page in the alternate language, but
that's a bit awkward for the site visitor.

But is it really all that likely your visitors will want to look at BOTH
languages? Just one or the other, surely?

I think the best option would be to use a LINK element and let the UAs deal
with it or not as they see fit. Something like this should do:

<LINK title="French version" type="text/html" rel="alternate" hreflang="fr"
href="/fr/services/general.shtml">

(see <http://www.w3.org/TR/REC-html40/struct/links.html#h-12.3.3>.)

Alternatively, if you're using an Apache server you should consider using
its content negotiation features.
Is there a way to pick up the URL for the current page, rewrite it on
the fly, and call up the equivalent page in the alternate language? For
example, to change this:

/en/services/general.shtml

into this:

/fr/services/general.shtml

Javascript? PHP?

If you want to do it this way, add a form with a select menu
("French","English") and process it using client-side javascript (based on
the value of location.href) or server-side PHP (based on the value of
$_SERVER[¹HTTP_REFERER¹]).

Phil
 
H

hyweljenkins

Of course. Move your files to .php instead and use
$_SERVER["SCRIPT_NAME"] or $_SERVER["PHP_SELF"] to get the current
document name, and write the links to other languages accordingly.
You'll need to break it up using explode() and then replace the
language array element with the target language identifier.
 
J

Jan Faerber

Jim Royal wrote:

Javascript? PHP?

Possible solutions:

#) The user can set the language and it will be saved in a cookie,
so when he comes again the selected language can be remembered.

#) The user can not set the language - it will be automatically detected.
 
J

Jim Royal

Philip Ronan said:
But is it really all that likely your visitors will want to look at BOTH
languages? Just one or the other, surely?

Often true, but not always. However, my thought is for people entering
into the site via deep linking. It would be nice to be able to flip the
page to the alternate language without forcing people to try and guess
where in the site hierarchy the page is located.
I think the best option would be to use a LINK element and let the UAs deal
with it or not as they see fit. Something like this should do:

<LINK title="French version" type="text/html" rel="alternate" hreflang="fr"
href="/fr/services/general.shtml">

Interesting. However, this would require encoding the URL manually into
each page, which is just as much work as making manual HREFs.
Alternatively, if you're using an Apache server you should consider using
its content negotiation features.

I will look into this.
Is there a way to pick up the URL for the current page, rewrite it on
the fly, and call up the equivalent page in the alternate language?

If you want to do it this way, add a form with a select menu
("French","English") and process it using client-side javascript (based on
the value of location.href) or server-side PHP (based on the value of
$_SERVER[¹HTTP_REFERER¹]).

This is probably what I'll do in the short term. Thanks muchly for the
input.
 
J

Jim Royal

Of course. Move your files to .php instead and use
$_SERVER["SCRIPT_NAME"] or $_SERVER["PHP_SELF"] to get the current
document name, and write the links to other languages accordingly.
You'll need to break it up using explode() and then replace the
language array element with the target language identifier.

I'll keep this in mind for the future. Thanks.
 
J

Jim Royal

Jan Faerber said:
#) The user can set the language and it will be saved in a cookie,
so when he comes again the selected language can be remembered.

#) The user can not set the language - it will be automatically detected.

I don't think either is a good idea, frankly. This is not like a
selection for country in an e-commerce site where alternate country
selections would be of no use. Some people do indeed want to be able to
see both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top