2013-08-09 7 views
5

私は、Java + Springを国際的なWebサイトに使用しています。Java/Springでは、欠落している変換値を正常に処理する方法はありますか?

2言語はZHとEN(中国語と英語)です。

messages.properties(英語のキーと値のペアの場合)とmessages_zh.propertiesの2つのファイルがあります。

サイトは#springMessageタグを使用して英語でコード化されています。私はPoeditor.comを使って、各英語のフレーズに中国語の値を翻訳者に提供させる。したがって、英語のmessages.propertiesファイルは常に完了していますが、messages_zh.propertiesファイルには常にキーが含まれていますが、messages_zh.propertiesファイルにはブランクが含まれることがあります。後で。

中国の価値がない場合は、私のウェブサイト上で同等の英語の値を表示するために私のシステムが必要です。

中国の価値が利用できないときはいつも英語を使うためにSpringに "フォールバック"するように教えてもらえますか?私はこれを値ごとに行う必要があります。

現在、中国の価値がない場合は、自分のサイトに空白のボタンのラベルが表示されます。中国語が空白のときはいつも、英語(デフォルト言語)を使用したいと思います。

答えて

4

この目的のために独自のカスタムメッセージソースを作成することができます。

何かのように:

public class SpecialMessageSource extends ReloadableResourceBundleMessageSource { 

     @Override 
     protected MessageFormat resolveCode(String code, Locale locale) { 
     MessageFormat result = super.resolveCode(code, locale); 
     if (result.getPattern().isEmpty() && locale == Locale.CHINESE) { 
      return super.resolveCode(code, Locale.ENGLISH); 
     } 
     return result; 
     } 

     @Override 
     protected String resolveCodeWithoutArguments(String code, Locale locale) { 
     String result= super.resolveCodeWithoutArguments(code, locale); 
     if ((result == null || result.isEmpty()) && locale == Locale.CHINESE) { 
      return super.resolveCodeWithoutArguments(code, Locale.ENGLISH); 
     } 
     return result; 
     } 
    } 

<bean id="messageSource" class="SpecialMessageSource"> 
..... 
</bean> 

として春のXMLでこのmessageSource Beanを設定すると、以下の方法のMessageSource'sいずれかを呼び出すことになるラベルを解決し得るために

String getMessage(String code, Object[] args, Locale locale); 
String getMessage(String code, Object[] args, String defaultMessage, Locale locale); 

resolveCode()が呼び出されます。 Rメッセージラベルは、引数を持って、あなたは
invalid.number= {0} is Invalid
以下のようにargsパラメータを経由して、これらの引数を渡すと、あなたはmessageSource.getMessage("INVALID_NUMBER", new Object[]{2d}, locale)

resolveCodeWithoutArguments()を呼び出すあなたのメッセージラベルは、引数を持っていない、あなたがnull
validation.success = Validation Successとしてargsパラメータを渡すときに呼び出されます
あなたが呼び出すmessageSource.getMessage("INVALID_NUMBER", null, locale)

+0

downvoteの理由は? – harrybvp

+0

誰があなたを落胆させたのか分かりませんが、私は正しい軌道に乗っているので、あなたにアップアップして答えのクレジットを与えます。私のコードが私が望むすべてのことをしているかどうかはわかりません。たぶんあなたは問題を見つけることができます。 – Ryan

+0

@Ryan私は自分のコードを更新し、あなたの望むように説明を追加しました – harrybvp

1

@ harrybvpの答えが正しいトラックに私を得ました。これは私のために働くように見えるコードである(そして、あなたが「スーパー」と呼ぶ方法を模擬する場合、ユニットテスト可能です):

public class GracefulMessageSource extends org.springframework.context.support.ReloadableResourceBundleMessageSource { 

     final static private Locale defaultLocale = Locale.ENGLISH; 

     @Override protected MessageFormat resolveCode(final String code, final Locale locale) { 
      MessageFormat result = superResolveCode(code, locale); 

      if (result.toPattern().isEmpty() && !locale.equals(this.defaultLocale)) { 
       result = superResolveCode(code, this.defaultLocale); 
      } 

      return result; 
     } 

     @Override protected String resolveCodeWithoutArguments(final String code, final Locale locale) { 
      String result = superResolveCodeWithoutArguments(code, locale); 

      if (result.isEmpty() && !locale.equals(this.defaultLocale)) { 
       result = superResolveCodeWithoutArguments(code, this.defaultLocale); 
      } 

      return result; 
     } 

     protected MessageFormat superResolveCode(final String code, final Locale locale) { 
      return super.resolveCode(code, locale); 

     } 

     protected String superResolveCodeWithoutArguments(final String code, final Locale locale) { 
      return super.resolveCodeWithoutArguments(code, locale); 

     } 

    } 

と私webmvc-configに

。XML:私のベロシティ・ビュー・テンプレートに続いて

<bean id="messageSource" class="com.mycode.fe.web.GracefulMessageSource"> 
    <property name="basename"> 
     <value>${content.path.config}/WEB-INF/messages</value> 
    </property> 
    <property name="defaultEncoding" value="UTF-8" /> 
    <property name="cacheSeconds" value="2"/> 
    <property name="fallbackToSystemLocale" value="true"/> 
</bean> 

引数なしで通常のキーの場合:#springMessage('menu_item1')

引数を受け付けキーの場合:#springMessageText('male_to_female_ratio' [3, 2])

その後のメッセージインチプロパティ(英語の翻訳): male_to_female_ratio = There is a {0}:{1} male-to-female ratio.

関連する問題