Question

I want to know is it better to use from google translate API or to have a url like this:

/example.com/en/page/show

and generate the views with the language mentioned in the url with lots of if's? Or what eles?

Thanks.

Was it helpful?

Solution

If you want to support multiple languages, one way to do this is using resources files and instead of hard-coding any string/resource on your page, use the resource representation.

This way you can easily add new languages, translate stuff etc...

Regarding the URL it depends, setting it via URL is one way, you could also use a cookie..

You can set the CurrentUICulture on the current thread, all resource retrieval methods and/or string operations actually support that you specify the CultureInfo you want to use. And it should take the culture which is set within the current thread automatically.

for example you could put something like this into your global.asax

void context_BeginRequest(object sender, EventArgs e)
{
    // eat the cookie (if any) and set the culture
    if (HttpContext.Current.Request.Cookies["lang"] != null)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["lang"];
        string lang = cookie.Value;
        var culture = new System.Globalization.CultureInfo(lang);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;
    }
}

Or, if you are using MVC, you routing config could contain the language, you could use the URL route to set the language instead of the cookie...

So this is just an example, I'd suggest you to find some more examples on google and figure out the best approach for your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top