setting and changing locale in jquery globalize -
i trying understand best practice automatically setting locale , potentially dynamically changing it, when using jquery-globalize library.
the requirements getting , running in jquery globalize, or @ least understand them are:
- include required javascript files
- load cldr data
- set locale
step #1 out of scope of question; assume prerequisite move forward.
step #2 documented plain javascript looking this, in example refer "dynamic" loading:
$.when( $.get( "cldr/main/en/ca-gregorian.json" ), $.get( "cldr/supplemental/likelysubtags.json" ), $.get( "cldr/supplemental/timedata.json" ), $.get( "cldr/supplemental/weekdata.json" ) ).then(function() { // normalize $.get results, need json, not request statuses. return [].slice.apply( arguments, [ 0 ] ).map(function( result ) { return result[ 0 ]; }); }).then( globalize.load ).then(function() { // code goes here. });
if understand code properly, uses chain of promises: json, normalize it, run through globalize.load, "your code".
however, not dynamic in sense of word use it. not responding user input or state changes. stating front "load english calendar information".
there other modules needed. don't believe globalize "aware" of directory structure of languages, meaning need load in these other files well, passing them globalize.load(json)
1 way or another.
then, although documentation bit disjointed, believe need set locale, step #3.
globalize.locale( "en" )
so finally, on questions:
- in order set locale, need aware of culture. practice pull user agent string or somesuch? in event don't provide detected locale, should manually fall given language?
in other words, there functionality built globalize handle grabbing brower or system culture , try using automatically, or need explicitly call globalize.locale()
?
i can set culture corresponding cldr json loaded. on language change or load, need invoke similar set of calls sample "load" script? don't suspect calling
globalize.locale(newlocale)
going unless json loaded. this?:function changelocale(locale) { // "validlocales" not shown in sample, imagine array of locales have explicitly provided cldr in distribution if(validlocales.indexof(locale) !== -1) {
$.when( $.get( "cldr/main/" + locale + "/ca-gregorian.json" ), ).then(function() { // normalize function }).then(globalize.load).then(function(){ // rest of stuff function(s) }); } else { // handle invalid locale }
?
this seems... clear-ish... documentation, isn't explicitly documented this, i'm not sure if i'm missing out on default behaviours don't have code (because they're inherent).
- to allow extensible globalization consumers of web application (web administrators deploying it), matter of documentation? "you can't switch 'fr', need download cldr files, place them in x directory, update "validlocales" configurable parameter, modify initialization script set new default? seems rather heavy expect deploying application undertake, vs. "add new language file iso country code, such fr.json".
the way recommend building application using globalize application example using webpack , npm.
now, answering questions more directly...
- in order set locale, need aware of culture. practice pull user agent string or somesuch? in event don't provide detected locale, should manually fall given language?
in other words, there functionality built globalize handle grabbing brower or system culture , try using automatically, or need explicitly call globalize.locale()?
globalize unopinionated how applications should it. applications might find better allow users select site language. applications might find better use user agent information (https://stackoverflow.com/a/674570/798133). globalize should allow both usages.
i can set culture corresponding cldr json loaded. on language change or load, need invoke similar set of calls sample "load" script? don't suspect calling globalize.locale(newlocale) going unless json loaded. this?:
function changelocale(locale) { // "validlocales" not shown in sample, imagine array of locales have explicitly provided cldr in distribution if(validlocales.indexof(locale) !== -1) { $.when( $.get( "cldr/main/" + locale + "/ca-gregorian.json" ), ).then(function() { // normalize function }).then(globalize.load).then(function(){ // rest of stuff function(s) }); } else { // handle invalid locale } ?
this seems... clear-ish... documentation, isn't explicitly documented this, i'm not sure if i'm missing out on default behaviours don't have code (because they're inherent).
you correct. globalize.locale(<locale>)
won't except if proper data has been loaded in advance (either using globalize.load()
load data or using precompiled formatters , parsers optimal performance). so, yeap, application developer must handle that.
the application example using webpack , npm mentioned earlier takes care of loading data , generating optimal bundles production automatically. obviously, globalize allows several other arrangements or usages. let me know if have other/more specific questions here.
to allow extensible globalization consumers of web application (web administrators deploying it), matter of documentation? "you can't switch 'fr', need download cldr files, place them in x directory, update "validlocales" configurable parameter, modify initialization script set new default? seems rather heavy expect deploying application undertake, vs. "add new language file iso country code, such fr.json".
absolutely. believe can more improvements documentation , examples showing our best practices web applications using globalize. application example using webpack , npm recent update we've made, believe being pertinent question. let me know on questions.
Comments
Post a Comment