这是我对本地化的第一次努力,我读的越多,我就越困惑 . 说实话,我只是在寻找答案 . 所以这是我得到的错误:
未处理的异常:System.Resources.MissingManifestResourceException:找不到适合指定文化或中性文化的任何资源 . 确保在编译时将“MeetnGreetSf.Resources.AppResources.resources”正确嵌入或链接到程序集“MeetnGreetSf”中,或者所有所需的附属程序集都是可加载和完全签名的 . 发生了
这是 class :
public class TranslateHelper
{
readonly CultureInfo ci;
private string ResourceId = "MeetnGreetSf.Resources.AppResources";
private ResourceManager resmgr;
private CultureInfo cui = new CultureInfo("es-ES");
public TranslateHelper()
{
//ResourceId += "." + cui.Name;
resmgr = new ResourceManager(ResourceId, typeof(AppResource).GetTypeInfo().Assembly);
}
public string Translate(string Name)
{
var translation = resmgr.GetString(Name, ci);
return translation;
}
}
我认为我很接近它将.resources附加到文件而不是.resx?
在此先感谢您的帮助 .
忘了包括翻译类的使用方式 .
public Colors()
{
colorInfo = new ObservableCollection<ColorType>();
t = new TranslateHelper();
this.GenerateColors();
}
private void GenerateColors()
{
colorInfo.Add(new ColorType(t.Translate("Aqua"), Color.Aqua));
colorInfo.Add(new ColorType(t.Translate("Black"), Color.Black));
colorInfo.Add(new ColorType(t.Translate("Blue"), Color.Blue));
colorInfo.Add(new ColorType(t.Translate("Gray"), Color.Gray));
colorInfo.Add(new ColorType(t.Translate("Green"), Color.Green));
colorInfo.Add(new ColorType(t.Translate("Lime"), Color.Lime));
colorInfo.Add(new ColorType(t.Translate("Maroon"), Color.Maroon));
colorInfo.Add(new ColorType(t.Translate("Navy"), Color.Navy));
colorInfo.Add(new ColorType(t.Translate("Olive"), Color.Olive));
colorInfo.Add(new ColorType(t.Translate("Purple"), Color.Purple));
colorInfo.Add(new ColorType(t.Translate("Red"), Color.Red));
colorInfo.Add(new ColorType(t.Translate("Silver"), Color.Silver));
colorInfo.Add(new ColorType(t.Translate("Teal"), Color.Teal));
colorInfo.Add(new ColorType(t.Translate("White"), Color.White));
colorInfo.Add(new ColorType(t.Translate("Yellow"), Color.Yellow));
}
}
1 回答
请注意,我是Visual Studio的商业本地化工具的作者(为了完全公开) . 您应该调查“System.Threading.Thread.CurrentThread.CurrentUICulture”,这通常是.NET中所有翻译的内容(用于处理本地化资源) . 但是,如何处理它的机制可能会因项目类型而异 . 现在通常不需要你现在这样做的方式 . 它通常比那更容易(不需要专门的课程) . 以下是一个简单的控制台应用程序的基本过程(同样,事情可能因项目类型而异) .
创建一个名为"TestApp"的新控制台应用程序
打开项目的属性(例如,在解决方案资源管理器中右键单击它并选择"Properties"),然后单击属性窗口中的"Resources"选项卡 . 现在单击屏幕中间显示的按钮"This project does not contain a default resource file. Click here to create one" . 这会将"Resources.resx"添加到您的解决方案中(在解决方案资源管理器中的"Properties"文件夹下),它将在".resx"编辑器中自动打开 .
在".resx"编辑器中添加一个名为"MyString"的字符串 . 分配"This is my default string"(或其他) .
在您要使用字符串的代码中,添加"using TestApp.Properties"语句(继续阅读) . 您现在可以通过静态属性"Resources.MyString"访问该字符串 . 这在"Resources.Designer.cs"中声明,这是由VS在上面的步骤2中自动创建的"code-behind"文件(即,它是解决方案资源管理器中的"Resources.resx"的子节点) . VS为您查找此文件(为上面3中添加的每个字符串向"TestApp.Properties"名称空间添加新的静态属性) . 请注意,这些字符串称为"strongly typed resources" .
将新的".resx"文件添加到项目中并将其命名为"Resources.es.resx" . 这将包含西班牙语字符串 . 创建后,将其在解决方案资源管理器中拖到"Properties"文件夹(因此它现在与"Resources.resx"在同一文件夹中 - 所有本地化的".resx"文件必须与"Resources.resx"在同一文件夹中) .
双击"Resources.es.resx"并向其添加"MyString" . 将其文本设置为"This is my Spanish string" .
如果要更改代码中的语言以获取字符串的西班牙语版本,只需将代码中的"System.Threading.Thread.CurrentThread.CurrentUICulture"属性设置为"es" . 现在每当您引用"Resources.MyString"时,将返回该字符串的西班牙语版本(自动) . IOW,它遵循当前设置的"System.Threading.Thread.CurrentThread.CurrentUICulture" . 您需要(例如,当用户从您的GUI中单击他们想要的语言时),并且所有强类型资源(通常只是字符串)将自动采用新语言 .
这是如何工作的?下面的段落是我曾经发送给我的客户的一段摘录 . 它应该足以帮助您入门(请注意,对于上面的示例,下面对"YourApp"的引用将更改为"TestApp"):