4

Learning asp.net mvc and whenever I worked we had to localize our application. Looked at few articles eg

  1. http://afana.me/post/aspnet-mvc-internationalization.aspx (Creates a view for each language) No for me
  2. http://www.codeproject.com/KB/aspnet/BilingualMvc3Part1.aspx (uses Broswer default language which will never work for me. Can you imagine I am English speaker on holiday in Spain and need to use the website and i have not got a clue about how to change the settings) NO

  3. http://geekswithblogs.net/shaunxu/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx ( this one via URL but looking at his baseController sort of hardcode the application name in the cookie)

I am CONFUSED. Mainly because my knowledge is not great!! What is the recommended way to do it? Have you done in real world app? Would you share code? A snippet of how you tackled the problem will do?

I am looking for a neat way like you can do in asp.net .I need to give the ability to user to change languages at run time by clicking a flag or something.

Is there a base method to override? Is there a proper example somewhere? Do you have to write some sort of baseController class?

thanks a lot for your time

H H
  • 263,252
  • 30
  • 330
  • 514
user9969
  • 15,632
  • 39
  • 107
  • 175

2 Answers2

2

You should use resource files to handle localisation. With that you have the ability to get a language the user has choosen before or he has choosen right now. Here is a good blog how to do that http://adamyan.blogspot.com/2010/02/aspnet-mvc-2-localization-complete.html

IF the user has no active session, means no cookie that identifies him you should use the browser language or a maybe a geotracking service to determine the sitelanguage. It is up to you to present a easy way to change the current language... And as mentioned above resourcefiles will help you here.

sra
  • 23,820
  • 7
  • 55
  • 89
  • Hi,thanks for your reply. I have looked at the link .it looks like a copy of another article,not sure who the original author is.I definetely want to use resource file ,but was looking for a solid way of doing it. – user9969 Jun 02 '11 at 10:52
  • Why do you think this way isn't solid? I don't use views with localize content, but I thought the trick is the same; refer to resource strings and overwrite the the language. I am not sure if the blog overwrites it but I would do this. The rest is done by the resource. – sra Jun 02 '11 at 11:44
  • thanks for your comment. I will have a look again.I am a bit confused which method should the threading/switching take place,some other articles I have seen they use a baseController and write in the Executing and Core methods.In asp.net it was a clean cut in the InitCulture Method.In asp.net mvc is not that clear to me . – user9969 Jun 02 '11 at 12:30
  • To identify the user and his given or choosen language you will need sessions. Use this to store the language and pass this to the resource. why make it complicated and use a base controller? For sure you should implement it in a way that just require one lookup to the session per request. – sra Jun 02 '11 at 12:41
0

If you are using Asp.Net MVC

//A foreigner, has possibly brew a cookie for me
public class SpeakNativeTongueAttribute : ActionFilterAttribute, IActionFilter
{
    const string cookieName = "culture";

     void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
        var cookieKeys = filterContext.RequestContext.HttpContext.Request.Cookies.AllKeys;

        if (cookieKeys.Contains(cookieName))
        {
            //eat the cookie
            var theCultureCookie = filterContext.RequestContext.HttpContext.Request.Cookies[cookieName];
            var theCulture = theCultureCookie.Value;

            //say thanks in native tongue
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
        }
        else
        {
            //Didn't receive a cookie, don't speak their language
        }
    }
}

Set the cookie with Javascript if they click a certain language and do a reload on the page.

Kevin Hogg
  • 1,771
  • 25
  • 34
NicoJuicy
  • 3,435
  • 4
  • 40
  • 66