Domanda

I’m working with an application for both Windows desktop and Windows store, potentially I will add Windows Phone in the future. I’m having most of my logic in a library and create different GUI for the different platforms.

I want to localize my application and want to share string resources between the platforms. But how do I do that?

For Windows desktop the most common approach seems to be using resx files. Here is a short example:

http://compositeextensions.codeplex.com/discussions/52910

For Windows store app resw files are used instead, here is an example of that:

http://msdn.microsoft.com/en-us/library/windows/apps/hh965326.aspx

Both these solutions are platform specific which I don’t like :-(. I really want to have all my string in one file/language and being able to use that in all platforms. Is there any solution for this?

Update 17 Feb 2014: As I understand it resx and resw files are in the same format. What is missing in Windows store app is that no class file is generated for the resw file. If I just could get a file like that my problems would be solved. Then I could put an instance of that class in my view model and access all text via properties.

The class file generated in WPF application almost works. The problem is this line that looks something like this:

global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ResxTest.Properties.StringTest", typeof(StringTest).Assembly);

To get this to compile I need to change it to this:

global::System.Resources.ResourceManager temp = new System.Resources.ResourceManager("ResxTest.Properties.StringTest", typeof(StringTest).GetTypeInfo().Assembly);

But the resource ResxTest.Properties.StringTest can’t be loaded in my Windows Store application. For some reason I need to rename my resource to Resource.resw and load it with the name “Resource”. I have tried all kind of names but this is the only one that works. Using name MyApplication.MyResource never works.

I’m not sure if I’m on the right track. I’m almost so desperate that I will make my own solution were I convert an XML-file with all strings I needed to a huge class with properties that I could use to get all string without any resource files. But I think that is ugly and cheating so I hope someone could give me a better idea :-).

Update 24 Feb 2014: I was wrong! Things are working quite nicely with Portable class library. If I use that I could put an instance of auto generated C# class in my view model and access all strings from that object.

But if I use an ordinary library things doesn’t work as properly in Windows Store app (WPF is fine). I have tried to copy all files to a Windows store class library from a working Portable class library. When I try to create an instance of the auto generated file I always get:

An exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll but was not handled in user code

Additional information: Unable to load resources for resource file "MetroLib.StringResources" in package…

Quite annoying since I’m using the express editions of Visual Studio where portable class library isn’t available. So probably I will develop my own solution to generate classes from a resource file (which also gives me some other benefits). But I’m still curious what I’m doing wrong.

È stato utile?

Soluzione

I finally solved the problem. I simply developed a simple tool (ResToCode) to convert resource files to pure C# classes. Quite similar to what Visual Studios resgen.exe is doing, but with some extra features. It works really well so I’m quite happy about it :-).

The tool is available for anyone at CodeProject:

http://www.codeproject.com/Articles/744528/ResToCode-Localization-tool-for-Windows-Desktop-St

Altri suggerimenti

You can put all your resources in a Portable class library and use these libraries on all platform. You might have to check what version of .NET framework you are using. Portable libraries are not available on all the versions of all the platforms.

http://msdn.microsoft.com/en-us/library/vstudio/hh871422(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/vstudio/gg597391(v=vs.110).aspx#members

Have you looked at the Multilingual App Toolkit? It keeps the translations in an industry standard XLIFF format and can generate resources files for Windows and Windows Phone.

I had the same issue with the MissingManifestResourceException for Portable Class Libraries too. Today I created a new Portable Class Library with another name (without the string 'Resources' in it): and it works like a charm. No idea why this hasn't worked before (perhaps the name of the resource).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top