Grails url mapping localization
Posted by admin - 15/01/10 at 12:01:42 pmThis is simple "hack" that allows you to internationalize your url in your Grails (1.2.x) application. Lets say that we have application that is provided in two languages: Polish and English. Create ours controller:
FooController { def bar { } }
In ours locale files (message_pl.properties and message_en.properties) add "locale" param (because grails bug) with code specified for this file (pl, en).
Next step is to create new taglib that will generate internationalized link:
This Taglib doing nothing than add prefix for name of the route. It use util method StringUtils.toUpperCaseFristLetter, that capitalize first letter of provided string:
class StringUtils { text[0].toUpperCase() + text[1..-1] } }
Now we can provide url mappings as:
In ours gsp files we can use it like:
<g:localizedLink mapping="goToFooBar"><g:message code="text.goToFooBar" /></g:localizedLink>
5 Comments
RSS feed for comments on this post.
Sorry, the comment form is closed at this time.
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.
Thanks you for a nice solution.
Pozdrawiam, Szymon
Comment by Szymon Polom — July 25, 2010 #
[…] Grails url mapping localization Теги: grails, localization […]
Pingback by Grails: dynamic locale change < Stanfy Блог — November 19, 2010 #
Unfortunately this code doesn’t work in Grails 1.3.7:
out << link(attrs, body)
But you can fix it, just change some parts.
1) import this:
import org.codehaus.groovy.grails.plugins.web.taglib.*
class BaseTagLib {...
and
applicationTagLib.link.call(attrs, body)
instead ofout << link(attrs, body)
Comment by Vladimir — June 25, 2011 #
Oh, sorry, I forgot one peace of code.
You have to add
def applicationTagLib = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
before
applicationTagLib.link.call(attrs, body)
Comment by Vladimir — June 25, 2011 #
Thank you very much for this article. it saved me lots of time.
Comment by Logo Design — July 13, 2011 #