Localizing a Silverlight Application Using Resources

I have a problem localizing a Silverlight application using resources. I wanted my multilingual engine to be cross-platform, so I put all the localized resources in a project like Portable Class Library. In this project, I created two resource files, Localization.resx and Localization.en.resx, and I set the modifier access for the public in both files. Then I created a proxy class called "LocalizationProxy", which is a proxy class for enabling bindings.

public class LocalizationProxy : INotifyPropertyChanged { public Localization LocalizationManager { get; private set; } public LocalizationProxy() { LocalizationManager = new Localization(); } public void ResetResources() { OnPropertyChanged(() => LocalizationManager); } #region INotifyPropertyChanged region public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged<T>(Expression<Func<T>> selector) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(GetPropertyNameFromExpression(selector))); } } public static string GetPropertyNameFromExpression<T>(Expression<Func<T>> property) { var lambda = (LambdaExpression)property; MemberExpression memberExpression; if (lambda.Body is UnaryExpression) { var unaryExpression = (UnaryExpression)lambda.Body; memberExpression = (MemberExpression)unaryExpression.Operand; } else { memberExpression = (MemberExpression)lambda.Body; } return memberExpression.Member.Name; } #endregion } 

In the next step, I modified the Silverlight csproj file and added the culture "en" to the supported types

 <SupportedCultures>en</SupportedCultures> 

In addition, in the application resources that I created and the instance of the LocalizationProxy class

  <Application.Resources> <Localization:LocalizationProxy x:Key="LocalizationProxy"></Localization:LocalizationProxy> </Application.Resources> 

I also changed the "Neutral language" in the assembly to "Polish" - this should be the default application language. In the last step, I moved some values ​​from the view to resources

 <TextBlock TextWrapping="Wrap" x:Name="PageTitle" Text="{Binding Source={StaticResource LocalizationProxy},Path=LocalizationManager.Title,Mode=TwoWay}" /> 

Unfortunately, even though Thread.CurrentThread.CurrentCulture is "pl-PL", my application is still in English. However, if I use the same code in a Windows Phone application, everything works fine - I can even change the language of the application at runtime, is there a difference in the localization of the Silverlight application and the localization of Windows Phone applications?

Here is my application

http://www.fileserve.com/file/TkQkAhV/LocalizationSolution.rar

As I mentioned earlier, localization in Windows Phone works fine, but the labels are not translated in Silverlight applications

+6
source share
2 answers

You must use the full ISO 3166 and 639 codes in conjunction with a hyphen in Rumplin .

cm

0
source

make sure that you have taken all the steps below.

Create a ViewModel Class

Implement the INotifyPropertyChanged interface:

  public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } 

Create a property that returns the correct language:

  public object MainResourcesSmart { get { var myCulture = Thread.CurrentThread.CurrentUICulture.Name.ToUpper(); switch (myCulture) { case "EN-US": return new MyResourceEn(); case "ES-AR": return new MyResourceEs(); default: return new MyResource(); } } } 

Set all resources to public

Use the method below to update it on the screen every time you change the language:

  private void MainResourcesSmartRefresh() { OnPropertyChanged("MainResourcesSmart"); } 

Bind the ViewModel to your View (MainPage), for example:

  public MyViewModel ViewModel { get { return (MyViewModel)DataContext; } set { DataContext = value; } } public MainPage() { ViewModel = new MyViewModel(); } 

Bind the Resouce property to UserControl :

  <TextBlock Height="20" HorizontalAlignment="Left" VerticalAlignment="Bottom" Foreground="#7F4F8AB2" FontSize="10.667" Text="{Binding MainResourcesSmart.NameOfTheCompany}" FontFamily="Trebuchet MS" /> 
0
source

Source: https://habr.com/ru/post/906354/


All Articles