9 Eylül 2022 Cuma

SpringMVC LocaleResolver Arayüzü

Giriş
Açıklaması şöyle
Another way to determine which locale is currently being used is to use LocaleResolver. This interface has different implementations that determine the current locale based on the session, cookies, the Accept-Language header, or a fixed value.
FixedLocaleResolver Sınıfı
Açıklaması şöyle
This implementation is used mostly for debugging purposes and you will always have a single fixed language set up in the project application properties file
Örnek
Şöyle yaparız
@Bean
public LocaleResolver localeResolver() {
   FixedLocaleResolver lr = new FixedLocaleResolver();
   lr.setDefaultLocale(Locale.US);
   return lr;
}
AcceptHeaderLocaleResolver Sınıfı
Açıklaması şöyle
For web applications, one of the options is to use the “accept-language” HTTP header on the request. This implementation will take the value from the HTTP status header and apply it.
Örnek
Şöyle yaparız
@Bean
public LocaleResolver localeResolver() {
   AcceptHeaderLocaleResolver lr = new AcceptHeaderLocaleResolver();
   lr.setDefaultLocale(Locale.US);
   return lr;
}
SessionLocaleResolver Sınıfı
Açıklaması şöyle
Another approach for web a1pplications is to use Session to get the local for the user visiting the application. In this implementation, the value will store inside the session and will be available during the session’s lifetime.
Açıklaması şöyleLocaleChangeInterceptor yazısına bakabilirsiniz
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
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver lr = new SessionLocaleResolver();
    lr.setDefaultLocale(Locale.US);
    return lr;
}
CookieLocaleResolver Sınıfı
CookieLocaleResolver yazısına taşıdım


Hiç yorum yok:

Yorum Gönder