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..439f6e28641 --- /dev/null +++ b/grails-plugin-i18n/src/main/groovy/org/grails/plugins/i18n/I18nAutoConfiguration.java @@ -0,0 +1,62 @@ +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 = { 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(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) + public LocaleResolver localeResolver() { + return new SessionLocaleResolver(); + } + + @Bean + public LocaleChangeInterceptor localeChangeInterceptor() { + ParamsAwareLocaleChangeInterceptor localeChangeInterceptor = new ParamsAwareLocaleChangeInterceptor(); + 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 34d97d4356e..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,29 +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 - } - - localeChangeInterceptor(ParamsAwareLocaleChangeInterceptor) { - paramName = "lang" - } - - localeResolver(SessionLocaleResolver) - }} - @Override void onChange(Map event) { def ctx = applicationContext 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..6e4f0121063 --- /dev/null +++ b/grails-plugin-i18n/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +org.grails.plugins.i18n.I18nAutoConfiguration 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"