Skip to content

Commit

Permalink
Grails i18n Plugin: Support watching i18n properties, use Ant to conv…
Browse files Browse the repository at this point in the history
…ert files to ASCII
  • Loading branch information
rainboyan committed May 24, 2023
1 parent 9bd27ee commit 0adeb49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class GrailsDevelopmentModeWatchApplicationContextInitializer implements

private static final Log logger = LogFactory.getLog(GrailsDevelopmentModeWatchApplicationContextInitializer.class);

private static final List<String> FILE_EXTENSIONS = List.of("groovy", "java", "properties");
private static final String SOURCE_MAIN_JAVA = "src/main/java";
private static final String SOURCE_MAIN_GROOVY = "src/main/groovy";

Expand Down Expand Up @@ -123,7 +124,7 @@ private void enableDevelopmentModeWatch(Environment environment,
Queue<File> changedFiles = new ConcurrentLinkedQueue<>();
Queue<File> newFiles = new ConcurrentLinkedQueue<>();

this.directoryWatcher.addListener(new FileExtensionFileChangeListener(Arrays.asList("groovy", "java")) {
this.directoryWatcher.addListener(new FileExtensionFileChangeListener(FILE_EXTENSIONS) {

@Override
public void onChange(File file, List<String> extensions) throws IOException {
Expand Down Expand Up @@ -241,11 +242,16 @@ else if (i == 1) {
changedFile = changedFile.getCanonicalFile();
// Groovy files within the 'conf' directory are not compiled
boolean configFileChanged = false;
boolean i18nFileChanged = false;
String confPath = new File(BuildSettings.GRAILS_APP_DIR, "conf").getAbsolutePath();
String i18nPath = new File(BuildSettings.GRAILS_APP_DIR, "i18n").getAbsolutePath();
if (changedFile.getPath().contains(confPath)) {
configFileChanged = true;
}
if (configFileChanged) {
if (changedFile.getPath().contains(i18nPath)) {
i18nFileChanged = true;
}
if (configFileChanged || i18nFileChanged) {
pluginManager.informOfFileChange(changedFile);
}
else {
Expand Down Expand Up @@ -370,7 +376,7 @@ private void configureDirectoryWatcher(DirectoryWatcher directoryWatcher, String
String grailsAppFullPath = BuildSettings.GRAILS_APP_DIR.getAbsolutePath();
String grailsAppPath = grailsAppFullPath.substring(grailsAppFullPath.lastIndexOf(File.separator) + 1);
for (String dir : Arrays.asList(grailsAppPath, SOURCE_MAIN_JAVA, SOURCE_MAIN_GROOVY)) {
directoryWatcher.addWatchDirectory(new File(location, dir), Arrays.asList("groovy", "java"));
directoryWatcher.addWatchDirectory(new File(location, dir), FILE_EXTENSIONS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.grails.plugins.i18n

import java.nio.file.Files

import groovy.ant.AntBuilder
import groovy.util.logging.Slf4j
import org.springframework.core.PriorityOrdered
import org.springframework.core.io.Resource
Expand All @@ -38,7 +39,7 @@ import org.grails.spring.context.support.ReloadableResourceBundleMessageSource
class I18nGrailsPlugin extends Plugin implements PriorityOrdered {

String version = GrailsUtil.getGrailsVersion()
String watchedResources = ['file:./grails-app/i18n/**/*.properties',
def watchedResources = ['file:./grails-app/i18n/**/*.properties',
'file:./app/i18n/**/*.properties']

@Override
Expand Down Expand Up @@ -77,31 +78,16 @@ class I18nGrailsPlugin extends Plugin implements PriorityOrdered {
boolean nativeascii = application.config.getProperty('grails.enable.native2ascii', Boolean, true)
def resourcesDir = BuildSettings.RESOURCES_DIR
def classesDir = BuildSettings.CLASSES_DIR
def i18nDir = new File(BuildSettings.GRAILS_APP_DIR, 'i18n')

if (resourcesDir.exists() && event.source instanceof Resource) {
File eventFile = event.source.file.canonicalFile
File i18nDir = eventFile.parentFile
if (isChildOfFile(eventFile, i18nDir)) {
if (i18nDir.name == 'i18n' && i18nDir.parentFile.name in ['grails-app', 'app']) {
def appDir = i18nDir.parentFile.parentFile
resourcesDir = new File(appDir, BuildSettings.BUILD_RESOURCES_PATH)
classesDir = new File(appDir, BuildSettings.BUILD_CLASSES_PATH)
}

if (nativeascii) {
// if native2ascii is enabled then read the properties and write them out again
// so that unicode escaping is applied
def properties = new Properties()
eventFile.withReader {
properties.load(it)
}
// by using an OutputStream the unicode characters will be escaped
new File(resourcesDir, eventFile.name).withOutputStream {
properties.store(it, '')
}
new File(classesDir, eventFile.name).withOutputStream {
properties.store(it, '')
}
// if native2ascii is enabled then converts files from native encodings to ASCII
def ant = new AntBuilder()
ant.native2ascii(src: i18nDir, dest: resourcesDir,
includes: eventFile.name, encoding: 'UTF-8')
}
else {
// otherwise just copy the file as is
Expand Down

0 comments on commit 0adeb49

Please sign in to comment.