locked
How can I change language of my MVC 3 Application?? RRS feed

  • Question

  • User-1320061939 posted

    I have using cookies to save language configuration. The big problem is that the language work perfectly almost in all page, there is a place in the webpage that language keep changed back to en-US (this is the default language). Please help!!!!!!!! Thanks.

    Saturday, September 24, 2011 11:47 AM

Answers

  • User-434868552 posted

    @ Jack1987

    You wrote (or borrowed) JavaScript code to save your user's language in the cookie.

    Your JavaScript code does not fire randomly; something somewhere in your logic/design causes your JavaScript code to save the user's language, for example, your user might be clicking a button hooked up to your JavaScript code.

    However, at some point your code chooses the else path.

    if (HttpContext.Current.Request.Cookies["lang"] != null) {

    we know now for certain that the else path through your code gets chosen because you changed the language to French in your else block and French replaced en-US in your misbehaving viewe.

    else { 
    // use en-US as the default 
    var culture = new System.Globalization.CultureInfo("en-US"); 
    Thread.CurrentThread.CurrentCulture = culture; 
    Thread.CurrentThread.CurrentUICulture = culture; 
    } 
    

    That fact tells us that at some point HttpContext.Current.Request.Cookies["lang"] == null because you only set the language to French in your else block when HttpContext.Current.Request.Cookies["lang"] is null.

    So you need to look for the cause of that unexpected null value.

    BTW, you did not answer the question by ignatandrei.

    g.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 29, 2011 6:05 PM

All replies

  • User-434868552 posted

    @ Jack1987

    please note that imho you need to provide more information in order for forums.asp.net community members to be able to answer your question.  you do not need to post your entire project (likely many members would not have a lot of spare time to read through your entire project), just post those lines of code where you are not getting the results the you expected plus a bit more relevant description about your application and the steps that you've already taken to solve your problem; also, if applicable, post relevant parts of configuration files (not the whole file), et cetera.

    otherwise, we are in many cases simply guessing.

    g.

    Saturday, September 24, 2011 12:23 PM
  • User-1320061939 posted

    Hi, gerrlowry, sorry for no provide any helpful information, this is:

    I have this javascript code to save language in the cookie.

    <script type="text/javascript">
                function changeLang(lang) {
                    jQuery.cookie('lang', lang);
                    window.location.reload();
                    return false;
                }
        </script>
    


    then this is the code in C sharp:

    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;
    			}
    			else {
    				// use en-US as the default
    				var culture = new System.Globalization.CultureInfo("en-US");
    				Thread.CurrentThread.CurrentCulture = culture;
    				Thread.CurrentThread.CurrentUICulture = culture;
    			}




    The thing is that the language change in all the site except in only one view, in this view the language keep change back to en-US, is like language cookie don´t exist when I try to render this especific view.

    Wednesday, September 28, 2011 2:40 PM
  • User197322208 posted

    The thing is that the language change in all the site except in only one view, in this view the language keep change back to en-US, is like language cookie don´t exist when I try to render this especific view.

    What do you have as action/view in the view that changes the language and in the view that does not change the language?
    And press F5 - maybe it is a caching problem...

    Wednesday, September 28, 2011 6:35 PM
  • User-434868552 posted

    @ Jack1987

    Here's an experiment to try .... your else block defaults to "en-US" ... what happens if you default to another language, for example French?

    Do you still get your problem view as  "en-US" or is it now French?

    g. 

    Wednesday, September 28, 2011 9:41 PM
  • User-1320061939 posted

    @ gerrylowry

    You are right, it´s French now. I change default to French and now it´s show French in the problem view.

    Thursday, September 29, 2011 9:16 AM
  • User-434868552 posted

    @ Jack1987

    By implication, your JavaScript is not setting the language for the "problem view" ... you need to determine the reason that it's failing.

    g.

    Thursday, September 29, 2011 10:11 AM
  • User-1320061939 posted

    ok, thanks I will search for the error in my application

    Thursday, September 29, 2011 4:03 PM
  • User-1320061939 posted

    I am sorry but I don´t understand your reply, please can you help me?

    Thursday, September 29, 2011 4:05 PM
  • User-434868552 posted

    @ Jack1987

    You wrote (or borrowed) JavaScript code to save your user's language in the cookie.

    Your JavaScript code does not fire randomly; something somewhere in your logic/design causes your JavaScript code to save the user's language, for example, your user might be clicking a button hooked up to your JavaScript code.

    However, at some point your code chooses the else path.

    if (HttpContext.Current.Request.Cookies["lang"] != null) {

    we know now for certain that the else path through your code gets chosen because you changed the language to French in your else block and French replaced en-US in your misbehaving viewe.

    else { 
    // use en-US as the default 
    var culture = new System.Globalization.CultureInfo("en-US"); 
    Thread.CurrentThread.CurrentCulture = culture; 
    Thread.CurrentThread.CurrentUICulture = culture; 
    } 
    

    That fact tells us that at some point HttpContext.Current.Request.Cookies["lang"] == null because you only set the language to French in your else block when HttpContext.Current.Request.Cookies["lang"] is null.

    So you need to look for the cause of that unexpected null value.

    BTW, you did not answer the question by ignatandrei.

    g.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 29, 2011 6:05 PM
  • User-1320061939 posted

    @ ignatandrei

    Sorry, but I don´t understand what are you asking.

    Monday, October 3, 2011 4:14 PM
  • User197322208 posted

    Hello Jack

    I was asking about the specific differences between a page that language changes and a page that does not change the language.

    However,  does not matter now since you've solved the problem.

    Monday, October 3, 2011 9:59 PM
  • User-1320061939 posted

    Hello ignatandrei

    I solve the problem, thanks a lot for the help, thanks a lot to gerrylowry

    Wednesday, October 5, 2011 2:48 PM