locked
Combine String Resource files

    Question

  • Hi,

    I am working on Universal app(with Prism). Here is my scenario.
    I have a string resource file in following projects:
    1) Windows - Resources.resw - This file has windows specific string resources
    2) WindowsPhone - Resources.resw - This file has phone specific string resources
    3) Shared - SharedResources.resw - This file has string resources which are common between windows and phone.

    Now, How can I make ResourceLoader to search resources in "SharedResources.resw" and "Resources.resw" both the files?

    Any suggestions?

    Thanks in advance.
    Friday, July 25, 2014 7:07 AM

Answers

  • I believe this sample will show you how to do this:

    http://code.msdn.microsoft.com/windowsapps/Application-resources-and-cd0c6eaa


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by pratikmehta9 Monday, July 28, 2014 9:18 AM
    Friday, July 25, 2014 7:17 PM
    Moderator

All replies

  • I believe this sample will show you how to do this:

    http://code.msdn.microsoft.com/windowsapps/Application-resources-and-cd0c6eaa


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by pratikmehta9 Monday, July 28, 2014 9:18 AM
    Friday, July 25, 2014 7:17 PM
    Moderator
  • Thanks Matt, This was very useful.


    I have extended the ResourceLoader class as following:
    public class ResourceLoaderEx : IResourceLoaderEx
    {
        //This method is used when the PCL contains .resx file instead of .resw.
        public string GetString(string resource,  string baseName, Assembly assembly)         {
                ResourceManager resourceManager = new ResourceManager(baseName, assembly);
                return resourceManager.GetString(resource);
        }
    
         public string GetString(string resource)
        {
            ResourceLoader resourceLoader;
            if (resource.Contains("\\"))
            {
                resourceLoader = ResourceLoader.GetForCurrentView(resource.Substring(0, resource.LastIndexOf("\\")));
                resource = resource.Substring(resource.LastIndexOf("\\") + 1);
            }
            else
            {
                resourceLoader = ResourceLoader.GetForCurrentView();
            }
            return resourceLoader.GetString(resource);
        }
    }
    
    This class covers following scenarios:
    var windowsSpecificResource = resourceLoader.GetString(@"windows"); // This will fetch the resource from Resources.resw file
    var sharedResource = resourceLoader.GetString(@"SharedResources\testforshared"); // This will fetch the resource from the SharedResources.resw file
    var pclResource = resourceLoader.GetString("SaveErrorMessage", "Contoso.UILogic.Strings.Resources", typeof(Strings.Resources).GetTypeInfo().Assembly); // This will fetch the resource from Resources.resx file which is in my UILogic project.


    Any comments?
    Monday, July 28, 2014 9:18 AM