How to change the default message.properties

喜夏-厌秋 提交于 2019-12-11 12:40:25

问题


Let's say we have the following line of code:

<p>    <g:message code="nav.usuario.show"  />    </p>

If we are using an italian computer, Grails will look at messages_it.properties first. If grails desn't find nav.usuario.show=textLabel there, will try to find it in messages.properties. I want to change this behavior to look at message_es.properties instead the default messages.properties (but only if the label is not in the current locale language)

I tried the following code, but I didn't see any change. resources.groovy:

beans = {   
    localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
        defaultLocale = new Locale("it","")
        java.util.Locale.setDefault(defaultLocale)
    }   
}

回答1:


Simply copy the code of the language you want by default (for example messages_es.properties) to the default messages.properties file.

If you want to keep the English language, you have to create a new file with a name like messages_en.properties. Move the code of messages.properties there.




回答2:


If you want to fix the locale, put the following lines into the init closure of your BootStrap.groovy:

TimeZone.setDefault(TimeZone.getTimeZone("CET"))
Locale.setDefault(new Locale("it"));

or

Locale.setDefault(new Locale("es"));



回答3:


beans = {   
    localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver)   
}

Then setup a filter to change the default locale based on the request.

//Filter
class LocaleFilters {
    def localeResolver

    def filters = {
        localize(controller: '*') {
            before = {
                 Locale.setDefault(localeResolver.resolveLocale(request))
                 return true
            }
        }
    }
}

If the local context is Spanish then the default sets to _es, so on an so forth for other locales based on which locale the application is accessed.



来源:https://stackoverflow.com/questions/18617482/how-to-change-the-default-message-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!