blog: i18n

This commit is contained in:
haerong22
2021-10-14 15:20:58 +09:00
parent 6dfd233beb
commit c1a4577adc
5 changed files with 28 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package com.example.i18n.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -19,9 +20,8 @@ public class I18nConfig implements WebMvcConfigurer {
public LocaleResolver localeResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(Locale.ENGLISH);
resolver.setDefaultLocale(Locale.getDefault());
resolver.setCookieName("lang");
return resolver;
}
@@ -37,6 +37,7 @@ public class I18nConfig implements WebMvcConfigurer {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(10 * 60);
return messageSource;
}

View File

@@ -1,2 +1,2 @@
language = english
welcome = Hello
welcome = Hello, {0}

View File

@@ -1,2 +1,2 @@
language = 한글
welcome = 안녕?
welcome = 안녕? {0}

View File

@@ -8,6 +8,7 @@
<h1 th:text="'i18n Test'"></h1>
<h4 th:text="#{language}"></h4>
<h4 th:text="#{welcome}"></h4>
<h4 th:text="#{welcome('kim')}"></h4>
</body>
</html>

View File

@@ -0,0 +1,21 @@
package com.example.i18n;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.MessageSource;
import java.util.Locale;
@SpringBootTest
class MessageSourceTest {
@Autowired
MessageSource messageSource;
@Test
void messageSourceTest() {
System.out.println(messageSource.getMessage("welcome", new String[]{"kim"}, Locale.ENGLISH));
System.out.println(messageSource.getMessage("welcome", new String[]{"kim"}, Locale.KOREAN));
}
}