|
| 1 | +package org.grails.plugins.i18n; |
| 2 | + |
| 3 | +import grails.config.Settings; |
| 4 | +import grails.core.GrailsApplication; |
| 5 | +import grails.plugins.GrailsPluginManager; |
| 6 | +import grails.util.Environment; |
| 7 | +import org.grails.spring.context.support.PluginAwareResourceBundleMessageSource; |
| 8 | +import org.grails.web.i18n.ParamsAwareLocaleChangeInterceptor; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
| 10 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 11 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 12 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 13 | +import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration; |
| 14 | +import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; |
| 15 | +import org.springframework.context.MessageSource; |
| 16 | +import org.springframework.context.annotation.Bean; |
| 17 | +import org.springframework.context.support.AbstractApplicationContext; |
| 18 | +import org.springframework.web.servlet.DispatcherServlet; |
| 19 | +import org.springframework.web.servlet.LocaleResolver; |
| 20 | +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; |
| 21 | +import org.springframework.web.servlet.i18n.SessionLocaleResolver; |
| 22 | + |
| 23 | +@AutoConfiguration(before = { MessageSourceAutoConfiguration.class, WebMvcAutoConfiguration.class }) |
| 24 | +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) |
| 25 | +public class I18nAutoConfiguration { |
| 26 | + |
| 27 | + @Value("${" + Settings.GSP_VIEW_ENCODING + ":UTF-8}") |
| 28 | + private String encoding; |
| 29 | + |
| 30 | + @Value("${" + Settings.GSP_ENABLE_RELOAD + ":false}") |
| 31 | + private boolean gspEnableReload; |
| 32 | + |
| 33 | + @Value("${" + Settings.I18N_CACHE_SECONDS + ":5}") |
| 34 | + private int cacheSeconds; |
| 35 | + |
| 36 | + @Value("${" + Settings.I18N_FILE_CACHE_SECONDS + ":5}") |
| 37 | + private int fileCacheSeconds; |
| 38 | + |
| 39 | + @Bean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) |
| 40 | + public LocaleResolver localeResolver() { |
| 41 | + return new SessionLocaleResolver(); |
| 42 | + } |
| 43 | + |
| 44 | + @Bean |
| 45 | + public LocaleChangeInterceptor localeChangeInterceptor() { |
| 46 | + ParamsAwareLocaleChangeInterceptor localeChangeInterceptor = new ParamsAwareLocaleChangeInterceptor(); |
| 47 | + localeChangeInterceptor.setParamName("lang"); |
| 48 | + return localeChangeInterceptor; |
| 49 | + } |
| 50 | + |
| 51 | + @Bean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME) |
| 52 | + public MessageSource messageSource(GrailsApplication grailsApplication, GrailsPluginManager pluginManager) { |
| 53 | + PluginAwareResourceBundleMessageSource messageSource = new PluginAwareResourceBundleMessageSource(grailsApplication, pluginManager); |
| 54 | + messageSource.setDefaultEncoding(encoding); |
| 55 | + messageSource.setFallbackToSystemLocale(false); |
| 56 | + if (Environment.getCurrent().isReloadEnabled() || gspEnableReload) { |
| 57 | + messageSource.setCacheSeconds(cacheSeconds); |
| 58 | + messageSource.setFileCacheSeconds(fileCacheSeconds); |
| 59 | + } |
| 60 | + return messageSource; |
| 61 | + } |
| 62 | +} |
0 commit comments