Geeks With Blogs
Mark Pearl

 

One of the challenges one faces when doing multi language support in WPF is when one has several projects in one solution (i.e. a business layer & ui layer) and you want multi language support. Typically each solution would have a resource file – meaning if you have 3 projects in a solution you will have 3 resource files.

Image1

 

For me this isn’t an ideal solution, as you normally want to send the resource files to a translator and the more resource files you have, the more fragmented the dictionary will be and the more complicated it will be for the translator.

This can easily be overcome by creating a single project that just holds your translation resources and then exposing it to the other projects as a reference as explained in the following steps.

Step 1

Step 1 -  Add a class library to your solution that will contain just the resource files.

Image2

Your solution will now have an additional project as illustrated below.

Image3

Step 2

Reference this project to the other projects.

Image4

Step 3

Move all the resources from the other resource files to the translation projects resource file.

Step 4

Set the translations projects resource files access modifier to public.

Image5

Step 5

Reference all other projects to use the translation resource file instead of their local resource file.

To do this in xaml you would need to expose the project as a namespace at the top of the xaml file… note that the example below is for a project called MaxCutLanguages – you need to put the correct project name in its place.

 

xmlns:MaxCutLanguages="clr-namespace:MaxCutLanguages;assembly=MaxCutLanguages"

 

And then in the actual xaml you need to replace any text with a reference to the resource file.

<TextBlock Text="{x:Static MaxCutLanguages:Properties.Resources.HelloWorld}"/>

End Result

You can now delete all the resource files in the other projects as you now have one centralized resource file.

Posted on Wednesday, March 2, 2011 5:01 AM C# | Back to top


Comments on this post: Centralizing a resource file among multiple projects in one solution (C#/WPF)

# re: Centralizing a resource file among multiple projects in one solution (C#/WPF)
Requesting Gravatar...
Hi
Nice rwrite up. but i have a question. How will we do it if it is in a windows application.
Scenerio:
I have resource files spread across different project like this. i need to centralize it. so how do i do it in WIndows form case? Please clarify
Left by Tijo on Jul 21, 2011 9:14 AM

# re: Centralizing a resource file among multiple projects in one solution (C#/WPF)
Requesting Gravatar...
Yes !!!!!!!!!!!!!!!

I'tried a lot, including resources in a class library, in subfoder ... compile ok but didn't work.
And I tried with your example ................ Worked :)

Thanks a lot
Left by TheBeO on Aug 27, 2011 4:58 PM

# re: Centralizing a resource file among multiple projects in one solution (C#/WPF)
Requesting Gravatar...
There are also tools you can rely on to eliminate this issue altogether. For instance, see http://www.hexadigm.com and there are others as well (the former is very good).
Left by Jack on Jan 18, 2012 5:41 PM

# re: Centralizing a resource file among multiple projects in one solution (C#/WPF)
Requesting Gravatar...
But when we will deploy the project can we keep the .resx files outsiide the dll

in class library it will be compiled with the classes and will be the part of the dll and will have to modify the resource file by opening the project files.

Saperately modification of resource is not possible if it is in the dll.
is there any solution to exclude it from dll and refer it from outside saperate .resx file as we do in web applications.
Left by Mayur Rathi on Aug 09, 2012 4:43 PM

# re: Centralizing a resource file among multiple projects in one solution (C#/WPF)
Requesting Gravatar...
When i use this code
{x:Static MaxCutLanguages:Properties.Resources.HelloWorld}

It is giving error as
The tag 'Static' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml'.

Can anyone suggest solution for this?
Left by Gaurav on Apr 23, 2013 4:23 PM

# re: Centralizing a resource file among multiple projects in one solution (C#/WPF)
Requesting Gravatar...
Thanks a lot! I tried everything, and only this worked.
Left by Jo on Sep 04, 2013 5:33 PM

# re: Centralizing a resource file among multiple projects in one solution (C#/WPF)
Requesting Gravatar...
very nice, it works very well, but i have a question when i bind content like this {x:Static MaxCutLanguages:Properties.Resources.HelloWorld} it gives me a warning nest types are not supported in Properties.Resources this will affect the wpf desginer do not display the layout (Ivalid Markup)

Can anyone suggest solution for this?
Left by Karez on Nov 07, 2016 7:47 AM

Your comment:
 (will show your gravatar)


Copyright © MarkPearl | Powered by: GeeksWithBlogs.net