條列 CultureInfo 以及 Region 等資訊

Windows 環境中支援了多種 Cultures/Regions 設定,在 .NET Framework 中要如何取出或辨識呢?基本上,只需要使用 CultureInfo 類別就可以了...

Windows 環境中支援了多種 Cultures/Regions 設定,在 .NET Framework 中要如何取出或辨識呢?基本上,只需要使用 CultureInfo 類別就可以了。

不過若要條列這些 Cultures,你必須先指定使用何種 CultureType (括號中的數字是在我的電腦上找到的數目):

  • AllCultures (226)
  • FrameworkCultures (203)
  • InstalledWin32Cultures (123)
  • NeutralCultures (69)
  • ReplacementCultures (0)
  • SpecificCultures (157 - 這個數字和 NeutralCultures 的數字加起來等於 AllCultures 的數字)
  • UserCustomCulture (0)
  • WindowsOnlyCultures (23) - 在這個類別中的 Cultures 中,所有的 DisplayName 都已翻譯成中文

要把指定的 Cultures 條列出來,請使用以下的程式:

.aspx

    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Font-Names="Arial"
            Font-Size="10px">
            <asp:ListItem Value="-1">Choose one ...</asp:ListItem>
            <asp:ListItem Value="0">All Cultures</asp:ListItem>
            <asp:ListItem Value="1">Framework Cultures</asp:ListItem>
            <asp:ListItem Value="2">Installed Win32 Cultures</asp:ListItem>
            <asp:ListItem Value="3">Neutral Cultures</asp:ListItem>
            <asp:ListItem Value="4">Replacement Cultures</asp:ListItem>
            <asp:ListItem Value="5">Specific Cultures</asp:ListItem>
            <asp:ListItem Value="6">User Custom Cultures</asp:ListItem>
            <asp:ListItem Value="7">Windows Only Cultures</asp:ListItem>
        </asp:DropDownList><br />
        <asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="10px"></asp:Label><br />
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" BackColor="White"
            BorderColor="#E7E7FF" BorderStyle='None' BorderWidth='1px' CellPadding='3' Font-Names='Verdana'
            Font-Size="10px" GridLines="Horizontal" PageSize="15">
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <AlternatingRowStyle BackColor="#F7F7F7" />
        </asp:GridView>
        &nbsp;</div>
    </form>

.aspx.vb

Imports System.Globalization
Imports System.Threading
Imports System.Data
Imports System.Data.SqlClient

Partial Class Ch1016_CultureInfo
    Inherits System.Web.UI.Page

    Enum CulType As Integer
        AllCultures = 0
        FrameworkCultures = 1
        InstalledWin32Cultures = 2
        NeutralCultures = 3
        ReplacementCultures = 4
        SpecificCultures = 5
        UserCustomCulture = 6
        WindowsOnlyCultures = 7
    End Enum

    Protected Function dvCulInfo(ByVal culs() As CultureInfo, Optional ByVal sortField As String = "LCID") As DataView
        Dim dt As New DataTable
        dt.Columns.Add("LCID", GetType(Integer))
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("DisplayName", GetType(String))
        dt.Columns.Add("NativeName", GetType(String))
        dt.Columns.Add("RegionName", GetType(String))
        dt.Columns.Add("CurrencySymbol", GetType(String))
        For Each cul As CultureInfo In culs
            If cul.IsNeutralCulture Then
                dt.Rows.Add(cul.LCID, cul.Name, cul.DisplayName, cul.NativeName, "(Neutral)", "(N/A)")
            ElseIf cul.LCID = 127 Then 'Invariant Culture
                dt.Rows.Add(cul.LCID, cul.Name, cul.DisplayName, cul.NativeName, "(Invariant)", "(N/A)")
            Else
                Dim region As New RegionInfo(cul.LCID)
                dt.Rows.Add(cul.LCID, cul.Name, cul.DisplayName, cul.NativeName, region.DisplayName, region.CurrencySymbol)
            End If
        Next
        Label1.Text = dt.Rows.Count.ToString & " items found."
        Dim dv As New DataView(dt)
        dv.Sort = sortField
        Return dv
    End Function

    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.AllCultures), e.SortExpression)
        Select Case DropDownList1.SelectedValue
            Case CulType.AllCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.AllCultures), e.SortExpression)
            Case CulType.FrameworkCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.FrameworkCultures), e.SortExpression)
            Case CulType.InstalledWin32Cultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures), e.SortExpression)
            Case CulType.NeutralCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.NeutralCultures), e.SortExpression)
            Case CulType.ReplacementCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.ReplacementCultures), e.SortExpression)
            Case CulType.SpecificCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.SpecificCultures), e.SortExpression)
            Case CulType.UserCustomCulture
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.UserCustomCulture), e.SortExpression)
            Case CulType.WindowsOnlyCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.WindowsOnlyCultures), e.SortExpression)
        End Select
        GridView1.DataBind()
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Select Case DropDownList1.SelectedValue
            Case CulType.AllCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.AllCultures))
            Case CulType.FrameworkCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.FrameworkCultures))
            Case CulType.InstalledWin32Cultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures))
            Case CulType.NeutralCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.NeutralCultures))
            Case CulType.ReplacementCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.ReplacementCultures))
            Case CulType.SpecificCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            Case CulType.UserCustomCulture
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.UserCustomCulture))
            Case CulType.WindowsOnlyCultures
                GridView1.DataSource = dvCulInfo(CultureInfo.GetCultures(CultureTypes.WindowsOnlyCultures))
        End Select
        GridView1.DataBind()
    End Sub
End Class

使用這個程式,可以把 Windows 中可使用的各個 Culture/Region 的詳細資料列出來,包括 LCID、Name、DisplayName、NativeNmae... 等等,當你要做 Globalization 時可以做為參考。我已經順便將排序做好了,看的時候比較方便。


Dev 2Share @ 點部落