Silverlightのローカライズ

ローカリゼーションはかつてあなたの国際的な家にやって来ます。 どんなものを建てようと-巨大な超高層ビルやおじさんのトムの小屋-この家の住民の言語を話すことができなければなりません。



あなたのSilverlightホームがローカライズを必要とするならば、あなたは歓迎されます、私はいくらかのペンキとブラシを与えて、ポスターをあなた自身で描くようにします。



問題。



デスクトップアプリケーションの開発にリソースを使用することに問題はありません。ResXファイルを作成し、特定の言語のセカンダリ(サテライト)アセンブリを使用することは長い間可能です。 Silverlightはこれらの関数を継承しましたが、デスクトップアプリケーションとは異なり、ローカライズされたリソースの数を増やすとアセンブリのサイズが大きくなることを覚えておく必要があります。 そのため、ユーザーはダウンロードを待っており、ダウンロードした情報の一部はまったく必要ないことがわかりました。 一方、サーバーについては、ファイルが大きいほど、サーバーの負荷が大きくなると言っています。 リソースを修正する必要があることを想像してください。つまり、完全なアセンブリを再度組み立ててレイアウトする必要があり、キャッシュから取り出すことができないため、ユーザーは再びそれをロードします。 そして、リソースは別々に存在し、すべてを変えました。

要約:追加のビルドリソースには、ユーザー、サーバー、開発者の余分な時間が必要です。



復習



私の決定に使用する一般的なアプローチがあり、それを紹介したいと思います。



リソースの作成は簡単ですが、簡単に説明します。

-ソリューションエクスプローラーの目的のフォルダーのコンテキストメニューから新しいアイテムを追加します([追加]-> [新しいアイテム])。

-[全般]タブを選択します。

-リソースファイルの要素を選択します(または、その名前の要素の完全なリストを調べます)。

-名前を入力-ProjectResources;

-[追加]をクリックします(または単にEnterキーを押します)。

目的のフォルダーにリソースが作成されました。 ソリューションエクスプローラーでファイルをダブルクリックして、リソースファイルデザイナーを開きます。 表示されるグリッドの最初の列にリソースの名前-「メッセージ」を書き込み、次の列に値-「Hello World!」を入力します。

もちろん! リソースをパブリックにすることを忘れないでください。「アクセス修飾子」で、ステータスを「内部」から「パブリック」に変更します。



これで、メイン(ニュートラル)リソースの作成は終了し、Merlezonskyバレエの次の部分であるリソースのプロバイダーの作成に進むことができます。 このアプローチは、SilverlightでのReversiのプログラミングのトピックで既に説明されており、リソースである一連のプロパティを持つクラスを作成することで構成されています。



public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  1. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  2. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  3. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  4. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  5. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  6. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  7. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  8. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  9. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  10. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  11. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  12. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  13. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



  14. public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .



public class ResProvider : INotifyPropertyChanged { static ProjectResources resources = new ProjectResources(); public ProjectResources ProjectResources { get { return resources; } } #region INotifyPropertyChanged Members // Implemention of interface. #endregion } * This source code was highlighted with Source Code Highlighter .





マークアップで使用すると、次のようになります。







  1. < ユーザー制御 >
  2. < UserControl.Resources >
  3. < リソース:ResProvider x:Key = "ResProvider" />
  4. </ UserControl.Resources >
  5. < グリッド >
  6. < TextBlock Text = "{Binding Source = {StaticResource ResProvider}、Path = ProjectResources.Message}" />
  7. </ グリッド >
  8. </ UserControl >
*このソースコードは、 ソースコードハイライターで強調表示されました。


さらに、このプロバイダーをアプリケーション全体で使用する必要がある場合は、アプリケーションレベルで、つまりApp.xamlで定義するだけです。



メインリソースの準備ができたので、プロバイダーは、追加の言語でリソースを拡張する必要があります。



「どのように?」と尋ねた人のために:

-ソリューションエクスプローラーで目的のフォルダーのコンテキストメニューを呼び出します([追加]-> [新しいアイテム])。

-[全般]タブを選択します。

-メインリソース(この場合はProjectResources)の名前でリソースファイルを作成します。

-最後に、目的のカルチャの名前を追加します-ProjectResources.ru-Ru.resx;

-[追加]をクリックします。

目的の名前のファイルが作成されます。 デザイナでリソースを開き、最初の列にベースリソースに一致する名前を書き込みます。この場合は「Message」で、2番目の列に翻訳された値「Hello World!」を入力します。



私は新しいものを思いつきませんでした。実際、私の投稿では2つのドキュメントを説明しました。

  1. 方法:Silverlightベースのアプリケーションにリソースを追加する
  2. Silverlightおよびローカライズ文字列データ
上記のドキュメントから最初の記事を注意深く読んだ人(または既に見つけた人)のために、特別なセクション<SupportedCultures />をxmlプロジェクトに追加することについて特に言及しなかったと言います。 このセクションを追加すると、私が話を始めたときの問題-追加リソースの使用から逃れることはできません。



解決策



残念ながら、私が開発した方法は、1つのチェックボックス状態でリソースの遅延ロードを有効にしたい人には適していません。 ああ、私はそれをねじった方法。 要するに-あなたは池を排水し、目的の魚に到達するために一生懸命働く必要があります。



最初に、 codeplexからResourceExtensionライブラリの最新バージョンをダウンロードし、Silverlightプロジェクトへの参照に追加します。



リソースの作成は上記と変わりませんので、省略します。 必要なリソースは、ビルド前イベントを追加することだけです。 プロジェクトのSilverlightプロパティを呼び出します。 [ビルドイベント]タブを開きます。 事前ビルドで、次を追加します。

@ECHO off

DEL "$(TargetPath)"

FOR /F "delims=" %%i IN ( 'findstr /M /L /S /C:"global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager" "$(ProjectDir)*.Designer.cs"' ) DO (

DEL resource.gen

FOR /F "usebackq delims=" %%j IN ( "%%i" ) DO (

ECHO. %%j > resourcecurrentrow.gen

FOR /F "tokens=1,2* delims=(=" %%k IN (resourcecurrentrow.gen) DO (

IF "%%l" == " new global::System.Resources.ResourceManager" (ECHO %%k = new ResourceExtension.ResourceXManager(%%m)>>resource.gen ELSE (ECHO. %%j) >>resource.gen

))

XCOPY resource.gen "%%i" /Q /Y /R /K

)



* This source code was highlighted with Source Code Highlighter .






このアクションは、ResourceManagerクラスを、すべてのプロジェクトリソースのResourceExtensionライブラリに実装されたResourceXManagerに自動的に置き換えます。



残念ながら、このコードは、プロジェクトファイルに追加セクション<UseHostCompilerIfAvailable> FALSE </ UseHostCompilerIfAvailable>がなければ機能しません。 このコードを<PropertyGroup>セクションに追加します。



マークアップは変更されておらず、すべてがそのままです。



しかし、プロバイダーは変更され、CultureResourceProviderクラスの継承者となり、プロパティのメタデータを取得しました。





  1. ResourceExtension を使用します。
  2. ResourceExtension.Implementation を使用します。
  3. 名前空間 ResourceExtensionExample.Resources
  4. {
  5. パブリック クラス ResProvider:CultureResourcesProvider
  6. {
  7. private static ProjectResources projectResources = new ProjectResources();
  8. [ResourceProperty]
  9. パブリック ProjectResources ProjectResources
  10. {
  11. get { return projectResources; }
  12. }
  13. }
  14. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


最後のコード。 集めます。 チェックボックスをオンにしてすべてのファイルを表示し、bin \ Debug \ ru-RUフォルダーを見つけます。これをClientBinフォルダーに移動し、xapファイルの横に配置する必要があります。

それで判明するはずです

そして、「エレガントなショートパンツに簡単に変えられる」:





  1. パブリック 部分 クラス MainPage:UserControl
  2. {
  3. パブリック MainPage()
  4. {
  5. InitializeComponent();
  6. }
  7. private void Switch_Click( オブジェクト送信者、RoutedEventArgs e)
  8. {
  9. ((ResProvider)リソース[ "ResProvider" ])。Options.CultureInfo = new CultureInfo( "ru-Ru" );
  10. }
  11. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


ロシア語ロケールでリソースを便利にダウンロードできます。



成功しなかった場合。 サンプルをダウンロードして、そこですべてがどのように実装されているかを確認できます。



コンポーネントはアルファ段階にあるため、クレームはなく、自己責任で使用してください。

この実装では、文字列リソースのみが機能します。



なぜこのアプローチが選ばれたのですか? 中身は? どのように機能しますか? これについては、2番目の記事「Silverlight Resource Extension。 パート2.記述的」、これは近い将来に終了する予定です。



All Articles