Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NET中重写的区域性特定资源的可靠计算_C#_.net_Cultureinfo_Resx_Resourcemanager - Fatal编程技术网

C# NET中重写的区域性特定资源的可靠计算

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数 我正在尝试编写一个函数,该函数能够可靠地计算该语言中重写了多少字符串,与初始值相比,它可以提供一个不错的整体翻译百分比 例如,我们总共

从标题中猜出我的意思可能有点困难,所以让我详细解释一下

我有一个利用资源(resx)进行翻译的应用程序。我有标准的
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个资源,应该发生以下情况:

  • 当使用
    en-US
    文化时,我们获得100%的完成率
  • 当使用
    en-GB
    文化时,我们获得了100%的完成率,因为它可以追溯到
    en-US
    ,我们已经涵盖了这一点
  • 使用
    es
    culture时,我们的完成率为60%
  • 当使用
    es-UY
    区域性时,我们获得了60%的完成率,因为.NET将
    es
    视为后备策略。请注意,我们没有声明
    es
    ,但是声明了
    es
  • 使用
    zh CN
    时,我们的完成率为0%,即使我们已经讨论过的
    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
它只需要两个相当不错的假设:

  • 当前区域性资源集的资源不能超过默认(en-US)cul