Forum Moderators: phranque

Message Too Old, No Replies

translate a URL using mod_rewrite mapping and php

translating a URL in another language

         

thee

4:42 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



Hi,

I'm currently trying to use URL rewriting to translate the whole URL of a website depending on which language you choose. The user could switch languages and currencies anytime on every page.

A dynamic URL would be:

/index.php?category=1&lang=en&currency=usd

depending on these last arguments, this would be translated in english and US dollars:

/vegetables/index_usd.html

or in french and euros:

/legumes/index_eur.html

Now, it`s quiet easy to code some php to take care of the currency in a link (e.g. switch between "usd" and "eur" in the <a href=""> tag depending on the value of $lang).

I could write a map text file so a category is associated with the appropriate term

vegetables 1

The questions are:

-Is it possible to have two maps.txt (one in french one in english) associated with a language variable?

-How could I make a link that switches the language using php?

Perhaps this way of doing things is way too complicated, what do you think? :)

Thanks in advance for your answers,

Thee

webdoctor

12:46 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



-Is it possible to have two maps.txt (one in french one in english) associated with a language variable?

-How could I make a link that switches the language using php?

Perhaps this way of doing things is way too complicated, what do you think? :)

It does sound compliated.

Surely the biggest difference between your pages is the language? So, organise your content by language first, then by category, then by product.

How about organising your URL structure like this:

http://www.example.com/en/vegetables/beans.html
http://www.example.com/fr/legumes/haricots.html
http://www.example.com/de/gemuese/bohnen.html

You could then mod_rewrite these to

http://www.example.com/script.php?lang=en&cat=vegetables&prod=beans
http://www.example.com/script.php?lang=fr&cat=legumes&prod=haricots
http://www.example.com/script.php?lang=de&cat=gemuese&prod=bohnen

with a couple of simple .htaccess lines

You could even do this by locale (en-gb, en-us) rather than by language, and then the locale could trigger the prices to appear in the appropriate currency.

thee

3:15 pm on Mar 9, 2006 (gmt 0)

10+ Year Member



thanks for your reply
these are very interesting clues

Surely the biggest difference between your pages is the language? So, organise your content by language first, then by category, then by product.

Yes I think I'll probably organize the content this way by detecting the browser`s locales
/en_us/vegetables/beans.html
/en_en/vegetables/beans.html

Now should I give up the complete translation of the URL?

All this is forcing me to a major redesign of what I had chosen previously... oh well I guess that`s the way every piece of code is done :) *or maybe not

webdoctor

4:27 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now should I give up the complete translation of the URL?

No, why?

Are you storing your products in some kind of backend database?

How about having a list of products, give each of them a unique ID number. In this table you hold information that DOESN'T vary by locale.


ProductID,Product
01,<beans data>
02,<carrots data>
03,<potatoes data>

then you have a second table of locales, mapping to currencies and any other locale-related info


LocaleID,Currency
en_GB,GBP
en_US,USD
de_DE,EUR
fr_FR,EUR

then you can list your localised content in a third table in which you can map each product to its localised content.


ProductID,LocaleID,LocalProductName,LocalisedContent
01,en_GB,beans,Our beans are the best in the UK...
01,en_US,beans,Our beans are the best in the USA...
01,de_DE,bohnen,Unsere Bohnen...
01,fr_FR,haricots,Notre haricots...

Do you get my drift? Your master script can then pull the local product name parameter and locale from the rewritten URI, search for it in the third table, and pull the relevant currency and product parameters from the first two tables.

webdoctor

4:29 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you want me to send you a quote for this project? :-)

thee

1:18 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



hehe no thank you! your explanations are worth a lot more than that: you`re not helping a company you`re helping the ever growing knowledge of a junior webmaster my dear Obi-Wan :)

Ok I completely get your point.
I also have a category attribute Vegetable
so it looks like this:

ProductID,LocaleID,LocaleCategory,LocalProductName,LocalisedContent
01,en_GB,Vegetables,beans,Our beans are the best
01,fr_FR,Legumes,haricots,Notre haricots...

http://www.example.com/script.php?locale=en_us&cat=vegetables&prod=beans
Rewritten to: http://www.example.com/en_us/vegetables_beans.html

Basically it's the other way round and it's much more simpler. Again thank you!

webdoctor

1:41 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I also have a category attribute Vegetable
so it looks like this:

ProductID,LocaleID,LocaleCategory,LocalProductName,LocalisedContent
01,en_GB,Vegetables,beans,Our beans are the best
01,fr_FR,Legumes,haricots,Notre haricots...

OK - but you can normalise this a bit more :-)

A product's category doesn't depend on the locale, so instead of using the localised category names, use category IDs instead. Then add yet another table listing localised category names, cross referenced to category IDs.

ProductID,LocaleID,CategoryID,LocalProductName,LocalisedContent
01,en_GB,1,beans,Our beans are the best
01,fr_FR,1,haricots,Notre haricots...

and

CategoryID,LocaleID,LocalCategoryName
1,en_GB,Vegetables,
1,fr_FR,Legumes
1,de_DE,Gemüse

That way you avoid having to re-type "Legumes" for every product, you type "1" which is the category ID for "Legumes" in locale fr_FR (it's also the category ID for "Vegetables" in locale en_GB).

You're going to need some good INNER JOINs in your SQL to query across all these tables...