すべての言語がフォルダーで区切られているように、Spring の i18n ユーティリティを使用したいと考えています。このようなフォルダー構造を使用して、すべてを 1 つのフォルダーにまとめるよりも整理する予定です。
- i18n - ja
- メッセージのプロパティ
- アプリケーションのプロパティ
 
- フランス - メッセージのプロパティ
- アプリケーションのプロパティ
 
 
- ja
これは可能ですか?
すべての言語がフォルダーで区切られているように、Spring の i18n ユーティリティを使用したいと考えています。このようなフォルダー構造を使用して、すべてを 1 つのフォルダーにまとめるよりも整理する予定です。
i18n
フランス
これは可能ですか?
これは可能ですか?
はい、そうです。
私が最初に考えた簡単な解決策はbasenames、次のようなプロパティでメッセージ ソースをセットアップすることでした。
<bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="useCodeAsDefaultMessage" value="true" />
    <property name="basenames">
        <list>
            <value>i18n.en.messages</value>
            <value>i18n.en.application</value>
            <value>i18n.fr.messages</value>
            <value>i18n.fr.application</value>
        </list>
    </property>
</bean>
しかし、もう一度考えてみると、上記が機能しないことに気付きました。ResourceBundlebundles をインスタンス化する s 戦略に基づいて、バンドルの名前を指定すると、バンドル リストの最初の名前がバンドルを解決します (たとえば、looking formessages_fr.properties戦略は を探し、見つからない場合はデフォルトとしてi18n/en/messages_fr.properties解決されます)。i18n/en/messages.propertiesmessages_fr.properties
カスタム フォルダー構成に基づいてバンドルを検出するものが必要になります。 Spring が提供するデフォルトの実装の代わりに、独自のMessageSource実装を作成してアプリケーションで使用する必要があります。基本的な実装は次のようになります。
package pack.age;
import java.util.Locale;
import java.util.ResourceBundle;
import org.springframework.context.support.ResourceBundleMessageSource;
public class ByFolderResourceBundleMessageSource extends ResourceBundleMessageSource {
    private String rootFolder;
    @Override
    protected ResourceBundle getResourceBundle(String basename, Locale locale) {
        String langCode = locale.getLanguage().toLowerCase();
        String fullBaseName = this.rootFolder + "." + langCode + "." + basename;
        ResourceBundle bundle = super.getResourceBundle(fullBaseName, locale);
        if (bundle == null) {
            String defaultBaseName = this.rootFolder + ".Default." + basename;
            bundle = super.getResourceBundle(defaultBaseName, locale);
        }
        return bundle;
    }
    public void setRootFolder(String rootFolder) {
        this.rootFolder = rootFolder;
    }
}
次のような構成:
<bean id="messageSource" 
        class="pack.age.ByFolderResourceBundleMessageSource">
    <property name="useCodeAsDefaultMessage" value="true" />
    <property name="rootFolder" value="i18n" />
    <property name="basenames">
        <list>
            <value>messages</value>
            <value>application</value>
        </list>
    </property>
</bean>
そして、次のようなフォルダー設定を使用します。
i18n
  ├───Default
  │     ├─── application.properties
  │     └─── messages.properties
  │
  ├─── en
  │     ├─── application.properties
  │     └─── messages.properties
  │
  └─── fr
        ├─── application.properties
        └─── messages.properties