65.9K
CodeProject 正在变化。 阅读更多。
Home

验证所有语言的 Resx 文件中的字符串

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2008年9月23日

CPOL
viewsIcon

36131

downloadIcon

270

一个用于验证支持多种语言的 Resx 文件中的本地化字符串的工具。

引言

.NET 框架提供使用 resx 文件进行语言本地化的支持。此工具可帮助您在 GridView 中交叉检查各种语言的 resx 文件。代码基于 .NET 3.5。

背景

有关使用卫星程序集进行本地化,请参阅:.NET - 使用资源文件进行本地化

使用代码

使用此实用程序,用户可以选择多个 resx 文件并验证键是否存在于所有语言中。它以醒目的红色突出显示错误,以便开发人员可以快速修复 resx 文件。该实用程序本身就是几行代码……这要归功于 LINQ 和 .NET 3.5。

void ParseFiles(string[] fileNames)
{
    int marker = 1;
    
    foreach (string file in fileNames)
    {

        XElement xElement = XElement.Load(file);

        IEnumerable<XElement> data = 
            from text in xElement.Elements("data")
            select text;

        foreach (XElement node in data)
        {
            string key = node.FirstAttribute.Value;

            int value = dictionary.ContainsKey(key)? dictionary[key]:0;

            dictionary[key] = value | marker;
        }

        marker = marker << 1;
    }
}

关注点

该代码使用 LINQ,并且在实现目的上看起来非常简洁明了。

© . All rights reserved.