There are many, many different ways to go about this, and I advise you to put some time into searching google and finding the way that best suits your needs.
I will give you an example I like to use, but keep in mind that the information I'm posting is only one out of a multiple possible approaches.
First off, you have to get the user's language. To do this you can either use geolocation and base the language off of where the user is (see this answer for a good way, or search google and use one of many other approaches), or you can get it from the user's browser (see this thread for information on doing that).
Once you have that, it's just a matter of displaying localized content to the user. My preferred approach is the following, which combines all variants into a single file (alternative ways of doing this utilize multiple different HTML files):
First off you have your HTML:
<body>
<h2>
<span class="lang lang_en">This is in english!</span>
<span class="lang lang_it">Questo è in italiano!</span>
</h2>
</body>
Each version of the text exists within its own span. Then in your CSS you hide all the span
s:
.lang{
display:none;
}
Finally, in the JS, we reveal only the span
s that contain text in the language we want to display:
var lang = ...; //language
switch (lang) {
case "en":
$(".lang_en").css("display", "inline");
break;
case "it":
$(".lang_it").css("display", "inline");
break;
default:
// Set your default language here
}
Of course, whether or not this approach is a good choice for you depends on your needs. I like it because it doesn't depend on any framework besides JQuery, and is simple to understand. Of course, depending on your needs, it may be even prohibitively inefficient (such is the case with very, very large webpages). That is up to you to decide.