Re: can't get setting locale to work
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Friday, August 22, 2008, 10:17:21 PM, Anna Putnam wrote: > Great! Thanks that makes sense (and works). I guess I should have > stated my true objective. I am in the process of internationalizing my app. In some > cases I would like the browser language to drive which locale to > use but in others I would like to override this behavior and force either English or > Spanish depending on the case. Is there an easy way to do this? I > thought the setting approach below would do it but it is not working for me. Any > suggestions? Most certainly you should not deal with choosing the locale inside the templates. Rather, totally outside FreeMarker, when you receive the HTTP request, you decide what view locale you want, and then you set the locale setting of FreeMarker through the FreeMarker Java API (*not* inside the templates; keep them clear of these highly technical/expert things if possible). Regarding the nature of FreeMarker setting in general read: http://freemarker.org/docs/pgui_config_settings.html You most certainly will want to adjust the locale setting in "Layer 3", as that is the scope that is (usually) created per-HTTP-request. Now adjusting layer 3 settings is a bit tricky, because the Environment object is created internally by Template.process. So, where now you do something like this: myTemplate.process(root, out); instead you will do something like this: Environment env = myTemplate.createProcessingEnvironment(root, out); env.setTheSettingYouWant(...); env.setTheOtherSettingYouWant(...); ... env.process(); // process the template This is equivalent with the previous, only you have chance to adjust the environment before the template is executed. Last not least, note the Configuration.getTemplate has a version that has a Locale parameter to find the localized variation of the template. Also note this regarding the behavior of #incluide and #import: http://freemarker.org/docs/ref_directive_include.html#ref_directive_include_localized > Thanks again, > Anna > > -----Original Message----- > From: Daniel Dekany [mailto:[email protected]] > Sent: Friday, August 22, 2008 1:14 PM > To: Anna Putnam > Cc: [email protected] > Subject: Re: [FreeMarker-user] can't get setting locale to work > > Friday, August 22, 2008, 9:57:55 PM, Anna Putnam wrote: > >> I am having trouble using >> <#setting locale="es_ES"/> >> >> I have this in my ftl code: >> >> <h1>${locale}</h1> >> <#setting locale="es_ES"/> >> <h1>${locale}</h1> >> >> And always my page has: >> >> <h1>en_US</h1> >> <h1>en_US</h1> >> >> Is there other configuration that I need to make this work? > > When you write ${locale}, you print the *plain* variable (most > certainly a data-model variable) called "locale". That you could set > as <#assign locale = "es_ES">, but as it is just a plain variable it > wouldn't have any special meaning for FreeMarker. Now, when you write > write <#setting locale="es_ES"> (as opposed to #assign) then you set the > FreeMasker *setting* called "locale". For reading the value of that > setting you can use the *special* variable called "locale", and for > referring to special variable you use an initial dot, like this: > ${.locale} > >> I am using freemarker version 2.3.8 >> >> Thank you in advance. >> >> -Anna >> >> > > -- > Best regards, > Daniel Dekany > > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > FreeMarker-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/freemarker-user -- Best regards, Daniel Dekany ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/