Enterprise Library Additions: String Resource Generator

Enterprise Library Additions: String Resource Generator

 

Written by: Rickie Lee (rickieleemail#yahoo.com)

My blog: www.cnblogs.com/rickie

Enterprise Library v1.0Application Blocks中大量采用SR.strings作为字符串资源文件,记录应用程序中需要的输出信息。通过String Resource Generator处理后,该文件自动产生同名的.CS.RESX文件。其中.CS文件是一个用来访问这些字符串资源的类,包括格式化参数的特性,如.NET {}格式相同,具体可以查阅String.Format帮助。

 

1. String Resource Generator简介

Version1.2.5

作者:Martin, his blog: http://wah.onterra.net/blog/

Download: http://projectdistributor.readify.net/Projects/Project.aspx?projectId=2

安装:下载上诉MSI文件安装即可,通过VS.NET在现有的.RESXSR.strings文件设置Custom tool属性为:StringResourceToolSRCodeGen

蝈蝈俊.net对上述工具也有简单介绍。

 

2. String Resource Generator v1.2.5新支持如下特性:

  • 支持VB.NET(当然有C#
  • 支持改变类的名称,通过使用不同的.strings文件名
  • 支持定制namespace,通过在solution explorer中设置custom tool namespace属性
  • 支持多个.strings文件,如SR.strings, SR.strings-de, SR.strings-cn,要求后缀中不允许有. dot
  • 当然还有好多其他功能,具体可以查询上述链接

 

3. SR.strings文件编辑

特殊字符说明:

#;   注释字符串

#!    指定特殊选项

#! accessor_class_accessibility = public 定义SR类为public,而不是缺省的internal

#! culture_info = <YourCodeHere> 定义特定的culture,而不是缺省的Culture属性。

#! generate_class = false 设置不自动生成class,如下所示:

#! generate_class = false

[strings.de]

ResName = German blah

 

简单的方法是照着Enterprise LibrarySR.strings文件编辑就可以了。如下是节选Security Application Block Quick StartSR.strings文件:

#

# Options description:

#

 

# Options are specified as lines starting with "#!"

# To use an option below delete the first # in the corresponding line

 

# To define the SR class public instead of internal (default):

##! accessor_class_accessibility = public

 

# To use a code fragment as the culture instead of the Culture property (default):

##! culture_info = <YourCodeHere>

# for example, #! culture_info = MyResources.CommonCultureInfo

# where CommonCultureInfo is a static member of a class MyResources

 

#! accessor_class_accessibility = public

 

[strings]

 

#Messages used by database management scenarios

CreateUserTitleMessage = Create a new user

UserCreatedMessage(username) = User {0} created.

DeleteUserMessage(username) = User {0} deleted.

AddUserRoleMessage(username, role) = Role {1} added for user {0}.

DeleteUserRoleMessage(username, role) = Role {1} deleted for user {0}.

AddUserRoleErrorMessage = Please select a user name and select or enter a role.

DeleteUserRoleErrorMessage = Please select a user name and a role.

DeleteUserErrorMessage = Please select a user name.

ErrorMessage = Error

 

节选的SR.cs文件(并不是上述SR.strings对应的SR.cs类文件):

public class SR

{

    public static System.Globalization.CultureInfo Culture

    {

        get

        {

            return Keys.Culture;

        }

        set

        {

            Keys.Culture = value;

        }

    }

   

    public static string AddUserRoleErrorMessage

    {

        get

        {

            return Keys.GetString(Keys.AddUserRoleErrorMessage);

        }

    }

   

    public static string AuthenticateTitleMessage

    {

        get

        {

            return Keys.GetString(Keys.AuthenticateTitleMessage);

        }

    }

}

作者:Rickie Lee (rickieleemail#yahoo.com)

本文参考String Resource Generator项目和Enterprise Library Security Application Block Quickstart. Special thanks go out to Martin.

 

Reference:

1. Rickie, Microsoft patterns & practices Enterprise Library系列分析文章

 

posted @ 2005-02-24 07:03  Rickie  阅读(2442)  评论(3编辑  收藏  举报