Reputation: 455
I found some tutorials and added two resources file to Properties folder of my project and named them "Resources.tr-TR.resx" and "Resources.en-US.resx" and as default my "Resources.resx" is file also there. I set Access Modifier to "Public". And I call it in my xaml code like
Content="{x:Static p:Resources.MainWindow}"
In my files It has values and initially I can see it reads it correct. I have a menu button which changes the language and in action method I write
private void englishLanguageMenuButton_Click(object sender, RoutedEventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
}
or in another action lets say
private void macedonianLanguageMenuButton_Click(object sender, RoutedEventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("tr-TR");
}
However system doesnt work. What is missing ? Can I dynamically change the language ? Or how can I change the resx file dynamically
Thanks
Upvotes: 1
Views: 2468
Reputation: 2741
Try this
Add this code in your Application
/// <summary>
/// Wraps up XAML access to instance of WPFLocalize.Properties.Resources, list of available cultures, and method to change culture
/// </summary>
public class CultureResources
{
/// <summary>
/// Backing filed for provider
/// </summary>
private static ObjectDataProvider provider;
/// <summary>
/// Gets Resource provider
/// </summary>
public static ObjectDataProvider ResourceProvider
{
get
{
if (provider == null)
{
provider = (ObjectDataProvider)App.Current.FindResource("Resources");
}
return provider;
}
}
/// <summary>
/// Change the current culture used in the application.
/// If the desired culture is available all localized elements are updated.
/// </summary>
/// <param name="culture">Culture to change to</param>
public static void ChangeCulture(CultureInfo culture)
{
////remain on the current culture if the desired culture cannot be found
//// - otherwise it would revert to the default resources set, which may or may not be desired.
V_Parcel.Properties.Resources.Culture = culture;
ResourceProvider.Refresh();
}
/// <summary>
/// The Resources ObjectDataProvider uses this method to get an instance of the WPFLocalize.Properties.Resources class
/// </summary>
/// <returns>Returns resource instance</returns>
public V_Parcel.Properties.Resources GetResourceInstance()
{
return new V_Parcel.Properties.Resources();
}
}
Add a XAML CultureDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resource="clr-namespace:Cultures">
<!-- Resources ODP contains the current instance of the WPFLocalize.Properties.Resources class.
Used in bindings to get localized strings and automatic updates when the culture is updated -->
<ObjectDataProvider x:Key="Resources" ObjectType="{x:Type resource:CultureResources}" MethodName="GetResourceInstance" />
</ResourceDictionary>
In your App.XAml
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CultureDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Binding should be like
Content="{Binding ShowTerminalName,Source={StaticResource Resources}}"
And in culture change event write this
Thread.CurrentThread.CurrentCulture = new CultureInfo(currentCulture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentCulture);
CultureInfo cultureInfo = new CultureInfo(currentCulture);
CultureResources.ChangeCulture(cultureInfo);
.resx Access modifier should be public
Upvotes: 3