GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / groovy / org / codehaus / groovy / grails / plugins / i18n / I18nGrailsPlugin.groovy
blob8f0856f7e549181bb82aa28631b256b89ee42a82
1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
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;
27 /**
28 * A plug-in that configures Grails' internationalisation support
30 * @author Graeme Rocher
31 * @since 0.4
33 class I18nGrailsPlugin {
35 def version = grails.util.GrailsUtil.getGrailsVersion()
36 def watchedResources = "file:./grails-app/i18n/*.properties"
38 def doWithSpring = {
39 // find i18n resource bundles and resolve basenames
40 def baseNames = []
42 def messageResources
43 if(application.warDeployed) {
44 messageResources = parentCtx?.getResources("**/WEB-INF/grails-app/i18n/*.properties")?.toList()
46 else {
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) {
67 paramName = "lang"
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
80 if (!context) {
81 log.debug("Application context not found. Can't reload")
82 return
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",
91 dest:i18nDir,
92 includes:"*.properties",
93 encoding:"UTF-8")
96 else {
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()
106 else {
107 log.warn("Bean messageSource is not an instance of org.springframework.context.support.ReloadableResourceBundleMessageSource. Can't reload")