i18n

package az.etibarli.i18n.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import java.util.Arrays;
import java.util.Locale;

@Configuration
public class I18nConfig {

private static final Locale LOCALE_AZ = Locale.of("az");
private static final Locale LOCALE_EN = Locale.of("en");
private static final Locale LOCALE_RU = Locale.of("ru");

@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setBasename("i18n/messages");
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setUseCodeAsDefaultMessage(true);
return resourceBundleMessageSource;
}

@Bean
public LocaleResolver localeResolver() {
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(LOCALE_RU);
localeResolver.setSupportedLocales(Arrays.asList(LOCALE_AZ, LOCALE_EN, LOCALE_RU));
return localeResolver;
}

} 


package az.etibarli.i18n.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class MessageSourceUtil {

private final MessageSource messageSource;

public String getMessage(String key, Object... args) {
final var locale = LocaleContextHolder.getLocale();
try {
return messageSource.getMessage(key, args, locale);
} catch (NoSuchMessageException ex) {
log.warn("Could not find message for key '{}' and locale '{}'", key, locale, ex);
}
return key;
}

}


package az.etibarli.i18n.exception;

import static az.etibarli.i18n.config.ErrorMessageAttribute.ALREADY_EXISTS;

public class NotFoundException extends RuntimeException {

public NotFoundException() {
super(ALREADY_EXISTS);
}

}


package az.etibarli.i18n.exception;

import az.etibarli.i18n.config.MessageSourceUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {

private final MessageSourceUtil messageSourceUtil;

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<Object> handleNotFoundException(NotFoundException ex) {
String message = messageSourceUtil.getMessage(ex.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(message);
}

}






































Комментарии

Популярные сообщения из этого блога

IoC:ApplicationContext, BeanFactory. Bean

Lesson1: JDK, JVM, JRE

Lesson_2: Operations in Java