detect and set culture automatically c# mvc












0















I am new in c# mvc, and I'm trying to make a multi language website, now so far I've been reading the best practice is work with resources or files resx and to set the language o culture in a cookie variable, but my question is how do I set the culture to a cookie automatically? I mean



if A person goes to the website for first time; how di I globally detect the pc or browser language and set the culture to that language detected for every future action the person will make on the website, also store it in a cookie variable due to if the person change the language and visit the website again,the website initializes with the language that the person chose.



so far I've got this:



global.asax



protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie language = HttpContext.Current.Request.Cookies["CultureInfo"];
if (language != null && language.Value != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language.Value);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language.Value);
}
else
{
Thread.CurrentThread.CurrentCulture = someWayToDetectThePcOrBrowserLanguage;
Thread.CurrentThread.CurrentUICulture = someWayToDetectThePcOrBrowserLanguage;
}

}


the someWayToDetectThePcOrBrowserLanguage method I don't have it, I'm looking for a way to do that. And once done that if the person is form a english country the url looks like this:



www.website.com/en/



if the person is from a spanish country looks like this:



www.website.com/es/



and so goes on for some more languages.



now to change the language I have this for the html:



<form id="languages" method="get">
<select name="selectLanguages" id="selectLanguages>
<option value="en">English</option>
<option value="es">Español</option>
<option value="fr">Français</option>
</select>
</form>


my jquery is this one:



$('#selectLanguages').on('change', function () {
var id = $(this).val();
$.ajax({
type: 'GET',
url: '/Language/ChangeCulture',
data: { lang: id },
success: function (e) {

}
})
})


finally the function is this one:



public void ChangeCulture(string lang)
{
Response.Cookies.Remove("CultureInfo");

HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["CultureInfo"];

if (languageCookie == null) languageCookie = new HttpCookie("CultureInfo");

languageCookie.Value = lang;

languageCookie.Expires = DateTime.Now.AddDays(10);

Response.SetCookie(languageCookie);

Response.Redirect(Request.UrlReferrer.ToString());
}


from this point on, I'm totally lost, if you can help me, this intends to change the url from this www.website.com/AutoDetectedLanguage/ to this www.website.com/anotherLanguage/



And also for every future action stay in the selected or detected culture e.g



www.website.com/en/controller/action



or



www.website.com/en/anotherController/anotherAction



if language changes



www.website.com/es/sameController/sameAction










share|improve this question




















  • 1





    Possible duplicate of Get CultureInfo from current visitor and setting resources based on that?. See also Detecting browser display language

    – Heretic Monkey
    Nov 20 '18 at 20:50













  • @HereticMonkey this solved one probleme that is detecting the browser language, but any ideas on how could I initialize the website with the language detected? e.g www.website.com/en/

    – Manuel Gonçalves
    Nov 20 '18 at 21:00








  • 1





    Then that's a dupe of Make URL language-specific (via routes) :)

    – Heretic Monkey
    Nov 20 '18 at 21:04











  • @HereticMonkey I do have the routeConfig.cs exacally that way but I can't get it to inialize this way www.example.com/en/ instead of www.example.com

    – Manuel Gonçalves
    Nov 21 '18 at 17:27
















0















I am new in c# mvc, and I'm trying to make a multi language website, now so far I've been reading the best practice is work with resources or files resx and to set the language o culture in a cookie variable, but my question is how do I set the culture to a cookie automatically? I mean



if A person goes to the website for first time; how di I globally detect the pc or browser language and set the culture to that language detected for every future action the person will make on the website, also store it in a cookie variable due to if the person change the language and visit the website again,the website initializes with the language that the person chose.



so far I've got this:



global.asax



protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie language = HttpContext.Current.Request.Cookies["CultureInfo"];
if (language != null && language.Value != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language.Value);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language.Value);
}
else
{
Thread.CurrentThread.CurrentCulture = someWayToDetectThePcOrBrowserLanguage;
Thread.CurrentThread.CurrentUICulture = someWayToDetectThePcOrBrowserLanguage;
}

}


the someWayToDetectThePcOrBrowserLanguage method I don't have it, I'm looking for a way to do that. And once done that if the person is form a english country the url looks like this:



www.website.com/en/



if the person is from a spanish country looks like this:



www.website.com/es/



and so goes on for some more languages.



now to change the language I have this for the html:



<form id="languages" method="get">
<select name="selectLanguages" id="selectLanguages>
<option value="en">English</option>
<option value="es">Español</option>
<option value="fr">Français</option>
</select>
</form>


my jquery is this one:



$('#selectLanguages').on('change', function () {
var id = $(this).val();
$.ajax({
type: 'GET',
url: '/Language/ChangeCulture',
data: { lang: id },
success: function (e) {

}
})
})


finally the function is this one:



public void ChangeCulture(string lang)
{
Response.Cookies.Remove("CultureInfo");

HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["CultureInfo"];

if (languageCookie == null) languageCookie = new HttpCookie("CultureInfo");

languageCookie.Value = lang;

languageCookie.Expires = DateTime.Now.AddDays(10);

Response.SetCookie(languageCookie);

Response.Redirect(Request.UrlReferrer.ToString());
}


from this point on, I'm totally lost, if you can help me, this intends to change the url from this www.website.com/AutoDetectedLanguage/ to this www.website.com/anotherLanguage/



And also for every future action stay in the selected or detected culture e.g



www.website.com/en/controller/action



or



www.website.com/en/anotherController/anotherAction



if language changes



www.website.com/es/sameController/sameAction










share|improve this question




















  • 1





    Possible duplicate of Get CultureInfo from current visitor and setting resources based on that?. See also Detecting browser display language

    – Heretic Monkey
    Nov 20 '18 at 20:50













  • @HereticMonkey this solved one probleme that is detecting the browser language, but any ideas on how could I initialize the website with the language detected? e.g www.website.com/en/

    – Manuel Gonçalves
    Nov 20 '18 at 21:00








  • 1





    Then that's a dupe of Make URL language-specific (via routes) :)

    – Heretic Monkey
    Nov 20 '18 at 21:04











  • @HereticMonkey I do have the routeConfig.cs exacally that way but I can't get it to inialize this way www.example.com/en/ instead of www.example.com

    – Manuel Gonçalves
    Nov 21 '18 at 17:27














0












0








0








I am new in c# mvc, and I'm trying to make a multi language website, now so far I've been reading the best practice is work with resources or files resx and to set the language o culture in a cookie variable, but my question is how do I set the culture to a cookie automatically? I mean



if A person goes to the website for first time; how di I globally detect the pc or browser language and set the culture to that language detected for every future action the person will make on the website, also store it in a cookie variable due to if the person change the language and visit the website again,the website initializes with the language that the person chose.



so far I've got this:



global.asax



protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie language = HttpContext.Current.Request.Cookies["CultureInfo"];
if (language != null && language.Value != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language.Value);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language.Value);
}
else
{
Thread.CurrentThread.CurrentCulture = someWayToDetectThePcOrBrowserLanguage;
Thread.CurrentThread.CurrentUICulture = someWayToDetectThePcOrBrowserLanguage;
}

}


the someWayToDetectThePcOrBrowserLanguage method I don't have it, I'm looking for a way to do that. And once done that if the person is form a english country the url looks like this:



www.website.com/en/



if the person is from a spanish country looks like this:



www.website.com/es/



and so goes on for some more languages.



now to change the language I have this for the html:



<form id="languages" method="get">
<select name="selectLanguages" id="selectLanguages>
<option value="en">English</option>
<option value="es">Español</option>
<option value="fr">Français</option>
</select>
</form>


my jquery is this one:



$('#selectLanguages').on('change', function () {
var id = $(this).val();
$.ajax({
type: 'GET',
url: '/Language/ChangeCulture',
data: { lang: id },
success: function (e) {

}
})
})


finally the function is this one:



public void ChangeCulture(string lang)
{
Response.Cookies.Remove("CultureInfo");

HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["CultureInfo"];

if (languageCookie == null) languageCookie = new HttpCookie("CultureInfo");

languageCookie.Value = lang;

languageCookie.Expires = DateTime.Now.AddDays(10);

Response.SetCookie(languageCookie);

Response.Redirect(Request.UrlReferrer.ToString());
}


from this point on, I'm totally lost, if you can help me, this intends to change the url from this www.website.com/AutoDetectedLanguage/ to this www.website.com/anotherLanguage/



And also for every future action stay in the selected or detected culture e.g



www.website.com/en/controller/action



or



www.website.com/en/anotherController/anotherAction



if language changes



www.website.com/es/sameController/sameAction










share|improve this question
















I am new in c# mvc, and I'm trying to make a multi language website, now so far I've been reading the best practice is work with resources or files resx and to set the language o culture in a cookie variable, but my question is how do I set the culture to a cookie automatically? I mean



if A person goes to the website for first time; how di I globally detect the pc or browser language and set the culture to that language detected for every future action the person will make on the website, also store it in a cookie variable due to if the person change the language and visit the website again,the website initializes with the language that the person chose.



so far I've got this:



global.asax



protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie language = HttpContext.Current.Request.Cookies["CultureInfo"];
if (language != null && language.Value != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language.Value);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language.Value);
}
else
{
Thread.CurrentThread.CurrentCulture = someWayToDetectThePcOrBrowserLanguage;
Thread.CurrentThread.CurrentUICulture = someWayToDetectThePcOrBrowserLanguage;
}

}


the someWayToDetectThePcOrBrowserLanguage method I don't have it, I'm looking for a way to do that. And once done that if the person is form a english country the url looks like this:



www.website.com/en/



if the person is from a spanish country looks like this:



www.website.com/es/



and so goes on for some more languages.



now to change the language I have this for the html:



<form id="languages" method="get">
<select name="selectLanguages" id="selectLanguages>
<option value="en">English</option>
<option value="es">Español</option>
<option value="fr">Français</option>
</select>
</form>


my jquery is this one:



$('#selectLanguages').on('change', function () {
var id = $(this).val();
$.ajax({
type: 'GET',
url: '/Language/ChangeCulture',
data: { lang: id },
success: function (e) {

}
})
})


finally the function is this one:



public void ChangeCulture(string lang)
{
Response.Cookies.Remove("CultureInfo");

HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["CultureInfo"];

if (languageCookie == null) languageCookie = new HttpCookie("CultureInfo");

languageCookie.Value = lang;

languageCookie.Expires = DateTime.Now.AddDays(10);

Response.SetCookie(languageCookie);

Response.Redirect(Request.UrlReferrer.ToString());
}


from this point on, I'm totally lost, if you can help me, this intends to change the url from this www.website.com/AutoDetectedLanguage/ to this www.website.com/anotherLanguage/



And also for every future action stay in the selected or detected culture e.g



www.website.com/en/controller/action



or



www.website.com/en/anotherController/anotherAction



if language changes



www.website.com/es/sameController/sameAction







c# asp.net-mvc asp.net-mvc-4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 19:50







Manuel Gonçalves

















asked Nov 20 '18 at 19:43









Manuel GonçalvesManuel Gonçalves

255




255








  • 1





    Possible duplicate of Get CultureInfo from current visitor and setting resources based on that?. See also Detecting browser display language

    – Heretic Monkey
    Nov 20 '18 at 20:50













  • @HereticMonkey this solved one probleme that is detecting the browser language, but any ideas on how could I initialize the website with the language detected? e.g www.website.com/en/

    – Manuel Gonçalves
    Nov 20 '18 at 21:00








  • 1





    Then that's a dupe of Make URL language-specific (via routes) :)

    – Heretic Monkey
    Nov 20 '18 at 21:04











  • @HereticMonkey I do have the routeConfig.cs exacally that way but I can't get it to inialize this way www.example.com/en/ instead of www.example.com

    – Manuel Gonçalves
    Nov 21 '18 at 17:27














  • 1





    Possible duplicate of Get CultureInfo from current visitor and setting resources based on that?. See also Detecting browser display language

    – Heretic Monkey
    Nov 20 '18 at 20:50













  • @HereticMonkey this solved one probleme that is detecting the browser language, but any ideas on how could I initialize the website with the language detected? e.g www.website.com/en/

    – Manuel Gonçalves
    Nov 20 '18 at 21:00








  • 1





    Then that's a dupe of Make URL language-specific (via routes) :)

    – Heretic Monkey
    Nov 20 '18 at 21:04











  • @HereticMonkey I do have the routeConfig.cs exacally that way but I can't get it to inialize this way www.example.com/en/ instead of www.example.com

    – Manuel Gonçalves
    Nov 21 '18 at 17:27








1




1





Possible duplicate of Get CultureInfo from current visitor and setting resources based on that?. See also Detecting browser display language

– Heretic Monkey
Nov 20 '18 at 20:50







Possible duplicate of Get CultureInfo from current visitor and setting resources based on that?. See also Detecting browser display language

– Heretic Monkey
Nov 20 '18 at 20:50















@HereticMonkey this solved one probleme that is detecting the browser language, but any ideas on how could I initialize the website with the language detected? e.g www.website.com/en/

– Manuel Gonçalves
Nov 20 '18 at 21:00







@HereticMonkey this solved one probleme that is detecting the browser language, but any ideas on how could I initialize the website with the language detected? e.g www.website.com/en/

– Manuel Gonçalves
Nov 20 '18 at 21:00






1




1





Then that's a dupe of Make URL language-specific (via routes) :)

– Heretic Monkey
Nov 20 '18 at 21:04





Then that's a dupe of Make URL language-specific (via routes) :)

– Heretic Monkey
Nov 20 '18 at 21:04













@HereticMonkey I do have the routeConfig.cs exacally that way but I can't get it to inialize this way www.example.com/en/ instead of www.example.com

– Manuel Gonçalves
Nov 21 '18 at 17:27





@HereticMonkey I do have the routeConfig.cs exacally that way but I can't get it to inialize this way www.example.com/en/ instead of www.example.com

– Manuel Gonçalves
Nov 21 '18 at 17:27












1 Answer
1






active

oldest

votes


















0














In my opinion I wouldn't go for seeking cookies and saving info in there or read the culture of a pc(cookies could be disabled in user system) remember giving the users the option to choose their language is the best option.
Now in how to solve this issue is to use globalization and localization because that is what they are designed for.



here is a link to help you started in that:



https://www.c-sharpcorner.com/UploadFile/4d9083/globalization-and-localization-in-Asp-Net-mvc-4/



I hope that helps if not many other links can be found on Google. Happy coding further.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400438%2fdetect-and-set-culture-automatically-c-sharp-mvc%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In my opinion I wouldn't go for seeking cookies and saving info in there or read the culture of a pc(cookies could be disabled in user system) remember giving the users the option to choose their language is the best option.
    Now in how to solve this issue is to use globalization and localization because that is what they are designed for.



    here is a link to help you started in that:



    https://www.c-sharpcorner.com/UploadFile/4d9083/globalization-and-localization-in-Asp-Net-mvc-4/



    I hope that helps if not many other links can be found on Google. Happy coding further.






    share|improve this answer




























      0














      In my opinion I wouldn't go for seeking cookies and saving info in there or read the culture of a pc(cookies could be disabled in user system) remember giving the users the option to choose their language is the best option.
      Now in how to solve this issue is to use globalization and localization because that is what they are designed for.



      here is a link to help you started in that:



      https://www.c-sharpcorner.com/UploadFile/4d9083/globalization-and-localization-in-Asp-Net-mvc-4/



      I hope that helps if not many other links can be found on Google. Happy coding further.






      share|improve this answer


























        0












        0








        0







        In my opinion I wouldn't go for seeking cookies and saving info in there or read the culture of a pc(cookies could be disabled in user system) remember giving the users the option to choose their language is the best option.
        Now in how to solve this issue is to use globalization and localization because that is what they are designed for.



        here is a link to help you started in that:



        https://www.c-sharpcorner.com/UploadFile/4d9083/globalization-and-localization-in-Asp-Net-mvc-4/



        I hope that helps if not many other links can be found on Google. Happy coding further.






        share|improve this answer













        In my opinion I wouldn't go for seeking cookies and saving info in there or read the culture of a pc(cookies could be disabled in user system) remember giving the users the option to choose their language is the best option.
        Now in how to solve this issue is to use globalization and localization because that is what they are designed for.



        here is a link to help you started in that:



        https://www.c-sharpcorner.com/UploadFile/4d9083/globalization-and-localization-in-Asp-Net-mvc-4/



        I hope that helps if not many other links can be found on Google. Happy coding further.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 20:48









        GeorgeTGeorgeT

        114




        114
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400438%2fdetect-and-set-culture-automatically-c-sharp-mvc%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word