C# NET中重写的区域性特定资源的可靠计算
从标题中猜出我的意思可能有点困难,所以让我详细解释一下 我有一个利用资源(resx)进行翻译的应用程序。我有标准的C# NET中重写的区域性特定资源的可靠计算,c#,.net,cultureinfo,resx,resourcemanager,C#,.net,Cultureinfo,Resx,Resourcemanager,从标题中猜出我的意思可能有点困难,所以让我详细解释一下 我有一个利用资源(resx)进行翻译的应用程序。我有标准的Strings.resx文件和所有字符串,以及Strings.xx xx.resx文件在特定的xx区域性中覆盖它们。初始Strings.resx文件具有X字符串数,其中特定于区域性的Strings.xx xx.resx文件的字符串数可能小于或等于X数 我正在尝试编写一个函数,该函数能够可靠地计算该语言中重写了多少字符串,与初始值相比,它可以提供一个不错的整体翻译百分比 例如,我们总共
Strings.resx
文件和所有字符串,以及Strings.xx xx.resx
文件在特定的xx
区域性中覆盖它们。初始Strings.resx
文件具有X
字符串数,其中特定于区域性的Strings.xx xx.resx
文件的字符串数可能小于或等于X
数
我正在尝试编写一个函数,该函数能够可靠地计算该语言中重写了多少字符串,与初始值相比,它可以提供一个不错的整体翻译百分比
例如,我们总共有10个字符串,在es
区域性中有6个字符串。当es-es
用户启动应用程序时,他将收到一条消息,es-es
翻译完成率为60%
到目前为止,我成功地编写了如下代码:
ushort defaultResourceSetCount = 0;
ResourceSet defaultResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-US"), true, true);
if (defaultResourceSet != null) {
defaultResourceSetCount = (ushort) defaultResourceSet.Cast<object>().Count();
}
ushort currentResourceSetCount = 0;
ResourceSet currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, false);
if (currentResourceSet != null) {
currentResourceSetCount = (ushort) currentResourceSet.Cast<object>().Count();
}
if (currentResourceSetCount < defaultResourceSetCount) {
// This is our percentage that we want to calculate and show to user
float translationCompleteness = currentResourceSetCount / (float) defaultResourceSetCount;
}
ushort defaultResourceSetCount=0;
ResourceSet defaultResourceSet=Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo(“en-US”),true,true);
if(defaultResourceSet!=null){
defaultResourceSetCount=(ushort)defaultResourceSet.Cast().Count();
}
ushort currentResourceSetCount=0;
ResourceSet currentResourceSet=Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture,true,false);
如果(currentResourceSet!=null){
currentResourceSetCount=(ushort)currentResourceSet.Cast().Count();
}
如果(currentResourceSetCount
上面的代码可以工作,但有很多方言限制,我想解决。它基本上只适用于在特定区域性中具有特定区域性的非常一般的情况,例如es es
和Strings
文件-如果用户使用其他东西,例如es-UY
,他将从.NET的es-es
,但这对我们的计算不起作用。我可以将GetResourceSet()
中的tryParents
布尔值切换到true
,但我总是会在原始strings
文件中声明的en-US
字符串的形式上得到一个回退,所以即使用户选择了完全不同的文化,我们也总是有100%的翻译进度
因此,基本上,在我们的示例中,Strings.resx
中有10个资源,而Strings.es es.resx
中有6个资源,应该发生以下情况:
- 当使用
文化时,我们获得100%的完成率en-US
- 当使用
文化时,我们获得了100%的完成率,因为它可以追溯到en-GB
,我们已经涵盖了这一点en-US
- 使用
culture时,我们的完成率为60%es
- 当使用
区域性时,我们获得了60%的完成率,因为.NET将es-UY
视为后备策略。请注意,我们没有声明es
,但是声明了es
es
- 使用
时,我们的完成率为0%,即使我们已经讨论过的zh CN
en US
en-US
。另一方面,我想假设任何比Strings.resx
更好的回退都可以被认为是翻译的,因为es-es-es
对于es-UY
用户来说是完全好的,但是en-US
对于zh-CN
来说是不好的。另一方面,en-US
对于en-GB
用户来说是完全合适的
也许我可以以某种方式比较ResourceSet
s和tryParents
集,并比较哪些字符串不同?不过,这需要进行引用比较,因为完全有可能某些字符串在两种不同的语言中具有相同的翻译。有可能吗
欢迎任何建议。这是我对这个问题提出的最好建议:
if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName.Equals("en")) {
return;
}
ResourceSet defaultResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo("en-US"), true, true);
if (defaultResourceSet == null) {
return;
}
HashSet<DictionaryEntry> defaultStringObjects = new HashSet<DictionaryEntry>(defaultResourceSet.Cast<DictionaryEntry>());
if (defaultStringObjects.Count == 0) {
return;
}
ResourceSet currentResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
if (currentResourceSet == null) {
return;
}
HashSet<DictionaryEntry> currentStringObjects = new HashSet<DictionaryEntry>(currentResourceSet.Cast<DictionaryEntry>());
if (currentStringObjects.Count >= defaultStringObjects.Count) {
// Either we have 100% finished translation, or we're missing it entirely and using en-US
HashSet<DictionaryEntry> testStringObjects = new HashSet<DictionaryEntry>(currentStringObjects);
testStringObjects.ExceptWith(defaultStringObjects);
// If we got 0 as final result, this is the missing language
// Otherwise it's just a small amount of strings that happen to be the same
if (testStringObjects.Count == 0) {
currentStringObjects = testStringObjects;
}
}
if (currentStringObjects.Count < defaultStringObjects.Count) {
float translationCompleteness = currentStringObjects.Count / (float) defaultStringObjects.Count;
Console.WriteLine("Do something with translation completeness: " + translationCompleteness);
}
if(CultureInfo.CurrentCulture.TwoLetterIsLanguageName.Equals(“en”)){
返回;
}
ResourceSet defaultResourceSet=Strings.ResourceManager.GetResourceSet(CultureInfo.GetCultureInfo(“en-US”),true,true);
if(defaultResourceSet==null){
返回;
}
HashSet defaultStringObjects=新HashSet(defaultResourceSet.Cast());
如果(defaultStringObjects.Count==0){
返回;
}
ResourceSet currentResourceSet=Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture,true,true);
如果(currentResourceSet==null){
返回;
}
HashSet currentStringObjects=新HashSet(currentResourceSet.Cast());
如果(currentStringObjects.Count>=defaultStringObjects.Count){
//要么我们已经100%完成了翻译,要么我们完全错过了它并使用了en-US
HashSet testStringObjects=新HashSet(currentStringObjects);
testStringObjects.ExceptWith(defaultStringObjects);
//如果最终结果为0,则这是缺少的语言
//否则,它只是一小部分碰巧相同的字符串
if(testStringObjects.Count==0){
currentStringObjects=testStringObjects;
}
}
如果(currentStringObjects.Count
它只需要两个相当不错的假设: