cultureinfo跟regioninfo類別

cultureinfo跟regioninfo類別

參考了msdn的cultureinfo跟regioninfo類別後

將內容整理一下


using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Collections.Generic;

/// <summary>
/// RegionCulutreUtility 的摘要描述
/// </summary>
public static class RegionCulutreUtility
{
    //RegionInfo
    //http://msdn.microsoft.com/zh-tw/library/system.globalization.regioninfo.aspx
    //CulutureInfo
    //http://msdn.microsoft.com/zh-tw/library/system.globalization.cultureinfo(VS.80).aspx
    [Flags]
    public enum CulType
    {
        曆法,
        比較字串,
        文化特性類別,
        時間格式,
        數字金額顯示格式,
        曆法清單,
        父文化特性,
        書寫系統
    }
    public static CultureInfo CulInfoTW = new CultureInfo("zh-TW");
    public static RegionInfo RegInfoTW = new RegionInfo("TW");
    public static DataTable DictionaryToDataTable(Dictionary<string, string> InputDir)
    {
        List<string> ColumnName = new List<string>();
        List<string> ColumnValue = new List<string>();
        DataTable dt = new DataTable();
        foreach (KeyValuePair<string, string> temp in InputDir)
        {
            ColumnName.Add(temp.Key);
            ColumnValue.Add(temp.Value);
        }
        foreach (string tempName in ColumnName)
        {
            DataColumn dc = new DataColumn(tempName);
            dt.Columns.Add(dc);
        }
        DataRow dr = dt.NewRow();
        for (int i = 0; i < ColumnValue.Count; i++)
            dr[i] = string.Copy(ColumnValue[i].ToString());
        dt.Rows.Add(dr);
        return dt;
    }
    #region 顯示文化特性    
    public static Dictionary<string, string> GetCultureInfo(string Culture)
    {
        int CultureCode;
        CultureInfo CulInf;
        if (int.TryParse(Culture, out CultureCode) == true)
            CulInf = new CultureInfo(CultureCode);
        else
            CulInf = new CultureInfo(Culture);
        Dictionary<string, string> result = new Dictionary<string, string>();
        result.Add("文化特性名稱", CulInf.DisplayName);
        result.Add("文化特性名稱2", CulInf.Name);
        result.Add("文化特性名稱3", CulInf.NativeName);
        result.Add("文化特性英文名稱", CulInf.EnglishName);
        result.Add("文化特性識別項", CulInf.LCID.ToString());
        result.Add("RFC4646識別項", CulInf.IetfLanguageTag);
        result.Add("ISO639-1二碼代碼", CulInf.TwoLetterISOLanguageName);
        result.Add("ISO639-2三碼代碼", CulInf.ThreeLetterISOLanguageName);
        result.Add("WINDOWS三碼代碼", CulInf.ThreeLetterWindowsLanguageName);
        result.Add("地區設定識別項", CulInf.KeyboardLayoutId.ToString());
        string IsNeutralCulture = (CulInf.IsNeutralCulture == true) ? "是" : "否";
        result.Add("是否表示中性文化", IsNeutralCulture);
        string IsReadOnly = (CulInf.IsReadOnly == true) ? "是" : "否";
        result.Add("是否唯讀", IsReadOnly);
        string UseUserOverride = (CulInf.UseUserOverride == true) ? "是" : "否";
        result.Add("是否使用使用者選定文化特性", UseUserOverride);
        result.Add("語言名稱", CulInf.Parent.DisplayName);
        return result;
    }
    public static Dictionary<string, string> GetTwCultureInfo()
    {
        Dictionary<string, string> result = GetCultureInfo("zh-TW");
        return result;
    }
    public static DataTable GetTwCultureInfoToDataTable()
    {
        return DictionaryToDataTable(GetTwCultureInfo());
    }
    public static DataTable GetCultureInfoToDataTable(string Culture)
    {
        return DictionaryToDataTable(GetCultureInfo(Culture));
    }
    public static object GetTwCulutureInfoObject(CulType CType)
    {        
        return GetCulutureInfoObject("zh-TW", CType);
    }
    public static object GetCulutureInfoObject(string Culture, CulType CType)
    {
        int CultureCode;
        CultureInfo CulInf;
        if (int.TryParse(Culture, out CultureCode) == true)
            CulInf = new CultureInfo(CultureCode);
        else
            CulInf = new CultureInfo(Culture);
        switch (CType.ToString())
        {
            case "曆法": return (object)CulInf.Calendar;
            case "比較字串": return (object)CulInf.CompareInfo;
            case "文化特性類別": return (object)CulInf.CultureTypes;
            case "時間格式": return (object)CulInf.DateTimeFormat;
            case "數字金額顯示格式": return (object)CulInf.NumberFormat;
            case "曆法清單": return (object)CulInf.OptionalCalendars;
            case "父文化特性": return (object)CulInf.Parent;
            case "書寫系統": return (object)CulInf.TextInfo;
            default: return null;
        }
    }
    #endregion
    #region 顯示區域特性
    
    public static Dictionary<string, string> GetRegionInfo(string Region)
    {
        int RegionCode;
        RegionInfo RegionInf;
        if (int.TryParse(Region, out RegionCode) == true)
            RegionInf = new RegionInfo(RegionCode);
        else
            RegionInf = new RegionInfo(Region);
        Dictionary<string, string> result = new Dictionary<string, string>();
        result.Add("國家全名", RegionInf.DisplayName);
        result.Add("國家全名英文", RegionInf.EnglishName);
        result.Add("國家全名母語", RegionInf.NativeName);
        result.Add("貨幣符號", RegionInf.CurrencySymbol);
        result.Add("貨幣英文名稱", RegionInf.CurrencyEnglishName);
        result.Add("貨幣母語名稱", RegionInf.CurrencyNativeName);
        string IsMetric = (RegionInf.IsMetric == true) ? "是" : "否";
        result.Add("是否使用公制", IsMetric);
        result.Add("識別碼", RegionInf.GeoId.ToString());
        result.Add("兩碼區域代碼", RegionInf.Name);
        result.Add("ISO3166三碼區域碼", RegionInf.ThreeLetterISORegionName);
        result.Add("Windows三碼區域碼", RegionInf.ThreeLetterWindowsRegionName);
        result.Add("ISO3166二碼區域碼", RegionInf.TwoLetterISORegionName);
        return result;        
    }
    public static Dictionary<string, string> GetTwRegionInfo()
    {
        Dictionary<string, string> result = GetRegionInfo("TW");
        return result;
    }
    public static DataTable GetTwRegionInfoToDataTable()
    {
        return DictionaryToDataTable(GetTwRegionInfo());
    }
    public static DataTable GetRegionInfoToDataTable(string Region)
    {
        return DictionaryToDataTable(GetRegionInfo(Region));
    }
    #endregion
}