Giriş
Şu satırı dahil ederiz
import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
Açıklaması şöyle
Note: For SessionLocaleResolver and CookieLocaleResolver we need to have an interceptor to get the parameter sent from the user for the preferred language and store it in session or cookie. Thanks to spring this has been done already don and you can use LocaleChangeInterceptor and just pass the name of the property to set the locale.
Örnek
Şöyle yaparız.
@Beanpublic LocaleResolver localeResolver() {return new SessionLocaleResolver();}@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() {return new LocaleChangeInterceptor();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor());}
Açıklaması şöyle
The LocaleChangeInterceptor looks for a request parameter name locale by default. If you are not interested to use that parameter, you can override it by giving your own parameter name.
Böylece kullanıcı farklı locale görmek isterse şöyle istekler gönderir
http://localhost:8181 http://localhost:8181/locale=kn
setParamName metodu
Örnek
Şöyle yaparız
@Bean public WebMvcConfigurer configurer(){ return new WebMvcConfigurerAdapter() { @Override public void addInterceptors (InterceptorRegistry registry) { LocaleChangeInterceptor l = new LocaleChangeInterceptor(); l.setParamName("localeParmName"); registry.addInterceptor(l); } }; }
Örnek
Şöyle yaparız.
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
Hiç yorum yok:
Yorum Gönder