добавил пример локализации
This commit is contained in:
parent
14050ac7a0
commit
d64a6aefd2
|
@ -0,0 +1,3 @@
|
|||
### Запрос эндпойнта hello
|
||||
GET http://localhost:8080/hello?name=Петька
|
||||
Language: en
|
|
@ -0,0 +1,19 @@
|
|||
package dev.struchkov.example.spring.i18n;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
@Bean
|
||||
public ReloadableResourceBundleMessageSource messageSource() {
|
||||
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
||||
messageSource.setBasename("classpath:messages");
|
||||
messageSource.setCacheSeconds(3600);
|
||||
messageSource.setDefaultEncoding("UTF-8");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package dev.struchkov.example.spring.i18n;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class HelloController {
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello(@RequestParam("name") String name, @RequestHeader("Language") String lang) {
|
||||
return messageSource.getMessage("welcome", new Object[]{name}, new Locale(lang));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package dev.struchkov.example.springbooti18n;
|
||||
package dev.struchkov.example.spring.i18n;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
@ -1 +0,0 @@
|
|||
|
|
@ -0,0 +1 @@
|
|||
welcome=Hello, {0}!
|
|
@ -0,0 +1 @@
|
|||
welcome=??????, {0}!
|
Loading…
Reference in New Issue