본문 바로가기
카테고리 없음

LocaleResolver

코동이 2020. 6. 25.

Locale : 사용자의 언어, 국가(뿐 아니라 사용자 인터페이스에서 사용자가 선호하는 사항을 지정한 매개변수의 모임)

 

This interface allows for implementations based on requestsessioncookies, etc. 
The default implementation is AcceptHeaderLocaleResolver, simply using the request's locale provided by the respective HTTP header.

LocaleResolver로 다국어를 설정하는 방법은 AcceptHeaderLocaleResolver(default), SessionLocaleResolver, CookieLocaleResolver, FixedLocaleResolver 4가지 방법이 있다.

 

LocaleResolver

@Bean
public LocaleResolver localeResolver(){
	SessionLocaleResolver localeResolver = new SessionLocaleResolver();
	localeResolver.setDefaultLocale(Locale.US);
	return localeResolver;
}

가장 먼저, LocaleResolver를 @Bean으로 등록한다. default는 Locale.US이다.

 

ResourceBundleMessageSource

@Bean
public ResourceBundleMessageSource messageSource(){
	ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
	messageSource.setBasename("messages");
	return messageSource;
}

 

The basenames follow ResourceBundle conventions: essentially, a fully-qualified classpath location. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root. Note that the JDK's standard ResourceBundle treats dots as package separators: This means that "test.theme" is effectively equivalent to "test/theme".

basename이 ResourceBundle 규칙을 따르며, 아무런 패키지 명시가 없으면 classpath의 root로 지정이 된다.

 

On the classpath, bundle resources will be read with the locally configured encoding
: by default, ISO-8859-1; consider switching this to UTF-8, or to null for the platform default encoding. On the JDK 9+ module path where locally provided ResourceBundle.Control handles are not supported, this MessageSource always falls back to ResourceBundle.getBundle(java.lang.String) retrieval with the platform default encoding: UTF-8 with a ISO-8859-1 fallback on JDK 9+ (configurable through the "java.util.PropertyResourceBundle.encoding" system property).       -

reference문서에 따르면, classpath에서, bundle resources들은 ISO-8859-1이 default 설정이므로 UTF-8은 따로 설정해야 한다.

 

In Java SE 9, properties files are loaded in UTF-8 encoding. In previous releases, ISO-8859-1 encoding was used for loading property resource bundles. UTF-8 is a much more convenient way to represent non-Latin characters.

JDK9버전에서부터는 properties files들이 ISO-8859-1이 아닌 UTF-8이 default로 설정된다.

 

Locations of static resources. Defaults to classpath:[/META-INF/resources/, /resources/, /static/, /public/].

스프링은 web properties는 classpath는 자동으로 여러위치를 탐색하며 maven에서 /resources를 탐색한다.

 

The class path is the path that the Java Runtime Environment (JRE) searches for classes and other resource files.

classpath란 JRE가 class들과 resource 파일들을 찾는 위치이다.

LocaleChangeInterceptor

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName("lang");
    return lci;
}

parameter 중에 "lang"이 발견되면 해당 값에 따라 locale이 변경되도록 Interceptor를 만든다.

Interceptor를 사용하려면 Application의 registry에 등록해야 하는데,

WebMvcConfigurer를 implements 하고 addInterceptor()를 Override 해야 한다.

 

<a href="customer/edit.do?lang=ko">고객 등록</a></br>
<a href="customer/edit.do?lang=en">customer enrollment</a></br>

Interceptor에서 lci.setParamName("lang")을 설정했으므로, Web 상에서, lang에 관한 URI가 서버에 요청 되었을 때, Interceptor가 확인하고 ko이면 한국어 로케일을 지정하고 en이면 영어 로케일을 지정한다.

 

@Configuration
public class SpringInternationalizationConfig implements WebMvcConfigurer {

...

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

...

 

By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder.

The file for the default locale will have the name messages.properties, and files for each locale will be named messages_XX.properties, where XX is the locale code.

The keys for the values that will be localized have to be the same in every file, with values appropriate to the language they correspond to.

다국어는 src/main/resouces 폴더에 정의하는게 default이며, 파일명의 default는 messages.properties이다.

 

다국어의 다양한 종류는 message_XX.properties를 사용한다. XX부분에 국가코드를 사용한다.

프랑스어를 사용하고 싶으면 messages_fr.properties

네덜란드어를 사용하고 싶으면 message_nl.properties

반응형