将生成的 resx 文件添加到项目中
Add a generated resx file to a project
我们的团队正在尝试为我们必须的所有语言翻译实施一个主 csv 文件,以使我们的系统团队能够维护我们的资源文件。我正在创建一个将在构建之前调用的 dll,以将所述 csv 文件转换为各自的 resx 和设计器文件。我已经生成了已完成的 resx 文件和设计器文件,但正在努力将生成的新 resx 文件正确添加到资源项目中。我查看了 this 将文件添加到项目的解决方案,但我无法正确添加依赖项。
这是我正在尝试使用的代码
var p =
Microsoft.Build.Evaluation
.ProjectCollection.GlobalProjectCollection
.LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);
if (p == null)
p = new Microsoft.Build.Evaluation.Project(projFilePath);
p.AddItem("Folder", @"C:\projects\BabDb\test\test2");
p.AddItem("Compile", @"C:\projects\BabDb\test\test2\Class1.cs");
p.Save();
对于可能偶然发现这个问题的任何人,以下是我最终解决它的方法
var resourceProject = Microsoft.Build.Evaluation
.ProjectCollection.GlobalProjectCollection
.LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);
if (resourceProject == null)
resourceProject = new Microsoft.Build.Evaluation.Project(projFilePath);
//add resx file if it doesn't exist in the project
if (resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == path) == null)
{
string designerPath = path.Replace(".resx", ".Designer.cs");
//Add metadata when the resx file has a designer file
resourceProject.AddItem("EmbeddedResource", path);
if (withDesigner)
{
var projectItem = resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == path);
projectItem.SetMetadataValue("Generator", "ResXFileCodeGenerator");
projectItem.SetMetadataValue("LastGenOutput", Path.GetFileName(designerPath));
resourceProject.AddItem("Compile", designerPath);
var designerItem = resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == designerPath);
designerItem.SetMetadataValue("AutoGen", "True");
designerItem.SetMetadataValue("DesignTime", "True");
designerItem.SetMetadataValue("DependentUpon", Path.GetFileName(path));
}
resourceProject.Save();
}
我们的团队正在尝试为我们必须的所有语言翻译实施一个主 csv 文件,以使我们的系统团队能够维护我们的资源文件。我正在创建一个将在构建之前调用的 dll,以将所述 csv 文件转换为各自的 resx 和设计器文件。我已经生成了已完成的 resx 文件和设计器文件,但正在努力将生成的新 resx 文件正确添加到资源项目中。我查看了 this 将文件添加到项目的解决方案,但我无法正确添加依赖项。
这是我正在尝试使用的代码
var p =
Microsoft.Build.Evaluation
.ProjectCollection.GlobalProjectCollection
.LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);
if (p == null)
p = new Microsoft.Build.Evaluation.Project(projFilePath);
p.AddItem("Folder", @"C:\projects\BabDb\test\test2");
p.AddItem("Compile", @"C:\projects\BabDb\test\test2\Class1.cs");
p.Save();
对于可能偶然发现这个问题的任何人,以下是我最终解决它的方法
var resourceProject = Microsoft.Build.Evaluation
.ProjectCollection.GlobalProjectCollection
.LoadedProjects.FirstOrDefault(pr => pr.FullPath == projFilePath);
if (resourceProject == null)
resourceProject = new Microsoft.Build.Evaluation.Project(projFilePath);
//add resx file if it doesn't exist in the project
if (resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == path) == null)
{
string designerPath = path.Replace(".resx", ".Designer.cs");
//Add metadata when the resx file has a designer file
resourceProject.AddItem("EmbeddedResource", path);
if (withDesigner)
{
var projectItem = resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == path);
projectItem.SetMetadataValue("Generator", "ResXFileCodeGenerator");
projectItem.SetMetadataValue("LastGenOutput", Path.GetFileName(designerPath));
resourceProject.AddItem("Compile", designerPath);
var designerItem = resourceProject.Items.FirstOrDefault(i => i.EvaluatedInclude == designerPath);
designerItem.SetMetadataValue("AutoGen", "True");
designerItem.SetMetadataValue("DesignTime", "True");
designerItem.SetMetadataValue("DependentUpon", Path.GetFileName(path));
}
resourceProject.Save();
}