Reputation: 2467
So I'm trying provide my site in multiple languages.
I'd like to have an option to change the language on the page without changing the url. (Just like YouTube)
My question: How can I provide my website in multiple languages while keeping the url the same? And also have each language indexed by search engines
Here's my current idea:
My current idea is to have each page look for a language variable (will be set in either a cookie or user setting)
Then from that variable, load a file that has an array with the text of the corresponding language.
Now I just pull values from the array and put them inside of elements.
For search engines, I can set a get the language option as a get variable.
But, this method may not be the best
Upvotes: 0
Views: 159
Reputation: 4594
There are several things you could do, among the options are
The disadvantages of each method respectively
This makes adding an actual subdomain more difficult as you'll have the language set as a subdomain while technically it's the same content with a different language.
This one makes parsing a URL more complex if you do this yourself. You'd have to account for the language being set or not being set (maybe then set a default language). If you don't set a default language then you'll also have to make sure that all your links contain the language in their path if this doesn't already happen automatically.
The downside with this is that you're saving data client side, e.g. the user can clear cookies and the preference or language setting will be gone.
Out of the above options the third one by far, is the most simple solution to implement. No messing with subdomains or the URL at all, just reading a value from a cookie and loading a locale if the language is supported, otherwise loading the default locale.
This is by far your best option that requires the least amount of effort when it comes to rebuilding parts of your application. (Assuming you don't have a framework you're using)
There are no real security downsides to using a cookie for this either if you save the language data in a cookie without other information. This is because you're not saving sensitive data so anyone who would steal that cookie wouldn't know anything except the language they prefer to see your site in - which is quite irrelevant in 99.9999% of the situations.
This is ofcourse a slightly biased answer since this question relies on experiences / opinions from other people but the facts have been mentioned above (very short, incomplete but it gets the point across).
Upvotes: 1
Reputation: 75
You store the language in a cookie. Xou are right there. Then you load a file based on the language. This file is something like XML or YAML.
XML (english):
<string id="title">My Page</string>
XML (german):
<string id="title">Meine Seite</string>
XML (klingon):
<string id="title">DutlhHo'</string> <!-- not actually klingon -->
You now just parse this.
Upvotes: 3