技術共有

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 ファイルを使用して、さまざまな言語でテキスト情報を定義できます。通常、messages.properties (デフォルト ファイル)、messages_en.properties (英語)、messages_zh_CN.properties (簡体字中国語) など、言語ごとに対応するリソース ファイルを作成します。

サンプル リソース ファイルmessages.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の編集者が制作したものなので、品質の高いものであることは間違いありません。転載する場合は出典を明記してください。