CultureInfo 类
命名空间:
System.Globalization
程序集:
System.Globalization.dll
提供有关特定区域性(对于非托管代码开发,则称为“区域设置”)的信息。 这些信息包括区域性的名称、书写系统、使用的日历、字符串的排序顺序以及对日期和数字的格式化设置。
LCID
LCID,全称 Locale Identifier,区域设置标识符,用于确定特定地区和语言的区域设置
使用LCID是为了标识不同地区和语言的代码。它是Locale Identifier(区域设置标识符)的缩写,用于表示不同地区和语言的组合。
每个LCID都是一个32位的无符号整数,由语言ID和子语言ID组成。语言ID是指一种语言,而子语言ID指的是地区或区域设置。
在Windows操作系统中,LCID被广泛用于识别和设置地区设置,如日期和时间格式、货币单位、数字格式、键盘布局等。
本文演示打印显示所有区域信息到表格中.
新建Windows应用程序CultureDemo
FormCulture窗体设计如图:
窗体设计器代码如下:
FormCulture.Designer.cs文件
namespace CultureDemo
{
partial class FormCulture
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgvCultureList = new System.Windows.Forms.DataGridView();
this.rtxtMessage = new System.Windows.Forms.RichTextBox();
this.Column8 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column7 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dgvCultureList)).BeginInit();
this.SuspendLayout();
//
// dgvCultureList
//
this.dgvCultureList.AllowUserToAddRows = false;
this.dgvCultureList.AllowUserToDeleteRows = false;
this.dgvCultureList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvCultureList.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column8,
this.Column1,
this.Column2,
this.Column3,
this.Column4,
this.Column5,
this.Column6,
this.Column7});
this.dgvCultureList.Location = new System.Drawing.Point(12, 12);
this.dgvCultureList.Name = "dgvCultureList";
this.dgvCultureList.ReadOnly = true;
this.dgvCultureList.RowHeadersWidth = 25;
this.dgvCultureList.RowTemplate.Height = 23;
this.dgvCultureList.Size = new System.Drawing.Size(1083, 331);
this.dgvCultureList.TabIndex = 0;
this.dgvCultureList.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvCultureList_CellContentClick);
this.dgvCultureList.CellEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvCultureList_CellEnter);
//
// rtxtMessage
//
this.rtxtMessage.Location = new System.Drawing.Point(12, 364);
this.rtxtMessage.Name = "rtxtMessage";
this.rtxtMessage.ReadOnly = true;
this.rtxtMessage.Size = new System.Drawing.Size(1083, 299);
this.rtxtMessage.TabIndex = 1;
this.rtxtMessage.Text = "";
//
// Column8
//
this.Column8.HeaderText = "序号";
this.Column8.Name = "Column8";
this.Column8.ReadOnly = true;
this.Column8.Width = 60;
//
// Column1
//
this.Column1.HeaderText = "LCID";
this.Column1.Name = "Column1";
this.Column1.ReadOnly = true;
this.Column1.Width = 70;
//
// Column2
//
this.Column2.HeaderText = "Name";
this.Column2.Name = "Column2";
this.Column2.ReadOnly = true;
//
// Column3
//
this.Column3.HeaderText = "DisplayName";
this.Column3.Name = "Column3";
this.Column3.ReadOnly = true;
this.Column3.Width = 120;
//
// Column4
//
this.Column4.HeaderText = "EnglishName";
this.Column4.Name = "Column4";
this.Column4.ReadOnly = true;
this.Column4.Width = 120;
//
// Column5
//
this.Column5.HeaderText = "NativeName";
this.Column5.Name = "Column5";
this.Column5.ReadOnly = true;
this.Column5.Width = 140;
//
// Column6
//
this.Column6.HeaderText = "Calendar";
this.Column6.Name = "Column6";
this.Column6.ReadOnly = true;
this.Column6.Width = 250;
//
// Column7
//
this.Column7.HeaderText = "TextInfo";
this.Column7.Name = "Column7";
this.Column7.ReadOnly = true;
this.Column7.Width = 220;
//
// FormCulture
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1107, 692);
this.Controls.Add(this.rtxtMessage);
this.Controls.Add(this.dgvCultureList);
this.Name = "FormCulture";
this.Text = "区域与文化Culture";
this.Load += new System.EventHandler(this.FormCulture_Load);
((System.ComponentModel.ISupportInitialize)(this.dgvCultureList)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dgvCultureList;
private System.Windows.Forms.RichTextBox rtxtMessage;
private System.Windows.Forms.DataGridViewTextBoxColumn Column8;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
private System.Windows.Forms.DataGridViewTextBoxColumn Column5;
private System.Windows.Forms.DataGridViewTextBoxColumn Column6;
private System.Windows.Forms.DataGridViewTextBoxColumn Column7;
}
}
相关程序FormCulture如下:
FormCulture.cs文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CultureDemo
{
public partial class FormCulture : Form
{
public FormCulture()
{
InitializeComponent();
/* LCID,全称 Locale Identifier,区域设置标识符,用于确定特定地区和语言的区域设置
* 使用LCID是为了标识不同地区和语言的代码。它是Locale Identifier(区域设置标识符)的缩写,用于表示不同地区和语言的组合。
* 每个LCID都是一个32位的无符号整数,由语言ID和子语言ID组成。语言ID是指一种语言,而子语言ID指的是地区或区域设置。
* 在Windows操作系统中,LCID被广泛用于识别和设置地区设置,如日期和时间格式、货币单位、数字格式、键盘布局等。
*/
}
private void FormCulture_Load(object sender, EventArgs e)
{
dgvCultureList.Rows.Clear();
CultureInfo[] cultureInfos = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
for (int i = 0; i < cultureInfos.Length; i++)
{
int lcid = cultureInfos[i].LCID;
string name = cultureInfos[i].Name;
string displayName = cultureInfos[i].DisplayName;
string englishName = cultureInfos[i].EnglishName;
string nativeName = cultureInfos[i].NativeName;
Calendar calendar = cultureInfos[i].Calendar;
TextInfo textInfo = cultureInfos[i].TextInfo;
string strTextInfo = $"LCID:{textInfo.LCID},CultureName:{textInfo.CultureName},ANSICodePage:{textInfo.ANSICodePage}";
dgvCultureList.Rows.Add(i + 1, lcid, name, displayName, englishName, nativeName, calendar.GetType(), strTextInfo);
}
}
private void dgvCultureList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
rtxtMessage.Clear();
if (e.RowIndex < 0 || e.ColumnIndex < 0)
{
return;
}
CultureInfo cultureInfo = new CultureInfo(Convert.ToString(dgvCultureList[2, e.RowIndex].Value));
Calendar calendar = cultureInfo.Calendar;
string calendarDescription = "";
if (calendar.GetType() == typeof(GregorianCalendar))
{
calendarDescription = "格里高利日历【公历】";
}
else
{
calendarDescription = calendar.GetType().Name;
}
TextInfo textInfo = cultureInfo.TextInfo;
string strTextInfo = $" LCID:{textInfo.LCID},\n CultureName:{textInfo.CultureName},\n ANSICodePage:{textInfo.ANSICodePage}";
rtxtMessage.AppendText($@"LCID:{cultureInfo.LCID},
Name:{cultureInfo.Name},
DisplayName:{cultureInfo.DisplayName},
EnglishName:{cultureInfo.EnglishName},
NativeName:{cultureInfo.NativeName},
Calendar:{calendar.GetType()},
日历描述:{calendarDescription}
TextInfo:
{strTextInfo}");
}
private void dgvCultureList_CellEnter(object sender, DataGridViewCellEventArgs e)
{
dgvCultureList_CellContentClick(null, e);
}
}
}
运行如图:
阿拉伯语