-1

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.

Hamid Reza
  • 2,859
  • 9
  • 44
  • 71

1 Answers1

1

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.

MichaC
  • 12,433
  • 2
  • 39
  • 53
  • For 2-page website resource files may be sufficient but for any professional project, especially dynamic ones, this has to be stored in the database. – T.S. Sep 25 '13 at 18:20
  • resource files are one way to do it, I didn't say its the only way ;) And using a database has tons of other issues, you would have to code something to do translations etc... We are also using the database, but I'm just saying – MichaC Sep 25 '13 at 18:23
  • you don't code translations. You code for language selection and this is a primitive thing. Translations are done by language specialists who knows computer terminology of the country. Please, name me one issue using Database? As a matter of fact DB will make life easy, because you can load strings for as many languages as you want without compiling anything and changing any code. – T.S. Sep 25 '13 at 18:58
  • As I said, I know that storing resources in databases is a good thing, but still, it is harder to maintain. For example what do you do to provide fully type safe resource objects to be used in your application? Easier or harder to do as if you use resource files? ;) – MichaC Sep 25 '13 at 19:06
  • Ela, let me disagree :o)) – T.S. Sep 25 '13 at 19:41