Skip to content

Commit ff94819

Browse files
committed
define base structure for i8n feature. you can define message code and define messages for other lang and send it as response
1 parent 417c2cd commit ff94819

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ir.bigz.springbootreal.configuration;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.MessageSource;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.support.ResourceBundleMessageSource;
8+
import org.springframework.web.servlet.LocaleResolver;
9+
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
10+
11+
import java.util.Locale;
12+
13+
@Configuration
14+
public class LocaleConfig {
15+
16+
@Value("${spring.messages.basename}")
17+
private String basename;
18+
19+
@Value("${spring.messages.encoding}")
20+
private String encoding;
21+
22+
@Bean
23+
public LocaleResolver localeResolver() {
24+
AcceptHeaderLocaleResolver localResolver = new AcceptHeaderLocaleResolver();
25+
localResolver.setDefaultLocale(Locale.US);
26+
return localResolver;
27+
}
28+
29+
@Bean
30+
public MessageSource messageSource() {
31+
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
32+
messageSource.setBasename(basename);
33+
messageSource.setDefaultEncoding(encoding);
34+
return messageSource;
35+
}
36+
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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.context.MessageSource;
78
import org.springframework.data.domain.Page;
89
import org.springframework.data.domain.Sort;
910
import org.springframework.http.HttpStatus;
@@ -13,19 +14,30 @@
1314

1415
import javax.validation.Valid;
1516
import java.util.List;
17+
import java.util.Locale;
1618

1719
@RestController
1820
@CrossOrigin
1921
@RequestMapping("/api")
2022
public class SampleController extends AbstractController{
2123

22-
final
23-
UserService userService;
24+
final UserService userService;
2425

25-
public SampleController(UserService userService) {
26+
final MessageSource source;
27+
28+
public SampleController(UserService userService, MessageSource source) {
2629
this.userService = userService;
30+
this.source = source;
31+
}
32+
33+
@GetMapping("/v1/welcome")
34+
public ResponseEntity<?> getLocaleMessage(
35+
@RequestHeader(name = "Accept-Language", required = false) final Locale locale,
36+
@RequestParam(name = "username", defaultValue = "Albert Xin", required = false) final String username) {
37+
return ResponseEntity.ok(source.getMessage("welcome.message", new Object[]{username}, locale));
2738
}
2839

40+
2941
@GetMapping(path = "/v1/user/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
3042
@ResponseStatus(HttpStatus.OK)
3143
public ResponseEntity<?> getUserById(@PathVariable("id") long id) {

src/main/resources/application-dev.properties

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ springdoc.use-fqn=true
3838

3939

4040
################### Application Jobs Schedule ##########################
41-
app.jobs.alertServiceJob=0 */1 * ? * *
41+
app.jobs.alertServiceJob=0 */1 * ? * *
42+
43+
################### Spring Configuration ##########################
44+
spring.main.allow-bean-definition-overriding=true
45+
46+
################### i18n Configuration ##########################
47+
spring.messages.basename=lang/messages
48+
spring.messages.encoding=UTF-8
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
welcome.message=Greetings {0}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
welcome.message=Bonjour {0}

0 commit comments

Comments
 (0)