Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18nAutoConfiguration #13847

Open
wants to merge 8 commits into
base: 7.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<String, Object> event) {
def ctx = applicationContext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.grails.plugins.i18n.I18nAutoConfiguration
Loading