Skip to content

Commit 312bcdf

Browse files
committed
Partial fix for #10196 - Remove dependency on groovy-ant for reloading of messages.properties
1 parent b5501df commit 312bcdf

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ import grails.plugins.Plugin
2020
import grails.util.BuildSettings
2121
import grails.util.Environment
2222
import grails.util.GrailsUtil
23-
import org.apache.commons.logging.LogFactory
23+
import groovy.util.logging.Slf4j
2424
import org.grails.spring.context.support.PluginAwareResourceBundleMessageSource
2525
import org.grails.web.i18n.ParamsAwareLocaleChangeInterceptor
2626
import org.springframework.context.support.ReloadableResourceBundleMessageSource
2727
import org.springframework.core.io.Resource
28-
import org.springframework.util.ClassUtils
2928
import org.springframework.web.servlet.i18n.SessionLocaleResolver
3029

30+
import java.nio.file.Files
31+
3132
/**
3233
* Configures Grails' internationalisation support.
3334
*
3435
* @author Graeme Rocher
3536
* @since 0.4
3637
*/
38+
@Slf4j
3739
class I18nGrailsPlugin extends Plugin {
3840

39-
private static LOG = LogFactory.getLog(this)
4041
public static final String I18N_CACHE_SECONDS = 'grails.i18n.cache.seconds'
4142
public static final String I18N_FILE_CACHE_SECONDS = 'grails.i18n.filecache.seconds'
4243

@@ -98,28 +99,45 @@ class I18nGrailsPlugin extends Plugin {
9899
def ctx = applicationContext
99100
def application = grailsApplication
100101
if (!ctx) {
101-
LOG.debug("Application context not found. Can't reload")
102+
log.debug("Application context not found. Can't reload")
102103
return
103104
}
104105

105-
if(!ClassUtils.isPresent("groovy.util.AntBuilder", grailsApplication.classLoader)) return
106-
107-
def nativeascii = application.config.getProperty('grails.enable.native2ascii', Boolean, true)
106+
boolean nativeascii = application.config.getProperty('grails.enable.native2ascii', Boolean, true)
108107
def resourcesDir = BuildSettings.RESOURCES_DIR
109108
def classesDir = BuildSettings.CLASSES_DIR
110109

111110
if (resourcesDir.exists() && event.source instanceof Resource) {
112111
File eventFile = event.source.file.canonicalFile
113-
def ant = getClass().classLoader.loadClass("groovy.util.AntBuilder").newInstance()
114112
File i18nDir = eventFile.parentFile
115113
if (isChildOfFile(eventFile, i18nDir)) {
116114
if( i18nDir.name == 'i18n' && i18nDir.parentFile.name == 'grails-app') {
117115
def appDir = i18nDir.parentFile.parentFile
118116
resourcesDir = new File(appDir, BuildSettings.BUILD_RESOURCES_PATH)
119117
classesDir = new File(appDir, BuildSettings.BUILD_CLASSES_PATH)
120118
}
121-
executeMessageBundleCopy(ant, eventFile, i18nDir, resourcesDir, nativeascii)
122-
executeMessageBundleCopy(ant, eventFile, i18nDir, classesDir, nativeascii)
119+
120+
if(nativeascii) {
121+
// if native2ascii is enabled then read the properties and write them out again
122+
// so that unicode escaping is applied
123+
def properties = new Properties()
124+
eventFile.withReader {
125+
properties.load(it)
126+
}
127+
// by using an OutputStream the unicode characters will be escaped
128+
new File(resourcesDir, eventFile.name).withOutputStream {
129+
properties.store(it, "")
130+
}
131+
new File(classesDir, eventFile.name).withOutputStream {
132+
properties.store(it, "")
133+
}
134+
}
135+
else {
136+
// otherwise just copy the file as is
137+
Files.copy( eventFile.toPath(),new File(resourcesDir, eventFile.name).toPath() )
138+
Files.copy( eventFile.toPath(),new File(classesDir, eventFile.name).toPath() )
139+
}
140+
123141
}
124142
}
125143

@@ -129,23 +147,4 @@ class I18nGrailsPlugin extends Plugin {
129147
}
130148
}
131149

132-
private void executeMessageBundleCopy(ant, File eventFile, File i18nDir, File targetDir, boolean nativeascii) {
133-
def eventFileRelative = relativePath(i18nDir, eventFile)
134-
135-
if (nativeascii) {
136-
ant.copy(todir: targetDir, encoding: "UTF-8") {
137-
138-
fileset(dir: i18nDir) {
139-
include name: eventFileRelative
140-
}
141-
filterchain {
142-
filterreader(classname: 'org.apache.tools.ant.filters.EscapeUnicode')
143-
}
144-
}
145-
} else {
146-
ant.copy(todir: targetDir) {
147-
fileset(dir: i18nDir, includes: eventFileRelative)
148-
}
149-
}
150-
}
151150
}

0 commit comments

Comments
 (0)