|
Hi,
While my multi-languages grails application is growing in size, it would be very convenient for me to have Grails choosing my gsp file according to the current request language (as done for messages). This feature will be very useful whenever my GSP files have large static content. For instance, if my current request language is French, any call to render view:"home.gsp" should first look for file 'home_fr.gsp' and if not found it will render the default file 'home.gsp'. It should work as well for templates.
I suppose that this kind of logic is not currently implemented in Grails, is it? IMHO, this feature will be useful for others but me. Do you think that you will use this feature? Should I create a JIRA issue for that? And last, for urgent matters, how can I implement this easily in my grails application ? Thank you. Fabien. |
|
Hi,
this can be done as follows: import org.springframework.web.servlet.support.RequestContextUtils as RCU class HomeController { def home = { def requestLocale = RCU.getLocale(request) render(view:"home.${requestLocale}") } } You'd also have to create a home.fr.gsp (or home.fr_FR.gsp depending on the actual request locale) file in grails-app/views/home 2010/1/3 Fabien7474 <[hidden email]>: > Hi, > > While my multi-languages grails application is growing in size, it would be > very convenient for me to have Grails choosing my gsp file according to the > current request language (as done for messages). This feature will be very > useful whenever my GSP files have large static content. > For instance, if my current request language is French, any call to render > view:"home.gsp" should first look for file 'home_fr.gsp' and if not found it > will render the default file 'home.gsp'. It should work as well for > templates. > > I suppose that this kind of logic is not currently implemented in Grails, is > it? IMHO, this feature will be useful for others but me. Do you think that > you will use this feature? Should I create a JIRA issue for that? > > And last, for urgent matters, how can I implement this easily in my grails > application ? > > Thank you. > > Fabien. > ________________________________ > View this message in context: New feature: Localized GSP files? > Sent from the grails - user mailing list archive at Nabble.com. > -- Viele Grüße / Best regards, Björn Wilmsmann --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Thx Björn.
Two remarks about your solution: 1- I need to write this piece of code for every action of every controller that implements this GSP localization. Is there a way to make it more generic and to propagate it to every action ? 2 - Default should still work. So in case, the file "home.${requestLocale}" does not exist, I should render by default "home". Do you know how to test for GSP file existence?
|
|
In reply to this post by fabien7474
I don't know if a JIRA issue is need. It is about GSP theme support like Wordpress blog system theme style.
You can achieve it by one tag, import import org.springframework.web.servlet.support.RequestContextUtils as RCU Class LangTemplateTag { static namespace="lang" static returnObjectForTag = ["template"] def template = {attrs -> def locale = RCU.getLocale(request).toString() if (attrs.template) { return attrs.template + "_" + locale } else if (attrs.view) { return attrs.view[0..-5] + "_" + locale + attrs.view[-4..-1] } } } In your controller, you can def template = lang.template("home") render(template: template, model: [your model here]) or def view = lang.template("home.gsp") render(view: view, model:[your model here]) Ugly but workable. Or you can rewrite your template engine. Weceem is a good example for it. I really hope Grails can provide more elastic way to provide template, theme and views. But I has no ideas about the direction:(. Thanks.
|
|
In reply to this post by fabien7474
To check if a view is exist,
boolean viewExist(viewUri) { def resource = grailsAttributes.pagesTemplateEngine.getResourceForUri(viewUri) return ( resource.file && resource.exists() ) }
|
|
In reply to this post by Björn Wilmsmann-2
Hi
You could add something like this to your bootstrap init method. import javax.servlet.http.HttpServletRequest; import org.springframework.web.servlet.support.RequestContextUtils as RCU .. HttpServletRequest.metaClass.getCustomLocale = {-> def locale = RCU.getLocale(delegate); return locale; } .. It adds new method to request class so now in controller (or gsp file or somewhere else) you can simple write request.customLocale to get current locale. Regards Marcin Muras
|
|
Thx for all your answers.
I have implemented what Hubert has suggested and it works perfectly. The idea to hook the request for a new method 'customLocale' is a good idea and I will use it also. Again, thank you, Fabien.
|
|
In reply to this post by HubertChang
Hi
I made complete solution for such requirement "Gives grails possibility to render translated view". I believe my solution is not perfect but it works :) Any suggestions or comments? Code: Add this to init method in bootstrap class //********************************************************************* // view localization // closure checks what localization exist for view def getLocalizedViewName = {viewUri, locale, separator, grailsAttributes -> println "Checking locale "+locale; String lang = locale.getLanguage(); String country = locale.getCountry(); String variant = locale.getVariant(); // Check for file with language, country and variant localization. if (variant.length() > 0) { String location = viewUri + separator + lang + separator + country + separator + variant; println "Check location "+location; def resource = grailsAttributes.pagesTemplateEngine.getResourceForUri(location); if(resource != null && resource.file && resource.exists()) { return location; } } // Check for file with language and country localization. if (country.length() > 0) { String location = viewUri + separator + lang + separator + country; println "Check location "+location; def resource = grailsAttributes.pagesTemplateEngine.getResourceForUri(location); if(resource != null && resource.file && resource.exists()) { return location; } } // Check for document with language localization. if (lang.length() > 0) { String location = viewUri + separator + lang println "Check location "+location; def resource = grailsAttributes.pagesTemplateEngine.getResourceForUri(location); if(resource != null && resource.file && resource.exists()) { return location; } } println "View "+viewUri+" for locale "+locale+" not exist"; return null; } // returns view name with optional localization suffix HttpServletRequest.metaClass.getView = {String viewUri, grailsAttributes -> def separator = '_'; def locale = RCU.getLocale(delegate); // check request locale def localizedViewUri = getLocalizedViewName(viewUri, locale, separator, grailsAttributes) println "localizedViewUri "+localizedViewUri; if(localizedViewUri != null) { return localizedViewUri; } // check for default locale locale = java.util.Locale.getDefault(); localizedViewUri = getLocalizedViewName(viewUri, locale, separator, grailsAttributes) if(localizedViewUri != null) { return localizedViewUri; } // localization don't exist. return default view. return viewUri; } // ****************************************************************************** Now in your controller you simply use solution: def someAction = { def translatedView = request.getView('/someView', grailsAttributes); render(view:translatedView); } translatedView represents view name with locale base suffix (e.g. /someView_en_EN, or /someView_en or maybe /someView if translations doesn't exist) I think that such solution should be in grails out of the box. I feel that the best solution is to extend GrailsViewResolver. Maybe someone have ideas how it should be done and how inject to grails custom view resolver? Enjoy. Marcin Muras
|
| Powered by Nabble | Edit this page |
