Last active
September 1, 2022 18:21
-
-
Save anataliocs/715cb36c3604a467d38d to your computer and use it in GitHub Desktop.
Validation messages in messages.properties file for i18n internationalization by locale in spring boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Application { | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
} | |
@RequestMapping("/") | |
@ResponseBody | |
String home() { | |
return " | |
} | |
@Bean | |
public LocaleResolver localeResolver() { | |
SessionLocaleResolver slr = new SessionLocaleResolver(); | |
slr.setDefaultLocale(Locale.US); | |
return slr; | |
} | |
@Bean | |
public ReloadableResourceBundleMessageSource messageSource() { | |
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); | |
messageSource.setBasename("classpath:locale/messages"); | |
messageSource.setCacheSeconds(3600); //refresh cache once per hour | |
return messageSource; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MessageByLocaleService { | |
public String getMessage(String id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class MessageByLocaleServiceImpl implements MessageByLocaleService { | |
@Autowired | |
private MessageSource messageSource; | |
@Override | |
public String getMessage(String id) { | |
Locale locale = LocaleContextHolder.getLocale(); | |
return messageSource.getMessage(id,null,locale); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user.login.invalid.id=0 | |
user.login.invalid=Your user name or password was invalid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/user") | |
public class UserControler { | |
@Autowired | |
UserService userService; | |
@Autowired | |
MessageByLocaleService messageByLocaleService; | |
@RequestMapping(value = "/userlogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) | |
public ResponseEntity<User> login( | |
@RequestBody UserCred userCred) | |
throws UnsupportedEncodingException { | |
String invalidLogin = messageByLocaleService.getMessage("user.login.invalid"); | |
} | |
} |
Thank you brow!! You saved my life LOL
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
at what location did you place messages_en_us-properties?
and what is significance of "classpath:locale/messages"?