Skip to content

Commit 5ac9e4b

Browse files
committed
add new method that resolve message from any path you define it and for use you must inject bean in every class you want
1 parent ff94819 commit 5ac9e4b

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/main/java/ir/bigz/springbootreal/configuration/LocaleConfig.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
import org.springframework.context.MessageSource;
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Primary;
8+
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
79
import org.springframework.context.support.ResourceBundleMessageSource;
810
import org.springframework.web.servlet.LocaleResolver;
911
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
1012

1113
import java.util.Locale;
14+
import java.util.Objects;
15+
16+
/**
17+
* you can define bean for message resource that it's read from specific path and inject bean in every class you want
18+
* or use default messageResource bean that read from spring config define in application.properties file
19+
*/
1220

1321
@Configuration
1422
public class LocaleConfig {
@@ -26,7 +34,21 @@ public LocaleResolver localeResolver() {
2634
return localResolver;
2735
}
2836

37+
@Bean("loadErrorMessageSource")
38+
public ReloadableResourceBundleMessageSource errorCodeSourceDesc(){
39+
return loadMessageSource("classpath:lang/error/code", 20, null);
40+
}
41+
42+
public ReloadableResourceBundleMessageSource loadMessageSource(String path, Integer timeToCache, String defaultEncoding){
43+
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
44+
messageSource.setBasename(path);
45+
messageSource.setDefaultEncoding(Objects.isNull(defaultEncoding) ? encoding: defaultEncoding);
46+
messageSource.setCacheSeconds(Objects.isNull(timeToCache) ? 10 : timeToCache);
47+
return messageSource;
48+
}
49+
2950
@Bean
51+
@Primary
3052
public MessageSource messageSource() {
3153
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
3254
messageSource.setBasename(basename);

src/main/java/ir/bigz/springbootreal/controller/SampleController.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import ir.bigz.springbootreal.service.UserService;
55
import ir.bigz.springbootreal.viewmodel.UserModel;
66
import ir.bigz.springbootreal.viewmodel.search.UserSearchDto;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
79
import org.springframework.context.MessageSource;
10+
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
811
import org.springframework.data.domain.Page;
912
import org.springframework.data.domain.Sort;
1013
import org.springframework.http.HttpStatus;
@@ -25,15 +28,25 @@ public class SampleController extends AbstractController{
2528

2629
final MessageSource source;
2730

31+
@Autowired
32+
@Qualifier("loadErrorMessageSource")
33+
ReloadableResourceBundleMessageSource loadMessageSource;
34+
2835
public SampleController(UserService userService, MessageSource source) {
2936
this.userService = userService;
3037
this.source = source;
3138
}
3239

40+
@GetMapping("/v1/geterror")
41+
public ResponseEntity<?> getErrorMessage(
42+
@RequestHeader(name = "Accept-Language", required = false) final Locale locale) {
43+
return ResponseEntity.ok(loadMessageSource.getMessage("Exception", new Object[0], locale));
44+
}
45+
3346
@GetMapping("/v1/welcome")
3447
public ResponseEntity<?> getLocaleMessage(
3548
@RequestHeader(name = "Accept-Language", required = false) final Locale locale,
36-
@RequestParam(name = "username", defaultValue = "Albert Xin", required = false) final String username) {
49+
@RequestParam(name = "username", defaultValue = "Java Geek", required = false) final String username) {
3750
return ResponseEntity.ok(source.getMessage("welcome.message", new Object[]{username}, locale));
3851
}
3952

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Exception=Internal server Exception
2+
CommunicationException=Systems intercommunication problem
3+
GeneralBusinessException=Internal business exception occurred
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Exception=Exception de serveur interne
2+
CommunicationException=Problème d'intercommunication des systèmes
3+
GeneralBusinessException=Une exception commerciale interne s'est produite

0 commit comments

Comments
 (0)