2021-08-08

This commit is contained in:
2021-08-08 20:15:27 +09:00
parent 2eef7786bd
commit 8ab9c4125b
26 changed files with 520 additions and 2 deletions

View File

@@ -0,0 +1,20 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,64 @@
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Locale;
/**
* 로케일 리졸버
* AcceptHeaderLocaleResolver를 기본 정책으로하되, GET 파라미터로 로캐일 변환이 가능하도록 한다.
* @author Elex
*/
@Slf4j
public class CustomLocaleResolver implements LocaleResolver {
/**
* 지원되는 로캐일 목록. 첫 번째 로캐일은 기본 로캐일로 사용된다.
*/
private static final List<Locale> supportedLocales = List
.of(Locale.ENGLISH, Locale.KOREAN);
private Locale locale = null;
private final AcceptHeaderLocaleResolver acceptHeaderLocaleResolver;
public CustomLocaleResolver() {
super();
acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
acceptHeaderLocaleResolver.setSupportedLocales(supportedLocales);
acceptHeaderLocaleResolver.setDefaultLocale(supportedLocales.get(0));
}
@Override
public @NotNull Locale resolveLocale(@NotNull HttpServletRequest request) {
if (null == locale) {
return acceptHeaderLocaleResolver.resolveLocale(request);
} else {
for (Locale loc : supportedLocales) {
if (locale.equals(loc)) return loc;
}
for (Locale loc : supportedLocales) {
if (locale.getLanguage().equals(loc.getLanguage())) return loc;
}
return supportedLocales.get(0);
}
}
/**
* 안터셉터에서 처리될껄?
* @param request
* @param response
* @param locale
*/
@Override
public void setLocale(@NotNull HttpServletRequest request, HttpServletResponse response,
Locale locale) {
this.locale = locale;
}
}

View File

@@ -0,0 +1,30 @@
package kr.pe.elex.examples;
import com.samskivert.mustache.Mustache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import java.util.Locale;
/**
* Mustache 템플릿에 국제화 문자열을 끼워넣기 위해 사용된다.
*/
@Slf4j
@ControllerAdvice
public class I18nAdvice {
@Autowired
private MessageSource messageSource;
@ModelAttribute("i18n")
public Mustache.Lambda i18n(Locale locale){
return (frag, out) -> {
String body = frag.execute();
String message = this.messageSource.getMessage(body, new String[]{}, locale);
log.debug("User Locale: {} -> {}", locale, message);
out.write(message);
};
}
}

View File

@@ -0,0 +1,35 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.HashMap;
@Slf4j
@Controller
public class MyController {
@GetMapping(path = {"/"})
public String index() throws Exception {
return "main";
}
}

View File

@@ -0,0 +1,54 @@
package kr.pe.elex.examples;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${spring.messages.basename:i18n}")
String messagesBasename = null;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
/**
* Locale resolver, 디폴트 로캐일을 지정한다.
*/
@Bean
public LocaleResolver localeResolver() {
/*
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
*/
return new CustomLocaleResolver();
}
/**
* Locale change interceptor, 요청 파라미터에 따른 로케일 변경
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource
= new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/" + messagesBasename);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}

View File

@@ -0,0 +1,8 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,9 @@
spring:
application:
name: My spring-boot project
mustache:
expose-request-attributes: true
messages:
basename: i18n
server:
port: 8080

View File

@@ -0,0 +1,10 @@
('-. ('-. ) (`-.
_( OO) _( OO) ( OO ).
(,------.,--. (,------.(_/. \_)-.
| .---'| |.-') | .---' \ `.' /
| | | | OO ) | | \ /\
(| '--. | |`-' |(| '--. \ \ |
| .--'(| '---.' | .--' .' \_)
| `---.| | | `---. / .'. \
`------'`------' `------''--' '--'
powered by ELEX

View File

@@ -0,0 +1 @@
hello.text = Hello

View File

@@ -0,0 +1 @@
hello.text = 안녕

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Spring-boot Examples
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<springProperty name="LOG_DIR" source="logging.path"
defaultValue="${user.home}/logs"/>
<property name="LOG_PATH" value="${LOG_DIR}/stephanie.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_PATH}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/sebastian_%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>60</maxHistory>
</rollingPolicy>
</appender>
<logger name="kr.pe.elex" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</logger>
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</root>
</configuration>

View File

@@ -0,0 +1,2 @@
<h1>i18n</h1>
<p>{{#i18n}}hello.text{{/i18n}}</p>