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:
class BaseTagLib { def localizedLink = { attrs, body -> attrs.mapping = message(code:'locale') + StringUtils.toUpperCaseFristLetter(attrs.mapping) out << link(attrs, body) } }
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 { static def toUpperCaseFristLetter(String text){ text[0].toUpperCase() + text[1..-1] } }
Now we can provide url mappings as:
class UrlMappings { static mappings = { name enGoToFooBar:'/go/to/foo/Bar' { controller = 'foo' action = 'bar' } name plGoToFooBar:'/idz/do/foo/Bar' { controller = 'foo' action = 'bar' } } }
In ours gsp files we can use it like:
<g:localizedLink mapping="goToFooBar"><g:message code="text.goToFooBar" /></g:localizedLink>
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.