기술나눔

Spring Boot의 국제화 및 지역화 문제 해결

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Spring Boot의 국제화 및 지역화 문제 해결

안녕하세요 여러분, 저는 Weizhuan Taoke System 3.0의 편집자이자 겨울에는 긴 내복을 입지 않지만 추운 날씨에는 시원해야 하는 프로그래머이기도 합니다!

국제화 및 지역화 개요

국제화(줄여서 i18n)와 지역화(줄여서 l10n)는 글로벌 사용자를 위한 애플리케이션을 개발할 때 매우 중요한 고려 사항입니다. 국제화는 다양한 언어와 문화적 습관의 요구에 적응할 수 있도록 소프트웨어 제품을 설계하고 구현하는 것을 의미하며, 현지화는 보다 사용자 친화적인 경험을 제공하기 위해 특정 지역이나 언어 습관에 따른 국제화 및 적응을 기반으로 합니다.

1. Spring Boot의 국제화 구성

Spring Boot에서 국제화 및 지역화는 주로 Spring에서 제공하는 리소스 파일 및 관련 지원을 통해 수행됩니다. 먼저 이러한 기능을 구성하고 사용하는 방법을 살펴보겠습니다.

1.1. 리소스 파일 구성

Spring Boot 프로젝트에서는 속성 파일(*.properties) 또는 YAML 파일을 사용하여 다양한 언어로 텍스트 정보를 정의할 수 있습니다. 일반적으로 message.properties(기본 파일), message_en.properties(영어), message_zh_CN.properties(중국어 간체) 등과 같이 각 언어에 해당하는 리소스 파일을 생성합니다.

샘플 리소스 파일 message.properties:

greeting.message=Welcome to our application!
button.submit=Submit
label.username=Username
label.password=Password
  • 1
  • 2
  • 3
  • 4
1.2. Spring Boot에서 국제 리소스 사용

Spring Boot에서 통과MessageSource그리고LocaleResolver다양한 언어로 리소스 파일을 로드 및 구문 분석하고 사용자의 로케일에 따라 조정합니다.

package cn.juwatech.localization;

import cn.juwatech.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import java.util.Locale;

@SpringBootApplication
public class Application {

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

    @Bean
    public LocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
        resolver.setDefaultLocale(Locale.US); // 设置默认Locale
        return resolver;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
1.3. 국제 자원 활용

컨트롤러 또는 서비스에서 다음을 전달합니다.MessageSource특정 로케일에서 텍스트 정보를 얻으려면.

package cn.juwatech.localization;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @Autowired
    private MessageSource messageSource;

    @GetMapping("/greeting")
    public String greeting() {
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage("greeting.message", null, locale);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2. 국제화 및 지역화 모범 사례

기본 구성 및 사용 외에도 다음과 같은 방법으로 국제화 및 지역화 효과를 더욱 최적화할 수 있습니다.

  • 더 많은 언어 지원: 사용자 요구에 따라 더 많은 언어로 리소스 파일을 추가합니다.
  • 동적으로 언어 전환: 요청 매개변수나 사용자 설정 등을 통해 사용자가 인터페이스에서 언어를 전환할 수 있도록 지원합니다.
  • 현지화된 날짜, 시간 및 통화: 날짜, 시간 및 통화를 현지화하려면 Spring에서 제공하는 서식 도구를 사용하세요.
  • 국제화 예외 정보: 사용자 경험을 향상시키기 위해 애플리케이션 예외 정보를 국제화합니다.

결론적으로

이 기사에서는 Spring Boot 프로젝트의 국제화 및 지역화 문제를 해결하는 방법을 소개하고, Spring에서 제공하는 국제화 지원과 모범 사례를 사용하여 리소스 파일을 구성하여 다중 언어 및 지역별 적응을 달성하는 방법을 소개합니다. 이러한 기술과 방법은 개발자가 글로벌 사용자를 위한 친숙하고 전문적인 애플리케이션을 구축하는 데 도움이 됩니다.

소액적립 타오케시스템 3.0 편집자가 제작한 고급상품이므로 재인쇄시 출처를 꼭 밝혀주세요!