是否可以从代码中生成卫星装配?

时间:2016-11-08 10:54:05

标签: c# .net localization globalization satellite-assembly

我即将详细阐述简化翻译工具的解决方案。因此,我目前尝试从我的代码中自动编译一个Satellite Assembly。

所以我想要的是取代以下命令的手动运行:

AL.exe /culture:de /out:de\TestResource.resources.dll /embed:TestResource.de.resources

到目前为止,我已经测试了生成.dll文件,该文件有效。但是嵌入/链接如下所示的资源并没有任何影响,但扩展了dll的大小。很明显它就在那里但不能用,就好像最终的dll是卫星装配一样。

    static void Main(string[] args)
    {
        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        CompilerParameters parameters = new CompilerParameters();

        parameters.GenerateExecutable = false;
        parameters.OutputAssembly = "./output/satellite_test.dll";
        parameters.EmbeddedResources.Add(@"./TestResource.en.resources");
        parameters.LinkedResources.Add(@"./TestResource.de.resources");

        CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, "");
    }

有没有办法以编程方式生成一个dll,它只包含一种语言的本地化资源,以便它可用作卫星装配?

1 个答案:

答案 0 :(得分:3)

最后,我设法从Code生成卫星装配。

以下代码生成适当的资源文件:

// Already the resourcefilename has to match the 
// exact namespacepath of the original resourcename.
var resourcefileName = @"TranslationTest.Resources.TestResource.de.resources";

// File has to be a .resource file. (ResourceWriter instead of ResXResourceWriter)
// .resx not working and has to be converted into .resource file.
using (var resourceWriter = new ResourceWriter(resourcefileName))
{
    resourceWriter.AddResource("testtext", "Language is german!!");
}

使用此资源文件有一些必要的编译:

CompilerParameters parameters = new CompilerParameters();

// Newly created assembly has to be a dll.
parameters.GenerateExecutable = false;

// Filename has to be like the original resourcename. Renaming afterwards does not work.
parameters.OutputAssembly = "./de/TranslationTest.resources.dll";

// Resourcefile has to be embedded in the new assembly.
parameters.EmbeddedResources.Add(resourcefileName);

最后编译程序集有一些必需的代码,必须编译成:

// Culture information has to be part of the newly created assembly.
var assemblyAttributesAsCode = @"
    using System.Reflection; 
    [assembly: AssemblyCulture(""de"")]";

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerResults results = codeProvider.CompileAssemblyFromSource(
    parameters, 
    assemblyAttributesAsCode
);