The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP] Do Windows Store apps support localized resources in assemblies? If so, how do you get a ResourceLoader for an assembly? RRS feed

  • Question

  • This MSDN article seems to imply that all resources for a Windows store app must be contained in the main executable:

    https://msdn.microsoft.com/en-us/library/Hh694557.aspx

    Is that actually a limitation?  If it is not, how do you get a ResourceLoader for an assembly? You can get a ResourceLoader for a particular string that represents a ResourceMap, but if that is the solution, I can't find any examples showing how to identify a ResourceMap string for a referenced assembly. The examples I can find seem to use ResourceManager, which is not recommended for use in Windows Store apps.

    In my example, I have an assembly named MVVMTestbed.ViewModels where I have enabled the Multilingual App Toolkit.  I have a resource string in there named "Mode_Teacher".  I have configured Windows 10 to use the pseudo-language language.  

    None of the following statements will correctly retrieve my resource:

    var rl = ResourceLoader.GetForViewIndependentUse("MVVMTestbed.ViewModels");
    var test1 = rl.GetString(key);
    var test2 = rl.GetString($"[MVVMTestbed.ViewModels]Mode_Teacher");
    var test3 = ResourceLoader.GetForViewIndependentUse().GetString("[MVVMTestbed.ViewModels]Mode_Teacher");
    var test4 = ResourceLoader.GetForViewIndependentUse().GetString("ms-resource://MVVMTestbed.ViewModels/Resources/Mode_Teacher");


    Monday, July 27, 2015 12:57 PM

Answers

  • It turns out that all of the resources for all referenced assemblies are included in the main ResourceMap for your executable.  I ran the following code:

    foreach (var map in ResourceManager.Current.AllResourceMaps) {
        Debug.WriteLine(map.ToString());
        foreach (var kvp in map.Value) {
            Debug.WriteLine($"{kvp.Key}={kvp.Value}");
        }
    }

    And observed the following resources from my main executable as well as the referenced assemblies:

    MVVMTestbed.ViewModels/Resources/Mode_Publisher=Windows.ApplicationModel.Resources.Core.NamedResource

    MVVMTestbed.ViewModels/Resources/Mode_Teacher=Windows.ApplicationModel.Resources.Core.NamedResource

    MVVMTestbed.Views.Universal/Resources/AllBooks_Header/HeaderText=Windows.ApplicationModel.Resources.Core.NamedResource

    MVVMTestbed.Views.Universal/Resources/TestTB/Text=Windows.ApplicationModel.Resources.Core.NamedResource

    Resources/Test=Windows.ApplicationModel.Resources.Core.NamedResource

    Resources/TestGreeting/Text=Windows.ApplicationModel.Resources.Core.NamedResource

    That showed me that I could use the following code to access the resources:

    var rlViewModels = ResourceLoader.GetForViewIndependentUse("MVVMTestbed.ViewModels/Resources");
    var str = rlViewModels.GetString("Mode_Teacher");
    var rlViews = ResourceLoader.GetForViewIndependentUse("MVVMTestbed.Views.Universal/Resources");
    var str2 = rlViews.GetString("TestTB/Text");



    • Marked as answer by David Cater Monday, July 27, 2015 7:37 PM
    • Edited by David Cater Thursday, July 30, 2015 1:33 PM
    Monday, July 27, 2015 7:36 PM

All replies

  • Note that I can have localized resources in the assembly. Those are picked up by the Xaml in the assembly using the x:Uid attribute.  In other words, if I display a Xaml page defined in an assembly referenced by my main executable, the strings in that Xaml page are correctly localized.

    However, I still can't find a way to access those strings programmatically using ResourceLoader.  I checked ResourceManager.Current.AllResourceMaps, and there are only two ResourceMap objects in there.  One appears to be for my application (it is identified by the GUID in Package.appxmanifest), and the other is for Microsoft.NET.CoreRuntime.1.0.

    Monday, July 27, 2015 7:14 PM
  • It turns out that all of the resources for all referenced assemblies are included in the main ResourceMap for your executable.  I ran the following code:

    foreach (var map in ResourceManager.Current.AllResourceMaps) {
        Debug.WriteLine(map.ToString());
        foreach (var kvp in map.Value) {
            Debug.WriteLine($"{kvp.Key}={kvp.Value}");
        }
    }

    And observed the following resources from my main executable as well as the referenced assemblies:

    MVVMTestbed.ViewModels/Resources/Mode_Publisher=Windows.ApplicationModel.Resources.Core.NamedResource

    MVVMTestbed.ViewModels/Resources/Mode_Teacher=Windows.ApplicationModel.Resources.Core.NamedResource

    MVVMTestbed.Views.Universal/Resources/AllBooks_Header/HeaderText=Windows.ApplicationModel.Resources.Core.NamedResource

    MVVMTestbed.Views.Universal/Resources/TestTB/Text=Windows.ApplicationModel.Resources.Core.NamedResource

    Resources/Test=Windows.ApplicationModel.Resources.Core.NamedResource

    Resources/TestGreeting/Text=Windows.ApplicationModel.Resources.Core.NamedResource

    That showed me that I could use the following code to access the resources:

    var rlViewModels = ResourceLoader.GetForViewIndependentUse("MVVMTestbed.ViewModels/Resources");
    var str = rlViewModels.GetString("Mode_Teacher");
    var rlViews = ResourceLoader.GetForViewIndependentUse("MVVMTestbed.Views.Universal/Resources");
    var str2 = rlViews.GetString("TestTB/Text");



    • Marked as answer by David Cater Monday, July 27, 2015 7:37 PM
    • Edited by David Cater Thursday, July 30, 2015 1:33 PM
    Monday, July 27, 2015 7:36 PM
  • Hi David,

    Thanks for posting the solution here in the forum.

    Best regards,

    Barry


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 28, 2015 6:28 AM
  • Thank you for the solution. I was having the same problems up to now.

    But I have still no idea how to access these localized string in XAML using x:Uid. Have you also figured out how to access string resources there?

    Thanks! :)

    UPDATE:

    I figured out how this works. You can access these using the full qualifier, such as:

    x:Uid="/<LibraryName>/Resources/<ResourceName>"

    Sunday, September 20, 2015 5:47 PM
  • So you're referencing a Uid in a resource file in an assembly other than the one where the XAML is defined?  I hadn't tried to do that.  Good to know that you can do that.  My resources.resw files are always in the same assembly as the XAML files that reference them currently.
    Thursday, October 1, 2015 1:21 PM