From da5b1790dc7fa489c46effc590b480fa303c6378 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 17 Nov 2024 19:08:22 -0800 Subject: [PATCH 1/8] I18nAutoConfiguration --- .../plugins/i18n/I18nAutoConfiguration.java | 21 +++++++++++++++++++ .../plugins/i18n/I18nGrailsPlugin.groovy | 2 -- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java new file mode 100644 index 00000000000..605561621b6 --- /dev/null +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java @@ -0,0 +1,21 @@ +package org.grails.plugins.i18n; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.web.servlet.DispatcherServlet; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.i18n.SessionLocaleResolver; + +@AutoConfiguration(before = { WebMvcAutoConfiguration.class }) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +public class I18nAutoConfiguration { + + @Bean + @ConditionalOnMissingBean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) + public LocaleResolver localeResolver() { + return new SessionLocaleResolver(); + } +} diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy index 34d97d4356e..41d18c9ec31 100644 --- a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy @@ -63,8 +63,6 @@ class I18nGrailsPlugin extends Plugin { localeChangeInterceptor(ParamsAwareLocaleChangeInterceptor) { paramName = "lang" } - - localeResolver(SessionLocaleResolver) }} @Override From fbe10f82c6ec09b95ec6299480ba3797a02f167e Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 17 Nov 2024 19:21:09 -0800 Subject: [PATCH 2/8] Auto configure localeChangeInterceptor --- .../org/grails/plugins/i18n/I18nAutoConfiguration.java | 8 ++++++++ .../org/grails/plugins/i18n/I18nGrailsPlugin.groovy | 4 ---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java index 605561621b6..b4a05e265fd 100644 --- a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java @@ -1,5 +1,6 @@ package org.grails.plugins.i18n; +import org.grails.web.i18n.ParamsAwareLocaleChangeInterceptor; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; @@ -7,6 +8,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; @AutoConfiguration(before = { WebMvcAutoConfiguration.class }) @@ -18,4 +20,10 @@ public class I18nAutoConfiguration { public LocaleResolver localeResolver() { return new SessionLocaleResolver(); } + + public LocaleChangeInterceptor localeChangeInterceptor() { + ParamsAwareLocaleChangeInterceptor localeChangeInterceptor = new ParamsAwareLocaleChangeInterceptor(); + localeChangeInterceptor.setParamName("lang"); + return localeChangeInterceptor; + } } diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy index 41d18c9ec31..1b711c9cd66 100644 --- a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy @@ -59,10 +59,6 @@ class I18nGrailsPlugin extends Plugin { } defaultEncoding = encoding } - - localeChangeInterceptor(ParamsAwareLocaleChangeInterceptor) { - paramName = "lang" - } }} @Override From c299c99ead521bb677658b63c50c3c12777c4e7d Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 17 Nov 2024 19:23:27 -0800 Subject: [PATCH 3/8] add AutoConfiguration.imports file --- ....springframework.boot.autoconfigure.AutoConfiguration.imports | 1 + 1 file changed, 1 insertion(+) create mode 100644 grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000000..070ecd4bead --- /dev/null +++ b/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +org.grails.plugins.web.controllers.ControllersAutoConfiguration From fffa81bfdcb74b3fe26c943ad2241058d3900e8a Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 17 Nov 2024 20:08:53 -0800 Subject: [PATCH 4/8] missing @Bean --- .../groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java | 1 + 1 file changed, 1 insertion(+) diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java index b4a05e265fd..d0b5bd55fa8 100644 --- a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java @@ -21,6 +21,7 @@ public LocaleResolver localeResolver() { return new SessionLocaleResolver(); } + @Bean public LocaleChangeInterceptor localeChangeInterceptor() { ParamsAwareLocaleChangeInterceptor localeChangeInterceptor = new ParamsAwareLocaleChangeInterceptor(); localeChangeInterceptor.setParamName("lang"); From 1b47f9be0b92a0423548f6c475dfdf4d2a1dee8a Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 17 Nov 2024 20:33:37 -0800 Subject: [PATCH 5/8] AutoConfiguration.imports file --- ...springframework.boot.autoconfigure.AutoConfiguration.imports | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 070ecd4bead..6e4f0121063 100644 --- a/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1 @@ -org.grails.plugins.web.controllers.ControllersAutoConfiguration +org.grails.plugins.i18n.I18nAutoConfiguration From 9b1b698e748ca7674fc0eda5e71499343295c000 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 17 Nov 2024 20:41:06 -0800 Subject: [PATCH 6/8] auto configure messageSource --- .../plugins/i18n/I18nAutoConfiguration.java | 35 ++++++++++++++++++- .../plugins/i18n/I18nGrailsPlugin.groovy | 24 ------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java index d0b5bd55fa8..96b3820dd11 100644 --- a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java @@ -1,20 +1,41 @@ package org.grails.plugins.i18n; +import grails.config.Settings; +import grails.core.GrailsApplication; +import grails.plugins.GrailsPluginManager; +import grails.util.Environment; +import org.grails.spring.context.support.PluginAwareResourceBundleMessageSource; import org.grails.web.i18n.ParamsAwareLocaleChangeInterceptor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; +import org.springframework.context.support.AbstractApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; -@AutoConfiguration(before = { WebMvcAutoConfiguration.class }) +@AutoConfiguration(before = { MessageSourceAutoConfiguration.class, WebMvcAutoConfiguration.class }) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class I18nAutoConfiguration { + @Value("${" + Settings.GSP_VIEW_ENCODING + ":UTF-8}") + private String encoding; + + @Value("${" + Settings.GSP_ENABLE_RELOAD + ":false}") + private boolean gspEnableReload; + + @Value("${" + Settings.I18N_CACHE_SECONDS + ":5}") + private int cacheSeconds; + + @Value("${" + Settings.I18N_FILE_CACHE_SECONDS + ":5}") + private int fileCacheSeconds; + @Bean @ConditionalOnMissingBean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) public LocaleResolver localeResolver() { @@ -27,4 +48,16 @@ public LocaleChangeInterceptor localeChangeInterceptor() { localeChangeInterceptor.setParamName("lang"); return localeChangeInterceptor; } + + @Bean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME) + public MessageSource messageSource(GrailsApplication grailsApplication, GrailsPluginManager pluginManager) { + PluginAwareResourceBundleMessageSource messageSource = new PluginAwareResourceBundleMessageSource(grailsApplication, pluginManager); + messageSource.setDefaultEncoding(encoding); + messageSource.setFallbackToSystemLocale(false); + if (Environment.getCurrent().isReloadEnabled() || gspEnableReload) { + messageSource.setCacheSeconds(cacheSeconds); + messageSource.setFileCacheSeconds(fileCacheSeconds); + } + return messageSource; + } } diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy index 1b711c9cd66..0c6d7625d3a 100644 --- a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nGrailsPlugin.groovy @@ -15,19 +15,12 @@ */ package org.grails.plugins.i18n -import grails.config.Config -import grails.config.Settings -import grails.core.GrailsApplication import grails.plugins.Plugin import grails.util.BuildSettings -import grails.util.Environment import grails.util.GrailsUtil import groovy.util.logging.Slf4j -import org.grails.spring.context.support.PluginAwareResourceBundleMessageSource -import org.grails.web.i18n.ParamsAwareLocaleChangeInterceptor import org.springframework.context.support.ReloadableResourceBundleMessageSource import org.springframework.core.io.Resource -import org.springframework.web.servlet.i18n.SessionLocaleResolver import java.nio.file.Files @@ -44,23 +37,6 @@ class I18nGrailsPlugin extends Plugin { String version = GrailsUtil.getGrailsVersion() String watchedResources = "file:./${baseDir}/**/*.properties".toString() - @Override - Closure doWithSpring() {{-> - GrailsApplication application = grailsApplication - Config config = application.config - boolean gspEnableReload = config.getProperty(Settings.GSP_ENABLE_RELOAD, Boolean, false) - String encoding = config.getProperty(Settings.GSP_VIEW_ENCODING, 'UTF-8') - - messageSource(PluginAwareResourceBundleMessageSource, application, pluginManager) { - fallbackToSystemLocale = false - if (Environment.current.isReloadEnabled() || gspEnableReload) { - cacheSeconds = config.getProperty(Settings.I18N_CACHE_SECONDS, Integer, 5) - fileCacheSeconds = config.getProperty(Settings.I18N_FILE_CACHE_SECONDS, Integer, 5) - } - defaultEncoding = encoding - } - }} - @Override void onChange(Map event) { def ctx = applicationContext From 0abd343127405fcff52700d922bc3f3581b6ae65 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 18 Nov 2024 01:27:16 -0800 Subject: [PATCH 7/8] force localeResolver --- .../groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java index 96b3820dd11..439f6e28641 100644 --- a/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java @@ -36,8 +36,7 @@ public class I18nAutoConfiguration { @Value("${" + Settings.I18N_FILE_CACHE_SECONDS + ":5}") private int fileCacheSeconds; - @Bean - @ConditionalOnMissingBean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) + @Bean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) public LocaleResolver localeResolver() { return new SessionLocaleResolver(); } From 0e1f7e4fefc4301a1313eda05c43762b1848952c Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 19 Nov 2024 10:13:46 -0800 Subject: [PATCH 8/8] change to autoconfigure to api --- grails-web-common/build.gradle | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/grails-web-common/build.gradle b/grails-web-common/build.gradle index eac6e4ebad5..157ea36afd9 100644 --- a/grails-web-common/build.gradle +++ b/grails-web-common/build.gradle @@ -10,7 +10,8 @@ dependencies { compileOnly "jakarta.servlet:jakarta.servlet-api" testCompileOnly "org.springframework:spring-test" - compileOnly "org.springframework.boot:spring-boot-autoconfigure" + // now used by plugins for autoconfiguration + api "org.springframework.boot:spring-boot-autoconfigure" api "org.springframework:spring-webmvc" api "org.springframework:spring-context-support" implementation "com.github.ben-manes.caffeine:caffeine"