0

プレゼンテーション層で JSON を使用して Spring MVC を使用して Java Web アプリケーションを開発しています。ハードコーディングの代わりに JSP で UI ラベルを表示するために、プロパティ ファイルから取得し、JSP 表現ではspring:messageタグを使用しています。これを実現するためにResourceBundleMessageSourceを使用しています。これは正常に機能しています。

ただし、アプリケーションはさまざまな地域からアクセスされ、地域に基づいて、使用する必要がある UI ラベル用のさまざまなプロパティ ファイルがあります。すべてのプロパティ ファイルのキーは同じですが、値が異なります。

たとえば、ユーザーがアプリケーションにアクセスする場所に「Region1」と「Region2」という 2 つの reqions があるとします。message_region1.properties と message_region2.properties などの 2 つのプロパティ ファイルがあります。ユーザーが Region1 からログインするときは message_Region1.properties を選択する必要があり、ユーザーが「Region2」からログインする場合は message_Region2.properties を選択して UI ラベルを表示する必要があります。両方のプロパティのすべてのキーが同じであるため、jsp ファイルに変更はありません。

それを達成するためのアドバイスはありますか?

4

1 に答える 1

0

Simple:) Create your message_region1.properties and message_region2.properties files. And Spring picks up the appropriate property file depending on the client's locale (using Default LocaleResolver). The property files generally are in the format filename_<language>_<country>.properties

And if you want to tell the Spring, how the locale should be resolved, use one of the 4 approaches given in spring documentation

EDIT:

To dynamically change the property file, use one of the below ways; I don't know if they are better than the 4 approaches (mentioned in above spec).

  1. Just before returning the view, change the locale to the "unique code" of the logged in user, as below.

    RequestContextUtils.getLocale(request).setDefault(new Locale("your code"));
    
  2. Change your message source in spring config to ReloadableResourceBundleMessageSource

    <bean id="messageSource"
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    

    And reset properties file name in controller, before returning the view, as below:

    messageSource.setBasename("classpath:messages" + your code);
    
于 2012-11-07T08:31:39.867 に答える