当我们想让我们的应用程序面向所有用户(说不同的语言)时,我们需要一种全球化的技术。
在C#中,我们按如下方式使用ResourceManager:
using System;
using System.Reflection;
using System.Resources;
public class Example
{
public static void Main()
{
// Retrieve the resource.
ResourceManager rm = new ResourceManager("ExampleResources" ,
typeof(Example).Assembly);
string greeting = rm.GetString("Greeting");
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("{0} {1}!", greeting, name);
}
}
// The example produces output similar to the following:
// Enter your name: John
// Hello John!程序集具有两种或两种以上语言资源:
组件
|--Assembly.en-us.resx|--Assembly.zh-cn.resx 然后,我们归档通过更改线程cultureinfo来更改资源以使用不同的资源。
如果应用程序有很多dll(程序集)文件。
我希望应用程序有一个单点(一个资源文件对应一种语言),
我的想法有没有好的解决方案?
在此之前,我只需更改视图(eg.Winform或UserControl)的Language,为相应的语言实现不同的UI。
发布于 2015-08-25 16:23:08
只需使用您描述的方式在C#中构建国际化即可。但是作为构建过程的最后一步,您可以运行Fody.Costura。
这将获取所有不同的.exe并将它们打包到您的应用程序中,这样您就只有一个包含所有内容的dlls文件。
这样做的好处是,您可以按照预期使用C#国际化框架,而不会受到任何干扰,但您仍然可以获得一个可以交付给客户的可执行文件。
发布于 2015-08-25 16:31:13
我发现C#国际化框架非常缺乏,所以我通常会为其他项目的资源和引用制作一个程序集。我从一些工具(DB,excel,textfile)生成的资源文件,并将源数据和资源文件都置于版本控制之下。
MyApp.sln
ResourceProject.csproj
Resources.resx
Resources.ru.resx
Resources.de.resx
Resource.cs
Core.csproj
UI.csproj资源类可以加载所有不同的程序集
namespace MyApp.Resources
{
public static class Resource
{
private static ResourceManager manager;
static Resource()
{
manager = new ResourceManager("MyApp.Resources", Assembly.GetAssembly(typeof(Resource)));
}
public static string GetString(string key, string culture)
{
return GetString(key, new CultureInfo(culture));
}
public static string GetString(string key, CultureInfo culture)
{
return manager.GetString(key, culture);
}
}
}这个简单的类可以通过各种方式进行扩展。在调用程序集中,您可以拥有基于当前UI区域性或线程区域性调用的实用工具类,具体取决于具体情况。
请注意,这完全避开了任何内置的WinForms或WPF i18N方法。
对于GUI:s,您可以创建一个递归转换整个表单的实用程序。查找本身可以/应该扩展为缺少键、备用参数、前缀/名称空间(如果您有数千个键等)的警告。
https://stackoverflow.com/questions/32198800
复制相似问题