3

I have been playing around WPF recently and I want to know how to use English culture resources even when the app culture is set to another culture say Arabic.

I have defined two resource files containing the HelloWorld key under a Resources folder.

  • AppResources.resx (Has HelloWorld = Hello World!)
  • AppResources.ar.resx (Has HelloWorld = مرحبا بالعالم)

HomePage.xaml

First I have declared the resource file's namespace as res

 xmlns:res="clr-namespace:Demo.Core.Resources;assembly=Demo.Core"

and then used it in the label to show Hello World!

<Label Content="{x:Static res:AppResources.HelloWorld}"/>

I have set Arabic as the app culture

CultureInfo cultureInfo = new CultureInfo("ar");
Resources.AppResources.Culture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

I want to show the English Hello World! But it is showing the Arabic hello world (مرحبا بالعالم)

This is because I have set the CurrentUICulture & AppResources culture to Arabic. Is there any way with these setting, I could get still use the English strings defined in AppResources.resx file as it is in XAML? Basically, I want to ignore the culture setting and want to use the English resource directly in the XAML. Thanks in advance.

Shiblu
  • 457
  • 3
  • 12

2 Answers2

2

You could use the ResourceManager class to programmatically get a resource in a specified culture:

ResourceManager rm = new ResourceManager(typeof(AppResources));
lbl.Content = rm.GetString("HelloWorld", CultureInfo.InvariantCulture);

You could use create a converter that performs the translation for you:

public class ResourceConverter : IValueConverter
{
    private static readonly ResourceManager s_rm = new ResourceManager(typeof(AppResources));
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string key = value as string;
        if (key != null)
            return s_rm.GetString(key, culture);

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

And use it in your XAML markup like this:

<Label Content="{Binding Source=HelloWorld, 
    Converter={StaticResource ResourceConverter}, 
    ConverterCulture=en-US}" />
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks. This solves the issue. But I am searching for a way to directly read the content of the AppResources.resx in file ignoring cultures to use in XAML. Let me wait a bit for alternatives to your answers before marking it as accepted. – Shiblu Mar 12 '19 at 08:28
2

This thread C#: How to get a resource string from a certain culture seems to give an answer to what you're looking for. Basically it boils down to call Resources.ResourceManager.GetString("foo", new CultureInfo("en-US")); If you need to use it directly from XAML then write MarkupExtension that, given resource key, returns needed localised strings

[MarkupExtensionReturnType(typeof(string))]
public class EnglishLocExtension : MarkupExtension 
{
  public string Key {get; set;}
  public EnglishLocExtension(string key)
  { 
     Key = key; 
  }
  public override object ProvideValue(IServiceProvider provider) 
  { // your lookup to resources 
  }
}

I'd prefer this appoach because it's more consice. Xaml:

<Label Content={EnglishLoc key}/>
Brat Wiekszy
  • 31
  • 1
  • 4