Search code examples
wpfvb.netxamlresx

I need to update an application to be available in French using pre-translated words stored in a .resx file at the push of a button


I am given two .resx files which provide all the words I'll need to use in English and French. Right now, the application is only available in English, and I need to create a button that will change the content of labels to point to the French .resx instead of the English one and vice-versa. The button will say English if French is the current language and Francais if English is the current.

The way the program is written currently, the language is hardcoded so that the labels only look at the English .resx, and aren't fluid depending on a selected language. I am new to this kind of programming and I'm stumped.

<Label Margin="12,2.222,0,0" Content="{Resx ResxName=LegalServicesTimesheets.Labels, Key=ApplicationTitleDASH}"/>

''Both .resx files have words associated with the key ApplicationTitleDash
''The French .resx file is called Labels.fr-CA.resx

As you can see, the English setting is currently hard-coded and I need to make it so the .resx file depends on which language is currently being used.


Solution

  • {Resx ...} is a markup extension. Looks like you're using the ResxExtension by Grant Frisken:

    <TextBlock Text="{Resx ResxName=MyApp.TestWindow, Key=MyText}"/>
    

    You don't have anything to change in your XAML markup. If you want you can specify a Language attribute and bind to, say, some UICulture property (a CultureInfo object) on your ViewModel:

    <Window ResxExtension.DefaultResxName="WpfApp.MainWindow" Language="{UICulture}"> 
    

    But otherwise the way .resx works, it pulls from whatever resource file best matches the Culture of the resource object, i.e. in your case:

    LegalServicesTimesheets.Culture = CultureInfo.GetCultureInfo("fr-CA")
    

    If you set the culture to a culture that doesn't have a .resx file, it should fall back to the "neutral" .resx content. Since you do have a .fr-CA.resx file, all resources pulled from LegalServicesTimeSheets will now be pulled from the .fr-CA.resx file.

    In code-behind, you can retrieve resource strings ad-hoc through the ResourceManager, by specifying a CultureInfo argument to the GeteString method:

    foo = LegalServicesTimesheets.ResourceManager.GetString("Labels", CultureInfo.CurrentUICulture)