Featured image of post SpringBoot2でメッセージプロパティを呼び出す設定【多言語化】

SpringBoot2でメッセージプロパティを呼び出す設定【多言語化】

SpringBoot2で独自のmessage.propertiesをロジックから読み込むための設定。読み込み用の設定クラスを用意して呼び出すまでと、ファイル名に言語コードを付けてLocaleごとに出し分ける多言語化まで♪

1074文字

独自プロパティファイルをソースから読み込みたい

SpringBootではエラーメッセージなどをプロパティファイルなどに外出しして管理することが可能になります。

呼び出すためには専用の設定を行う必要があるため、今回はその方法についてご紹介していきます。

手順

message.propertiesの用意

まずは読み込ませたいプロパティファイルを用意しましょう。

src/main/resources/custom/message.properties
hoge=fuga

メッセージファイルの読み込み設定

次に、上記で作成したプロパティファイルを読み取れるようにするための設定クラスを用意しましょう。

[adsense-responsive]

MessageSourceBean
package tech.blogenist.service.account.api.infrastructure.message;

import java.nio.charset.StandardCharsets;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ResourceBundleMessageSource;

@Configuration
public class MessageSourceBean
{
    private String[] BASE_NAMES =
    {
            "custom/message"
    };

    @Bean
    public MessageSource messageSource()
    {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames( BASE_NAMES );
        messageSource.setDefaultEncoding( StandardCharsets.UTF_8.name() );
        return messageSource;
    }

    @Bean
    public MessageSourceAccessor messageSourceAccessor()
    {
        return new MessageSourceAccessor( messageSource() );
    }

}

確認

では、実際に以下のように読み出した値を標準出力に出してみましょう。

[adsense-responsive]

サンプルコード
    @Autowired
    private MessageSource        messageSource;// 準備

   ...

        String value = messageSource
                .getMessage(
                        "hoge", //Keyを指定
                        null, // とりあえずnullで良い
                        Locale.getDefault() );// Localもとりあえずはデフォルトで良い

        System.out.println( value ); // fuga

では、これで実際にリクエストを投げてみましょう。

標準出力
2019-04-27 10:42:02.075  INFO 4380 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-27 10:42:02.076  INFO 4380 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-04-27 10:42:02.096  INFO 4380 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 20 ms
fuga

正しく取得出来ましたね♪

他言語化対応

プロパティファイル名に{ファイル名}_{言語コード}とすることでLocaleに合わせたファイルを自動で読み込んでくれるようになります。

以下のようなファイルを用意したとしましょう。

tree
api/src/main/resources/custom
├── message.properties
├── message.properties
└── message.properties

その上で、メッセージを取得する際の第三引数にLocaleを指定することで対応したプロパティファイルから値を取ってきてくれます。

サンプルコード
...
        String jaValue = messageSource
                .getMessage(
                        "hoge",
                        null,
                        Locale.JAPANESE );
        System.out.println( jaValue );// ふが

        String enValue = messageSource
                .getMessage(
                        "hoge",
                        null,
                        Locale.ENGLISH );
        System.out.println( enValue );//fuga

        String zhValue = messageSource
                .getMessage(
                        "hoge",
                        null,
                        Locale.CHINA );// ふが
        System.out.println( zhValue );

上記の例で、中国語用のファイルを用意してないのに日本語用のプロパティが読まれるのは、おそらくシステムのデフォルト言語が日本語になっているため、存在しない場合はそちらが適用されるからだと思います。

なので、_jaのプロパティファイルを削除すると、無印のデフォルトのプロパティが呼び出されました。

オススメの商品

現場至上主義 Spring Boot2 徹底活用

日清 カップヌードル 77g×20個

夜食のお供に。

終わりに

以上のように、SpringBootで簡単に独自エラーメッセージを読み込むことが出来ました。

エラーメッセージなどもプロパティファイルに出して管理することで、保守や他言語化なども可能になるのでぜひ試してみてください♪