Search code examples
jqueryasp.net-mvcglobalization

Getting localized language in JQuery


I am displaying and determining the selected language in my website by using URLs in this format :

/{languageCode}/Area/Controller/Action

And in my C# when I need to find the language Code I am using this syntax :

RouteData.Values["languageCode"]

However, when I need to call an action using JQuery, how do I determine the language code so that I can call the correct route i.e. en-US/Area/Controller/Action ? I don't know how to access this information in my client side Javascript. Can anybody help?


Solution

  • Since your URL has the language code. How about using

    window.location
    

    https://developer.mozilla.org/en/DOM/window.location

    And then extract the language from the url. Maybe something like:

    var url = "example.com/en-us/Area/Controller/Action"; //or window.location:
    var lang = url.split("/")[1];
    

    No need to use JQuery! :)