本地化.NET;使用 ResourceManager 时的后备语言 [英] Localization .NET; Fallback language when using ResourceManager

查看:81
本文介绍了本地化.NET;使用 ResourceManager 时的后备语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在研究使用 .NET 进行本地化.从本质上讲,我学会了如何自定义表单(使用 Language 和 Localizable 属性),然后相应地更改文化.

Recently I was delving into Localization with .NET. Essentially, I learnt how to customize a form (using the Language and Localizable property) and then change the culture accordingly.

但是,我发现在将我的硬编码英文字符串迁移到自动生成的资源文件中时,并使用 .GetString("Key") -- 好吧,我们只能说它不高兴:P.

However, I found that when migrating my hard coded English strings into the auto-generated resource files, and use .GetString("Key") -- well, let's just say it wasn't happy :P.

我决定制作一组单独的 resx 文件,专门用于硬编码的字符串翻译.他们遵循 [name].[culture-code].resx 的约定/要求.我为每种相关语言制作了这些;例如appstrings.de.resx(德语)和 appstrings.resx(作为不变基线).

I decided to make a separate set of resx files dedicated purely to the hard coded string translations. They followed the convention/requirement of [name].[culture-code].resx. I made of of these for each relevant language; e.g. appstrings.de.resx (For German) and appstrings.resx (as invariant baseline).

为了利用这些新资源,我创建了一个 ResourceManager 和 Resource Set 实例

To utilise these new resources, I created an instance of ResourceManager and Resource Set

Dim resManager As New ResourceManager("LanguageTest.appstrings", Assembly.GetExecutingAssembly)
Dim resSet As ResourceSet = resManager.GetResourceSet(My.Application.UICulture, True, True)

使用

My.Application.ChangeUICulture("de")

<小时>

原始问题

除非在 appstrings.de.resx 中明确定义了 resSet.GetString("Key"),否则它将返回一个空字符串.无论如何我可以让它回退到 appstrings.resx(Key"确实存在的地方),我认为它是默认基线?

Unless the resSet.GetString("Key") is explicitly defined in the appstrings.de.resx, it will return a blank string. Is there anyway I can make it fallback to the appstrings.resx (where "Key" does exist), which I assumed would be the default baseline?

更新

Rhapsody 在下面提出了一个建议,虽然实际的提示本身不起作用,但它实际上激发了一个有趣的点,使用 resManager.GetString("Key") 而不是 resSet.GetString("Key").到目前为止,这似乎没有缺陷.也就是说,返回专用语言文件中存在的值,而缺失"值在通过单个键访问时回退到默认区域性.

Rhapsody made a suggestion below, while the actual tip itself didn't work, it did in-fact spark an interesting point, using resManager.GetString("Key") as opposed to resSet.GetString("Key"). This appears to work without flaw so far. That is, values present in the specialized language file are returned, while 'missing' values fall-back to the default culture when accessed by a single key.

后续问题

剩下的唯一问题是使用 ResourceManger 而非缓存的 ResourceSet 对性能的影响是否会如此有害?

The only remaining issue would be whether the performance impact of using ResourceManger as opposed to a cached ResourceSet will be that detrimental?

推荐答案

您可以尝试为应用程序声明默认文化,如下所示

You can try declaring a default Culture for the application as below

[assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.Satellite)]

此代码声明您的默认文化为 en-US.在您的示例中,如果当前文化是de-DE",它会在de-DE"中查找资源,然后查找父de"文化等等,最后转到定义的文化(en-US)资源文件.请参阅 MSDN,了解 resourceManage 的回退策略.上面的代码通常在 assenblyInfo.cs 中.第二个参数告诉资源管理器资源在主程序集或卫星中的位置.

This code declares that your default culture is en-US. In your example if current culture is "de-DE" it looks for for resource in "de-DE", then looks for the parent "de" culture and so on and Ultimatly goes to the defined culture (en-US) resource file. see MSDN for fallback strategies by resourceManage. The above code normaly goes in the assenblyInfo.cs. The second parameter is telling the resource manager where are the resources located in Main assembly or satellite.

不幸的是,在此解决方案中,您必须使用区域性名称,而不是资源文件名.但是,您可以扩展 RM 以通过文件名使用特定资源,但是选择默认文化并将您的资源文件重命名为该文化的方法更简单,并且您拥有开箱即用的解决方案.

Unfortunatly in this solution you have to use a culture name, Not the resource file name. however you could extend RM to use a specific resource by file name, But its way simpler to pick a default culture and rename your resource file to that culture and you have the solution out of the box.

这篇关于本地化.NET;使用 ResourceManager 时的后备语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆