0

I've got a jQuery i18n plugin init:

jQuery.i18n.properties({
    name : 'appsConstants',
    path : '/gadgets/',
    mode : 'both',
    language : 'en'
});

How to take out the language variable into browser line like a parameter?

James Allardice
  • 164,175
  • 21
  • 332
  • 312
Pilot
  • 575
  • 1
  • 5
  • 11
  • Do you mean you want the `language` property to be configurable in the URL? e.g. `example.com?language=en`? – James Allardice Jun 26 '12 at 07:26
  • possible duplicate of [Get query string values in JavaScript](http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) – JJJ Jun 26 '12 at 07:35
  • @Juhana, but thats parsing parameter, I believe there should be simple setting. Like on JSP: – Pilot Jun 26 '12 at 07:46
  • 3
    Unfortunately there is no such thing built into JavaScript. You have to add for example the function in the accepted answer in the duplicate and then do `language: getParameterByName( 'locale' )`. – JJJ Jun 26 '12 at 07:55

2 Answers2

0

You have several options here:

  • Read the language from the URL path or an URL query parameter by parsing window.location
  • Set the language in a cookie
  • Generate your init Javascript dynamically on the server and set the language there (storing it in the session on the server).
chiborg
  • 26,978
  • 14
  • 97
  • 115
0

This article from Scott Hanselman talks about globalization (cultures/locale)

http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

I've used that for making my jQuery aware of the users culture (for use with a datepicker amongst others) I also downloaded the i18n jQuery plugin and registered that.

I went with the "Slightly Less Cheesy - Meta Tag" solution for "detecting" locale and registered a

<meta name="accept-language" content="en-US">

in the head section, then I could utilize that tag in my jQuery to figure out what locale/language to use.

The article is slightly outdated so some of the javascript needs an update here and there as an example Globalization.js has changed from $.global. to Globalize. Bits and pieces like that you need to update to get it all working.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
thmsn
  • 1,976
  • 1
  • 18
  • 25