2 * Copyright 2004-2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.codehaus
.groovy
.grails
.plugins
.i18n
18 import org
.apache
.commons
.io
.FilenameUtils
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.codehaus
.groovy
.grails
.plugins
.support
.GrailsPluginUtils
;
21 import org
.springframework
.web
.servlet
.i18n
.SessionLocaleResolver
;
22 import org
.springframework
.context
.support
.ReloadableResourceBundleMessageSource
23 import org
.codehaus
.groovy
.grails
.commons
.GrailsApplication
24 import org
.codehaus
.groovy
.grails
.support
.DevelopmentResourceLoader
25 import org
.codehaus
.groovy
.grails
.web
.i18n
.ParamsAwareLocaleChangeInterceptor
;
28 * A plug-in that configures Grails' internationalisation support
30 * @author Graeme Rocher
33 class I18nGrailsPlugin
{
35 def version
= grails
.util
.GrailsUtil
.getGrailsVersion()
36 def watchedResources
= "file:./grails-app/i18n/*.properties"
39 // find i18n resource bundles and resolve basenames
43 if(application
.warDeployed
) {
44 messageResources
= parentCtx?
.getResources("**/WEB-INF/grails-app/i18n/*.properties")?
.toList()
47 messageResources
= plugin
.watchedResources
50 messageResources?
.each
{
51 def baseName
= FilenameUtils
.getBaseName(it
.filename
)
52 baseName
= StringUtils
.substringBefore(baseName
, "_") // trims possible locale specification
53 baseNames
<< "WEB-INF/grails-app/i18n/" + baseName
55 baseNames
= baseNames
.unique()
57 log
.debug("Creating messageSource with basenames: " + baseNames
);
59 if(!application
.warDeployed
) {
60 messageSourceLoader(DevelopmentResourceLoader
, ref("grailsApplication", true), System
.getProperty(GrailsApplication
.PROJECT_RESOURCES_DIR
))
63 messageSource(ReloadableResourceBundleMessageSource
) {
64 basenames
= baseNames
.toArray()
66 localeChangeInterceptor(ParamsAwareLocaleChangeInterceptor
) {
69 localeResolver(SessionLocaleResolver
)
72 def doWithApplicationContext
= { ctx
->
73 if(!application
.warDeployed
) {
74 ctx
.messageSource
.resourceLoader
= ctx
.messageSourceLoader
78 def onChange
= { event
->
79 def context
= event
.ctx
81 log
.debug("Application context not found. Can't reload")
85 def i18nDir
= "${System.getProperty(GrailsApplication.PROJECT_RESOURCES_DIR)}/grails-app/i18n"
87 def ant
= new AntBuilder()
89 if(event
.application
.config
.grails
.enable
.native2ascii
== true) {
90 ant
.native2ascii(src
:"./grails-app/i18n",
92 includes
:"*.properties",
97 ant
.copy(todir
:i18nDir
) {
98 fileset(dir
:"./grails-app/i18n", includes
:"*.properties")
102 def messageSource
= context
.getBean("messageSource")
103 if (messageSource
instanceof ReloadableResourceBundleMessageSource
) {
104 messageSource
.clearCache()
107 log
.warn("Bean messageSource is not an instance of org.springframework.context.support.ReloadableResourceBundleMessageSource. Can't reload")