configuration for reading static.messages properties files in project

Assume that we have Resource Bundle 'message' and Resource Bundle 'validation' folders (there are properties files inside them), under static.messages folder , then we can configure for read this properties from resource folders like that:



 @Bean

    public ResourceBundleMessageSource messageSource() {

        ResourceBundleMessageSource source = new ResourceBundleMessageSource();

        source.setBasenames("static/messages/message", "static/messages/validation");

        source.setDefaultLocale(new Locale("az"));

        source.setUseCodeAsDefaultMessage(true);

        source.setDefaultEncoding("UTF-8");

        return source;

    }


    @Bean

    public LocalValidatorFactoryBean getValidator() {

        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();

        bean.setValidationMessageSource(messageSource());

        return bean;

    }




2 ci implementasiya

LocalConfig classi yaradiriq:

@Configuration
public class LocaleConfig {

@Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("i18n/messages");

return messageSource; }

@Bean
public LocaleResolver localeResolver() {
var localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(AZ);
localeResolver.setSupportedLocales(Arrays.asList(AZ, EN, RU));
return localeResolver;
}
}

Daha sonra lazim olan yerde 
@Autowired
private MessageSource messageSource;
messageSource.getMessage("account.name", null, Locale.ENGLISH);
yazib account.name keyine gore mesaji istenilen dilde almaq olar
mesaj paketinin structuru ashagidaki kimi olmalidir

3 cu hal:
eger i18n in ichinde 2 mesaj paketi olarsa, ashagidaki kimi
O halda ferqli cur implementasiya lazim olacaq
@Configuration
public class LocaleConfig {

private static final Locale AZ = new Locale("az");
private static final Locale EN = new Locale("en");
private static final Locale RU = new Locale("ru");

@Bean
public LocaleResolver localeResolver() {
var localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(AZ);
localeResolver.setSupportedLocales(Arrays.asList(AZ, EN, RU));
return localeResolver;
}
}
Local config classi yaradiriq.
enum yaradib error codea gore mesajin keyini map etmek olar
public final class Common {

public static final String I18N_ERROR = "i18n/error/messages";
public static final String I18N_STATEMENT = "i18n/statement/messages";
}
@AllArgsConstructor
public enum ErrorCodes {
DATE_RANGE_ONE_MONTH_EXCEEDED(26, "dateRange1MonthExceeded");
private final int code;
private final String message;

public int code() {
return this.code;
}
public String message() {
return ResourceBundle.getBundle(I18N_ERROR, LocaleContextHolder.getLocale()).getString(this.message);
}
}
error mesajlar uchun tetbiq olunan enum strcutrur ve mesajin keye gore alinmasini eyni 
shekilde statement mesajlari uchunde tetbiq etmek olar
yeni enum yaradib error code- key mappingi etmek lazim olacaq
istenilen halda enum yaradib code ve message key mappingi etmek daha semerelidir
ve lazim olarsa mesaji ele enumun ichinde resolve etmek ve ya mesaj keyi sadece
@Override
public String message() {
return this.message;
}
bu sheiklde enumdan goturub bashqa yerde resolve etmek olar.
Yuxaridaki enumun istifadesi: gorunduyu kimi errorhandler classinda
bash veren xetaya uygun enumu chagirib, response objnin contrcutoruna
error kodu ve mesaji otururuk. Mesaj enumun ichinde getMessage() methodunda
resolve olunur.
@ExceptionHandler(OneMonthRangeException.class)
public RestResponseDto<?> handleOneMonthRangeException(OneMonthRangeException ex) {
var errorCode = ErrorCodes.DATE_RANGE_ONE_MONTH_EXCEEDED;
return RestResponseDto.of(errorCode.code(), errorCode.message());
}

Comments

Popular posts from this blog

Installation instructions for some programs on linux ubuntu

Hibernate and Application Performance

timezone ile bagli Jackson deserializableda problem