Click here to Skip to main content
Click here to Skip to main content

StringsResourceGenerator, Custom Task + Add-in for managing strings in ResourceDictionary from code

By , 27 May 2008
Rate this:
Please Sign up or sign in to vote.

Introduction

This add-in 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 StringsResourceGeneratorTask, a custom task that runs the BeforeBuild step of MSBuild. You can use the task without the StringsResourceGenerator add-in, installing the DLL in the GAC and editing your project file manually. At the bottom of the project file, as children of the 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 information on the UsingTask and Target tags, see the "References" section. 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. ClassName is the name of the class that will be generated. 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", extension is not necessary).

Reload the project. A dialog will appear:

Choose "Load project normally".

The task can generate code for C# only, but it's easy to expand it for other languages. You have to inherit from the TypeCodeController class and override the StringsCodeTemplate property and the GeneratePropertyCode function. The CSController class, used to generate the C# 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 as the XAML file and have the same name, with the extension .xaml.cs.

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

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

In the C# file, remove all. If you want to add a string, go to the XAML file and add:

system:String x:Key="MyString">

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

MessageBox.Show(YuourClassName.MyString);

In 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 Add-in

This add-in 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 the XAML and cs files for managing the strings from code. Then, you click the add-in item. A dialog appears:

StringsResourceFileName represents the files name for the 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 the compile process. ClassName represents the name of the class that will be generated. Once you have generated the files, you can modify (or delete) the file name or class name, but manually, you have to edit the project file and change the attributes related to StringsResourceGeneratorTask. The add-in uses a template for adding the files to the project. The template, during installation, will go under Visual Studio 2008 C# 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 Development

Support for VB projects.

Known Bugs

After adding the XAML and cs files with the Add-in menu item, the first compilation doesn't work. After the second one, all works correctly. If your project doesn't have WindowsBase and PresentationFramework as references, you have to add them manually. If you select the project root, the add-in 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 the Custom Task:

For the Add-in Deploy:

License

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

About the Author

jonnynolimits
Software Developer
Italy Italy
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.

Comments and Discussions

 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 21:47 
GeneralVB support Pinmemberklauswiesel12-Sep-08 0:47 
GeneralRe: VB support Pinmemberjonnynolimits15-Sep-08 3:15 
GeneralVB support Pinmemberklauswiesel12-Sep-08 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 28 May 2008
Article Copyright 2008 by jonnynolimits
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid