Click here to Skip to main content
6,295,667 members and growing! (12,925 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » General License: The Code Project Open License (CPOL)

StringsResourceGenerator, Custom Task + Addin for managing strings in ResourceDictionary from code.

By jonnynolimits

Creates a xaml file for your strings and generates a class to semplify the using from code
C# (C# 3.0), WPF
Posted:27 May 2008
Views:6,407
Bookmarked:9 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 3.66 Rating: 4.05 out of 5

1

2
1 vote, 12.5%
3
1 vote, 12.5%
4
6 votes, 75.0%
5

Introduction

This Addin permits you to define strings into a ResourceDictionary of your project and use them in code just like you could do with resx files.

In this way, during localization process, you don't have to merge satellite dlls.

The Custom Task for MsBuild

The core of the utility is the StringsResourceGeneratorTask, a custom task that runs BeforeBuild step of MsBuild.

You can use the task without StringsResourceGenerator addin, installing the dll in GAC and editing your project file manually.

At the bottom of project file, as children of Project node, you have to add:

<UsingTask
    TaskName="StringsResourceGeneratorTask"
    AssemblyName="StringsResourceGeneratorTask,
        Version=1.0.0.0,
        Culture=neutral,
        PublicKeyToken=157d1456ea169140" />
<Target
    Name="BeforeBuild">
    <StringsResourceGeneratorTask
    Namespace="YourNamespace"
    AssemblyName="YourAssembly"
    ClassName="YourClassName"
    StringsResourceFileName="YourRelativeFileNamePath" />
</Target>

For UsingTask and Target tags see "References".
The StringsResourceGeneratorTask tag defines the custom tag and its attributes the properties.
The Namespace attribute specifies the namespace of the class that will be created.
The AssemblyName attribute defines the assembly of your project.
The ClassName is the name of the class that will be generated.
The StringsResourceFileName is the relative path of the files xaml and cs
(for example if the files are called LocalizableStrings.xaml and LocalizableStrings.xaml.cs under "Folder1", the value of StringsResourceFileName will be "Folder1/LocalizableStrings", any extension is necessary).

Reload the project. A dialog will appear:

Choose "Load project normally".

The task can generate code for cs only, but it's easy to expand it for other language.
You have to inherit from TypeCodeController class and override StringsCodeTemplate property and GeneratePropertyCode function.
The CSController class, used to generate cs code, is an example.

In the project you have to insert a .xaml file and a .cs file.
The cs file must be in the same path of the xaml file and have the same name of it with extension .xaml.cs.

The simpler way to obtain it is to insert a WPF UserControl.
In the xaml replace all with a empty ResourceDictionary with the reference to mscorlib library and prefix "system" for the namespace.
The prefix system is very important because it will be used by the custom task:

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

</ResourceDictionary>

In the cs file remove all. If you want add a string, go to xaml file and add:

system:String x:Key="MyString">Hello World!</system:String>

And compile the project (see "Known Bugs"). Now if you want show a MessageBox you can write:

MessageBox.Show(YuourClassName.MyString);

In the Documentation.zip there is a doc file "How to use StringsResourceGeneratorTask" that explains the settings of the task in a detailed way.

Here is the scheme for the task:

The Addin

This Addin adds an item in the Tools main menu with the name "StringsResourceGenerator...".
You must select an item or a folder in the project in which you want to add xaml and cs files for managing strings from code.
Then you click the addin item. A dialog appears:

StringsResourceFileName rappresents the files name for ResourceDictionary and code.
For example if you choose LocalizableStrings, two files will be added in the project:
LocalizableStrings.xaml and LocalizableStrings.xaml.cs.

Namespace is the namespace in which the class will be added.
AssemblyName is the name of the Assembly generated by compile process.
ClassName rappresents the name of the class that will be generated.
Once you have generated the files you can modify (or delete) file name or class name,
but manually you have to edit the project file and change the attributes related to StringsResourceGeneratorTask.
The Addin uses a template for adding files to project. The template, during the installation, will go under Visual Studio 2008 CSharp item templates.
There isn't a template for vb.

Environments supported

Visual Studio 2008 is the environment fully supported.
If you use Visual Studio 2005 you can use the custom task only and set it manually (see "The Custom Task for MsBuild").

Future Developments

Support for vb projects.

Known Bugs

After adding xaml and cs files with Addin menu item, the first compilation doesn't work. After the second one all works correctly.
If your project hasn't WindowsBase and PresentationFramework as references you have add them manually.
If you select the project root the addin doesn't work! You have to select an item or a folder.
If you modify or delete resource files previously created, the project file isn't updated automatically.

References

For Custom Task:
http://community.bartdesmet.net/blogs/bart/archive/2008/02/15/the-custom-msbuild-task-ookbook.aspx

http://blogs.msdn.com/msbuild/archive/2005/11/23/496396.aspx

http://bartdesmet.net/blogs/bart/archive/2006/04/13/3896.aspx


For Addin Deploy:
http://www.codeproject.com/KB/install/AddinCustomAction.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jonnynolimits


Member
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.
Occupation: Software Developer
Location: Italy Italy

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko22:47 17 Dec '08  
GeneralVB support Pinmemberklauswiesel1:47 12 Sep '08  
GeneralRe: VB support Pinmemberjonnynolimits4:15 15 Sep '08  
GeneralVB support Pinmemberklauswiesel1:13 12 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 May 2008
Editor:
Copyright 2008 by jonnynolimits
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project