问题
I am dealing with the following problem. In our project we have launched different internationalized domains for the same website. The problem comes with the UrlMappings for the different languages. for example:
- English: name contact: "/contact"(controller: 'static', action: 'index') { id = '/contact' }
- German: name deContact: "/kontakt"(controller: 'static', action: 'index') { id = '/contact' }
repeating all the code again and again. Is there a solution to group both url mappings to use the same controller, action and logic ?
for example it would be nice to have something similar like this: name contact: "[/contact|/kontakt/etc..]"(controller: 'static', action: 'index') { id = '/contact' } Giving optional urls that calls the same code.
Thanks in advance
回答1:
The URLMapping block is a dsl, but you can use Groovy inside of it.
You could do something like:
['contact', 'kontact'].each{
"/${it}"( view: "/blah")
}
which does create the routings you are asking for. This example is pretty simplistic, but you could potentially hook into things like the i18n message bundles to do something like
getKeysFor( 'contact' ).each{
... your mapping here
}
it feels like this is something that your controller should be handling, since the i18n support is likely to be better there than at the url mapping level.
I am pretty sure this would break your ability to use named url mappings though.
回答2:
You can have the UrlMapping
read the mapping name from the messageSource
per Locale
and use the same in the mapping. Something like:
UrlMapping.groovy
import org.springframework.context.i18n.LocaleContextHolder as LCH
class UrlMappings {
static mappings = {
def i18nContact = getGrailsApplication().getMainContext().getMessage('app.url.mapping.contact', [] as Object[], 'contact', LCH.getLocale())
"/${i18nContact}"(controller: 'static', action: 'index') { id = '/contact' }
}
}
messages.properties
app.url.mapping.contact=contact
messages_de.properties
app.url.mapping.contact=kontakt
You just need to keep on adding the entries for other locales
in the corresponding message source.
Note:
You can test the same by setting to default Locale
to GERMANY in the resources.groovy
as
import org.springframework.web.servlet.i18n.FixedLocaleResolver
beans = {
localeResolver(FixedLocaleResolver, Locale.GERMANY) {
Locale.setDefault(Locale.GERMANY)
}
}
回答3:
I would suggest a similar solution but at least for me an easier one. Basically the idea would be to have the mapping names in messages.properties
messages.properties
app.url.mapping.contact = enContact
messages_es.properties
app.url.mapping.contact = esContact
Then we can have this UrlMappings configuration
static mappings = {
name enContact: "/contact"(view:"/static/contact")
name esContact: "/contacto"(view:"/static/contact")
}
And finally in the views you can use the mapping attribute in this way:
<g:link mapping="${g.message(code:'app.url.mapping.contact', default:'enContact')}">
<g:message code="contact" default="Contact"/>
</g:link>
来源:https://stackoverflow.com/questions/16362408/grails-i18n-urlmappings