๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

[Spring] properties encoding ํ•˜๊ธฐ(๊ตญ์ œํ™” ํ•œ๊ธ€๊นจ์ง)

by dkswnkk 2022. 4. 27.

์„œ๋ก 

์Šคํ”„๋ง ํ•™์Šต ์ค‘ ๋‹ค๊ตญ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ๊ตญ์ œํ™”(Internationalization) ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ค๊ฐ€ ์ด์ƒํ•˜๊ฒŒ ํ•œ๊ธ€๋งŒ ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ Bean ๋“ฑ๋ก๋„ ํ•ด์ฃผ์—ˆ๊ณ , Controller, yml ์„ค์ •, message ๋ฒˆ๋“ค๊นŒ์ง€ ์™„๋ฒฝํ•˜๊ฒŒ ๊ตฌ์„ฑํ–ˆ๋Š”๋ฐ๋„ ์˜ค์ง ํ•œ๊ธ€ ๋ถ€๋ถ„๋งŒ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

main

@SpringBootApplication
public class ApiPracticeApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiPracticeApplication.class, args);
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.KOREA);
        return localeResolver;
    }
}

Controller

    @GetMapping("/hello-world-internationalized")
    public String helloWorldInternationalized(@RequestHeader(name = "Accept-Language", required = false) Locale locale) {
        log.info("Accept-Language : {}", locale);
        return messageSource.getMessage("greeting.message", null, locale);
    }

application.yml

spring:
  messages:
    basename: messages

message Bundle

 

// ํŒŒ์ผ ๊ฐ๊ฐ
greeting.message=์•ˆ๋…•ํ•˜์„ธ์š”
greeting.message=Hello
greeting.message=Bonjour

 

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” Intellij์˜ ๊ฐ„๋‹จํ•œ encoding ์„ค์ •์„ ํ†ตํ•ด ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์„ค์ • ์ „

mac ๊ธฐ์ค€์œผ๋กœ Preferences -> Editor -> File Encodings ๋ถ€๋ถ„์— ์ ‘๊ทผํ•˜์‹œ๋ฉด ์œ„์™€ ๊ฐ™์ด ์ฒดํฌ ๋ฐ•์Šค๊ฐ€ ํ’€๋ ค์žˆ๊ณ   ISO-8859-1๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์„ํ…๋ฐ ์ด๊ฒŒ ํ•œ๊ธ€์„ ์ง€์›ํ•˜์ง€ ์•Š์•„์„œ ํ•œ๊ธ€ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ์‹œ ํ•œ๊ธ€์ด ๊นจ์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด UTF-8๋กœ ๋ฐ”๊พธ๊ณ  ์ฒดํฌ๋ฐ•์Šค๋ฅผ ํ™œ์„ฑํ™” ์‹œ์ผœ์ค์‹œ๋‹ค.

์„ค์ • ํ›„

๊ทธ๋Ÿฌ๋ฉด ์ด์ œ properties ํŒŒ์ผ์˜ ์ €์žฅ์„ UTF-8๋กœ encodingํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋Œ“๊ธ€