Skip to main content
 首页 » 编程设计

c#之我如何以编程方式在 resx 文件中创建另一个列

2024年02月05日7zhengyun_ustc

描述:
我正在为 MultiLingual、MultiOrganization CMS 编写字典。

目前我们正在使用数据库解决方案 (Sql Server),但由于数据库流量巨大,我们正在寻找替代解决方案,我找到了 .resx 文件。

在这里,我们将以编程方式创建 .resx 文件和一个 .resx 文件/语言。 在每个 .resx 文件上,我们要添加一个 organizationId。您可以在下面看到英语语言的 .resx 文件。

我们想要 .resx 中的这种结构

Name    Value        Comment                   OrganizationId 
About   About     this file is for english          1 
Login   Login     this file is for english          2 
 

The scenario is that when in my cms a lang is added i will be creating a resource file with it which will contain the dictionary(English key , that language value) for the language.

Now everything would have gone as planned if one word would have one meaning but there is a requirement that Two organization can one have different meaning for a single word.

for example word hello will have meaning abc for organization1 and it will have meaning abc2 for organization2.

My Question:
1.How can I do this using .resx file and if its not possible using .resx file then what is the most efficient (speed,maintainance,flexibility) alternate for this scenario.

My Code:
Currently I am trying this sample:

             // Define a resource file named CarResources.resx.  
            using (ResXResourceWriter resx = new ResXResourceWriter(@"D:\VSProjects\CreateResourceFile\CreateResourceFile\CarResources.resx")) 
            { 
 
                resx.AddResource(new ResXDataNode("About", "About"){    Comment = "this file is for english" }); 
                resx.AddResource("Login", "Login"); 
                resx.AddResource("News", "News"); 
                resx.AddResource("Headline", "Headline"); 
                resx.AddResource("Information", SystemIcons.Information); 
 
            } 

请您参考如下方法:

Resx 文件具有严格的架构,它不包含您想要的其他字段。因此,即使您设法添加文件(即使用常规 XML API),标准 .Net 资源方法也可能无法处理此类修改后的文件。

根据您的需要,您可以使用完全自定义的 XML(LINQ-to-XML 提供了一种创建/读取 XML 文档的简单方法),或者如果您真的想使用 resx 文件 - 按组织分区文件(除了语言)喜欢

text.org1.en-us.resx 
text.org2.en-us.resx 
text.org1.fr.rest