按国家名称获取文化

时间:2014-01-22 14:09:59

标签: vb.net

我只有国名,我需要文化。我尝试过地区:

Dim region As New RegionInfo("USA")

有没有办法按国家名称获取文化信息?

1 个答案:

答案 0 :(得分:1)

你可以创建我刚刚创建的这个类(所以没有真正测试过):

public class CountryCultureInfo
{ 
    static CountryCultureInfo()
    {
        countryCultures = new Dictionary<string, HashSet<CultureInfo>>(StringComparer.InvariantCultureIgnoreCase);
        foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            RegionInfo region = new RegionInfo(culture.Name);
            HashSet<CultureInfo> cultures;
            if (!countryCultures.TryGetValue(region.ThreeLetterISORegionName, out cultures))
                cultures = new HashSet<CultureInfo>();
            cultures.Add(culture);
            countryCultures[region.ThreeLetterISORegionName] = cultures;
        }
    }

    private static Dictionary<string, HashSet<CultureInfo>> countryCultures;

    public static HashSet<CultureInfo> GetCultures(string threeLetterISORegionName)
    {
        HashSet<CultureInfo> cultures;
        countryCultures.TryGetValue(threeLetterISORegionName, out cultures);
        return cultures;
    }
}

如果你想在美国获得所有文化:

var allCulturesInUSA = CountryCultureInfo.GetCultures("USA"); // en-US, es-US

或在巴西:

var allCulturesInBrasil = CountryCultureInfo.GetCultures("BRA"); // pt-BR

编辑:抱歉,这是VB.NET版本(忽略了标签):

Public Class CountryCultureInfo
    Shared Sub New()
        countryCultures = New Dictionary(Of String, HashSet(Of CultureInfo))(StringComparer.InvariantCultureIgnoreCase)
        For Each culture As CultureInfo In CultureInfo.GetCultures(CultureTypes.SpecificCultures)
            Dim region As New RegionInfo(culture.Name)
            Dim cultures As HashSet(Of CultureInfo) = Nothing
            If Not countryCultures.TryGetValue(region.ThreeLetterISORegionName, cultures) Then
                cultures = New HashSet(Of CultureInfo)()
            End If
            cultures.Add(culture)
            countryCultures(region.ThreeLetterISORegionName) = cultures
        Next
    End Sub

    Private Shared countryCultures As Dictionary(Of String, HashSet(Of CultureInfo))

    Public Shared Function GetCultures(threeLetterISORegionName As String) As HashSet(Of CultureInfo)
        Dim cultures As HashSet(Of CultureInfo) = Nothing
        countryCultures.TryGetValue(threeLetterISORegionName, cultures)
        Return cultures
    End Function
End Class