добавил пример локализации
This commit is contained in:
3
http/examples.http
Normal file
3
http/examples.http
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
### Запрос эндпойнта hello
|
||||||
|
GET http://localhost:8080/hello?name=Петька
|
||||||
|
Language: en
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package dev.struchkov.example.spring.i18n;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocaleResolver localeResolver() {
|
||||||
|
return new UserLocaleResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package dev.struchkov.example.spring.i18n;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class HelloController {
|
||||||
|
|
||||||
|
private final MessageSource messageSource;
|
||||||
|
private final LocaleResolver localeResolver;
|
||||||
|
|
||||||
|
@GetMapping("/hello")
|
||||||
|
public String hello(@RequestParam("name") String name, HttpServletRequest request) {
|
||||||
|
return messageSource.getMessage("welcome", new Object[]{name}, localeResolver.resolveLocale(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package dev.struchkov.example.springbooti18n;
|
package dev.struchkov.example.spring.i18n;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package dev.struchkov.example.spring.i18n;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class UserLocaleResolver implements LocaleResolver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Locale resolveLocale(HttpServletRequest request) {
|
||||||
|
final String lang = request.getHeader("Language");
|
||||||
|
return Locale.forLanguageTag(lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
|
||||||
|
// Not needed for this example
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
|
|
||||||
|
|||||||
1
src/main/resources/messages_en.properties
Normal file
1
src/main/resources/messages_en.properties
Normal file
@@ -0,0 +1 @@
|
|||||||
|
welcome=Hello, {0}!
|
||||||
1
src/main/resources/messages_ru.properties
Normal file
1
src/main/resources/messages_ru.properties
Normal file
@@ -0,0 +1 @@
|
|||||||
|
welcome=??????, {0}!
|
||||||
Reference in New Issue
Block a user