6

The main menu of my program uses a ContextMenu composed of MenuItems. During the localization of my program (using Resource Dictionaries), I set a DynamicResource as the Header of each one of my MenuItems. Strangely DynamicResource compiles, but doesn't seem to affect any change during localization (the language on the Headers does not change).

Example of a MenuItem:

//I'm not sure if the x:Name or the PlacementRectangle is interfering with anything...
<ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}">
<MenuItem Header="{DynamicResource open}" />
</ContextMenu>

What are the constraints of the MenuItem control? Is it supposed to work with DynamicResource? My overall goal is to localize these strings, how do I do that?

This program is in WPF. Thank you.

UPDATE: This is how my Resource Dictionaries are referenced in my App.xaml file:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="Lang.en-US.xaml" />
        </ResourceDictionary.MergedDictionaries>
    <ResourceDictionary>
<Application.Resources>

UPDATE 2: The example string in my English Resource Dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:String x:Key="open">Open</sys:String>
</ResourceDictionary>

Update 3: An example function for how I change the current Resource Dictionary to Spanish:

private void spanishChange_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Resources.MergedDictionaries.Clear();

    Application.Current.Resources.MergedDictionaries.Add(
            (ResourceDictionary)Application.LoadComponent(new Uri("LangspES.xaml", UriKind.Relative)));

    LanguageChange.FireLanguageChanged();
}
Eric after dark
  • 1,768
  • 4
  • 31
  • 79
  • Have you looked at Microsoft's recommendations? http://msdn.microsoft.com/en-us/library/ms788718(v=vs.110).aspx – Dan Busha Dec 11 '13 at 20:33
  • Yes I've seen this. If this is the only solution it looks like I might need to use another method for my menu. – Eric after dark Dec 11 '13 at 20:43
  • How are you creating this MenuItem? It should work. – 123 456 789 0 Dec 11 '13 at 20:55
  • The `MenuItem` is included in the implementation of a `ContextMenu`. I will update my question – Eric after dark Dec 11 '13 at 21:05
  • `\Resources\LocalizedResources.resx`, `Content="{x:Static res:LocalizedResources.YOURKEY}"`. That's how I do localization. The language that's being read depends on the threads culture settings: // The following line provides localization for data formats Thread.CurrentThread.CurrentCulture = YOURCULTURE; // The following line provides localization for the application's user interface Thread.CurrentThread.CurrentUICulture = YOURCULTURE; – SeToY Dec 19 '13 at 10:59
  • Have a read of the largest most comprehensive answer I've ever seen on SO... http://stackoverflow.com/questions/2648849/how-are-dynamicresources-built-and-their-use-in-contextmenus – Paul Zahra Jan 08 '14 at 14:39
  • @PaulZahra The part titled: "Dynamic ContextMenu updated through data binding" is getting close to what I need, but if I use this solution I don't know how to deal with `MenuItem` click events – Eric after dark Jan 08 '14 at 19:07
  • Instead of binding the whole list through the context menu, why not bind menuitems individually? See http://stackoverflow.com/questions/1013558/elementname-binding-from-menuitem-in-contextmenu – Paul Zahra Jan 09 '14 at 09:18
  • Are you saying that I should bind the `Headers` to `strings`? – Eric after dark Jan 09 '14 at 14:51
  • @Ericafterdark What I'm saying is instead of binding the whole list with something like instead try binding the entries like etc – Paul Zahra Jan 10 '14 at 09:11

1 Answers1

2

Have you added LANGUAGE.xaml file to App.ResourceDictionary or control ResourceDictionary?

e.g.

<Application.Resources>
    <ResourceDictionary Source="LANGUAGE1.xaml" />
    <ResourceDictionary Source="LANGUAGE2.xaml" />
</Application.Resources>

If not how are you referencing your resource dictionaries?

Update:

If you change

<MenuItem Header="{DynamicResource open}" />

to

<MenuItem Header="{StaticResource open}" />

Does it then work ? Or even does

<TextBox DockPanel.Dock="Top" Text="{StaticResource open}" />

work ?

Seemingly your xaml should work, which makes me wonder have you setup localisation correctly in your app?

For how to setup localisation in .net 4.5 see this msdn link

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • I am referencing my dictionaries just like this – Eric after dark Jan 08 '14 at 16:46
  • Non of those methods work... :-/ Right now I am even trying it with a `TextBlock`. I double checked and these words are definitely in my resource dictionaries. I can verify that localization is setup correctly because the rest of my program localizes fine. – Eric after dark Jan 10 '14 at 13:16
  • hmmm... It's not something daft that 'open' is a reserved word is it? Try changing to something else just encase (worth trying). – Paul Zahra Jan 10 '14 at 13:41
  • Nope, changed it and still nothing – Eric after dark Jan 10 '14 at 13:45
  • Put your language file (xaml) in your question, maybe it's something in there. – Paul Zahra Jan 10 '14 at 14:11
  • hmm... btw have you tried taking the extra full stop out ? i.e. Lang.en-US.xaml to Langen-US.xaml or even LangenUS.xaml as a test – Paul Zahra Jan 10 '14 at 15:15
  • Wow, this is frustrating. I removed the extra full stop in all of my dictionaries and still not working. FYI, the buttons that I use to change the language are located in the Menu that I am working with. I've also set a break point in the functions that I use to change the language and verified that the code does get run through. I've updated my question with one of those functions. – Eric after dark Jan 10 '14 at 16:43
  • hmm... a bit stumped to be honest... at this point i would begin seperation of code to isolate the problem, beginning with just setting up a merged dictionary and reading from it to a text box, then incrementaly introduce your existing code, i.e. then put it in a context menu, then localise it etc – Paul Zahra Jan 13 '14 at 09:48