CultureInfo和ISO 639-3

Bid*_*dou 3 .net c# culture iso cultureinfo

我正在寻找一种CultureInfoISO 639-3语言代码构造对象的方法.我没有在MSDN中找到任何关于它的内容,并试图从所有文化列表中获取它不起作用...

CultureInfo cInfo = CultureInfo.GetCultures(CultureTypes.AllCultures)
                         .FirstOrDefault(r => String.Equals(r.ThreeLetterISOLanguageName, "CCH", StringComparison.CurrentCultureIgnoreCase));
Run Code Online (Sandbox Code Playgroud)

将始终返回null(请注意,"CCH"是ISO-639-3列表中的一种语言).

任何想法都表示赞赏,谢谢!

the*_*man 7

MSDN文档指出的CultureInfo对象只有ISO 639-2 three-letter codeISO 639-1 two-letter code.这意味着您需要某种映射才能将ISO 639-3代码链接到特定CultureInfo实例.

此维基百科页面包含带映射的表.也许您可以剪切并粘贴到XML文件中,并将其用作类库中的嵌入式资源,以便提供映射.或者甚至只是在Dictionary<string,string>某处定义一个静态.

或者,我确信会有一个第三方库可以为你做这件事(虽然我不知道有什么不在我的脑海里).

编辑:

我没有意识到ISO 639-3代码有时只有ISO 639-2代码映射.这里的问题是CultureInfo该类不是为处理ISO 639-3规范而设计的,因此您可能必须找到一个完全不同的CultureInfo第三方实现,它将支持这一点 - 或者自己创建.


Pie*_*erV 5

我也有类似的需要在 ISO 639-2B/T 和 ISO 639-3 格式之间进行转换。我创建了一个 TT4 解决方案,它在编译时生成所有 7K+ 条目的列表。我可以使用字典而不是列表,但我正在搜索多个字段,所以没有太大价值。

从以下位置下载并提取制表符分隔的文本文件:http://www-01.sil.org/iso639-3/download.asp 将其复制到您的项目路径,根据需要重命名。

创建设计时模板文件:https://msdn.microsoft.com/en-us/library/dd820620.aspx

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Microsoft.VisualBasic.dll" #> 
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Microsoft.VisualBasic.FileIO" #>

// Generated code
using System.Collections.Generic;

namespace Foo
{
    // ISO 639-3
    // http://www-01.sil.org/iso639-3/download.asp
    public class ISO_639_3
    {
        // The three-letter 639-3 identifier
        public string Id { get; set; }
        // Equivalent 639-2 identifier of the bibliographic applications code set, if there is one
        public string Part2B { get; set; }
        // Equivalent 639-2 identifier of the terminology applications code set, if there is one
        public string Part2T { get; set; }
        // Equivalent 639-1 identifier, if there is one
        public string Part1 { get; set; }
        // I(ndividual), M(acrolanguage), S(pecial)
        public string Scope { get; set; }
        // A(ncient), C(onstructed), E(xtinct), H(istorical), L(iving), S(pecial)
        public string Language_Type { get; set; }
        // Reference language name
        public string Ref_Name { get; set; }
        // Comment relating to one or more of the columns
        public string Comment { get; set; }

        // Create a list of all known codes
        public static List<ISO_639_3> Create()
        {
            List<ISO_639_3> list = new List<ISO_639_3> {
<# 
    // Setup text parser
    string filename = this.Host.ResolvePath("iso-639-3.tab"); 
    TextFieldParser tfp = new TextFieldParser(filename)
    {
        TextFieldType = FieldType.Delimited,
        Delimiters = new[] { ",", "\t" },
        HasFieldsEnclosedInQuotes = true,
        TrimWhiteSpace = true
    };

    // Read first row as header
    string[] header = tfp.ReadFields();

    // Read rows from file
    // For debugging limit the row count
    //int maxrows = 10;
    int maxrows = int.MaxValue;
    int rowcount = 0;
    string term = "";
    while (!tfp.EndOfData && rowcount < maxrows)
    {
        // Read row of data from the file
        string[] row = tfp.ReadFields();
        rowcount ++;

        // Add "," on all but last line
        term = tfp.EndOfData || rowcount >= maxrows ? "" : ",";

        // Add new item from row data
#>
                new ISO_639_3 { Id = "<#=row[0]#>", Part2B = "<#=row[1]#>", Part2T = "<#=row[2]#>", Part1 = "<#=row[3]#>", Scope = "<#=row[4]#>", Language_Type = "<#=row[5]#>", Ref_Name = "<#=row[6]#>", Comment = "<#=row[7]#>" }<#=term#>
<# 
    } 
#>  
            };
            return list;
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

生成的代码将为包含所有语言的列表创建一个初始值设定项。此文件较大,会减慢编辑速度,编译时间较长,除非需要,否则请保持卸载。片段:

public static List<ISO_639_3> Create()
{
    List<ISO_639_3> list = new List<ISO_639_3> {
        new ISO_639_3 { Id = "aaa", Part2B = "", Part2T = "", Part1 = "", Scope = "I", Language_Type = "L", Ref_Name = "Ghotuo", Comment = "" },
        new ISO_639_3 { Id = "aab", Part2B = "", Part2T = "", Part1 = "", Scope = "I", Language_Type = "L", Ref_Name = "Alumu-Tesu", Comment = "" },
        new ISO_639_3 { Id = "aac", Part2B = "", Part2T = "", Part1 = "", Scope = "I", Language_Type = "L", Ref_Name = "Ari", Comment = "" },
Run Code Online (Sandbox Code Playgroud)

使用生成的列表根据需要进行映射,例如

    public static ISO_639_3 GetISO_639_3(string language)
    {
        // Create list if it does not exist
        if (Program.Default.ISO6393List == null)
        {
            Program.Default.ISO6393List = ISO_639_3.Create();
        }

        // Match the input string type
        ISO_639_3 lang = null;
        if (language.Length > 3 && language.ElementAt(2) == '-')
        {
            // Treat the language as a culture form, e.g. en-us
            CultureInfo cix = new CultureInfo(language);

            // Recursively call using the ISO 639-2 code
            return GetISO_639_3(cix.ThreeLetterISOLanguageName);
        }
        else if (language.Length > 3)
        {
            // Try long form
            lang = Program.Default.ISO6393List.Where(item => item.Ref_Name.Equals(language, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            if (lang != null)
                return lang;
        }
        else if (language.Length == 3)
        {

            // Try 639-3
            lang = Program.Default.ISO6393List.Where(item => item.Id.Equals(language, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            if (lang != null)
                return lang;

            // Try the 639-2/B
            lang = Program.Default.ISO6393List.Where(item => item.Part2B.Equals(language, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            if (lang != null)
                return lang;

            // Try the 639-2/T
            lang = Program.Default.ISO6393List.Where(item => item.Part2T.Equals(language, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            if (lang != null)
                return lang;
        }
        else if (language.Length == 2)
        {
            // Try 639-1
            lang = Program.Default.ISO6393List.Where(item => item.Part1.Equals(language, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            if (lang != null)
                return lang;
        }

        // Not found
        return lang;
    }
Run Code Online (Sandbox Code Playgroud)