Thread.CurrentUICulture Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur budaya saat ini yang digunakan oleh Resource Manager untuk mencari sumber daya khusus budaya pada waktu proses.
public:
property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public System.Globalization.CultureInfo CurrentUICulture { get; set; }
member this.CurrentUICulture : System.Globalization.CultureInfo with get, set
Public Property CurrentUICulture As CultureInfo
Nilai Properti
Objek yang mewakili budaya saat ini.
Pengecualian
Properti diatur ke null.
Properti diatur ke nama budaya yang tidak dapat digunakan untuk menemukan file sumber daya. Nama file sumber daya hanya boleh menyertakan huruf, angka, tanda hubung, atau garis bawah.
.NET Core dan .NET 5+ saja: Membaca atau menulis budaya utas dari utas lain tidak didukung.
Contoh
Contoh berikut menentukan apakah bahasa budaya UI utas saat ini adalah bahasa Prancis. Jika tidak, itu menetapkan budaya UI dari utas saat ini ke Bahasa Inggris (Amerika Serikat).
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
// Change the current culture if the language is not French.
CultureInfo current = Thread.CurrentThread.CurrentUICulture;
if (current.TwoLetterISOLanguageName != "fr") {
CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = newCulture;
// Make current UI culture consistent with current culture.
Thread.CurrentThread.CurrentCulture = newCulture;
}
Console.WriteLine("The current UI culture is {0} [{1}]",
Thread.CurrentThread.CurrentUICulture.NativeName,
Thread.CurrentThread.CurrentUICulture.Name);
Console.WriteLine("The current culture is {0} [{1}]",
Thread.CurrentThread.CurrentUICulture.NativeName,
Thread.CurrentThread.CurrentUICulture.Name);
}
}
// The example displays the following output:
// The current UI culture is English (United States) [en-US]
// The current culture is English (United States) [en-US]
open System.Globalization
open System.Threading
// Change the current culture if the language is not French.
let current = Thread.CurrentThread.CurrentUICulture
if current.TwoLetterISOLanguageName <> "fr" then
let newCulture = CultureInfo.CreateSpecificCulture "en-US"
Thread.CurrentThread.CurrentUICulture <- newCulture
// Make current UI culture consistent with current culture.
Thread.CurrentThread.CurrentCulture <- newCulture
printfn
$"The current UI culture is {Thread.CurrentThread.CurrentUICulture.NativeName} [{Thread.CurrentThread.CurrentUICulture.Name}]"
printfn
$"The current culture is {Thread.CurrentThread.CurrentUICulture.NativeName} [{Thread.CurrentThread.CurrentUICulture.Name}]"
// The example displays the following output:
// The current UI culture is English (United States) [en-US]
// The current culture is English (United States) [en-US]
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
' Change the current culture if the language is not French.
Dim current As CultureInfo = Thread.CurrentThread.CurrentUICulture
If current.TwoLetterISOLanguageName <> "fr" Then
Dim newCulture As CultureInfo = CultureInfo.CreateSpecificCulture("en-US")
Thread.CurrentThread.CurrentUICulture = newCulture
' Make current UI culture consistent with current culture.
Thread.CurrentThread.CurrentCulture = newCulture
End If
Console.WriteLine("The current UI culture is {0} [{1}]",
Thread.CurrentThread.CurrentUICulture.NativeName,
Thread.CurrentThread.CurrentUICulture.Name)
Console.WriteLine("The current culture is {0} [{1}]",
Thread.CurrentThread.CurrentUICulture.NativeName,
Thread.CurrentThread.CurrentUICulture.Name)
End Sub
End Module
' The example displays output like the following:
' The current UI culture is English (United States) [en-US]
' The current culture is English (United States) [en-US]
Contoh kode berikut menunjukkan pernyataan utas yang memungkinkan antarmuka pengguna Formulir Windows ditampilkan dalam budaya yang diatur di Panel Kontrol. Kode tambahan diperlukan.
using System;
using System.Threading;
using System.Windows.Forms;
class UICulture : Form
{
public UICulture()
{
// Set the user interface to display in the
// same culture as that set in Control Panel.
Thread.CurrentThread.CurrentUICulture =
Thread.CurrentThread.CurrentCulture;
// Add additional code.
}
static void Main()
{
Application.Run(new UICulture());
}
}
open System.Threading
open System.Windows.Forms
type UICulture() =
inherit Form()
do
// Set the user interface to display in the
// same culture as that set in Control Panel.
Thread.CurrentThread.CurrentUICulture <- Thread.CurrentThread.CurrentCulture
// Add additional code.
new UICulture() |> Application.Run
Imports System.Threading
Imports System.Windows.Forms
Public Class UICulture : Inherits Form
Sub New()
' Set the user interface to display in the
' same culture as that set in Control Panel.
Thread.CurrentThread.CurrentUICulture = _
Thread.CurrentThread.CurrentCulture
' Add additional code.
End Sub
Shared Sub Main()
Application.Run(New UICulture())
End Sub
End Class
Keterangan
Budaya UI menentukan sumber daya yang dibutuhkan aplikasi untuk mendukung input dan output pengguna, dan secara default sama dengan budaya sistem operasi. CultureInfo Lihat kelas untuk mempelajari tentang nama dan pengidentifikasi budaya, perbedaan antara budaya invarian, netral, dan spesifik, dan cara informasi budaya memengaruhi utas dan domain aplikasi. CultureInfo.CurrentUICulture Lihat properti untuk mempelajari bagaimana budaya UI default utas ditentukan.
Penting
Properti CurrentUICulture tidak berfungsi dengan andal saat digunakan dengan utas apa pun selain utas saat ini. Dalam .NET Framework, membaca properti dapat diandalkan, meskipun mengaturnya untuk utas selain utas saat ini tidak. Pada .NET Core, dilemparkan InvalidOperationException jika utas mencoba membaca atau menulis CurrentUICulture properti pada utas yang berbeda. Kami menyarankan agar Anda menggunakan CultureInfo.CurrentUICulture properti untuk mengambil dan mengatur budaya saat ini.
Yang CultureInfo dikembalikan oleh properti ini bisa menjadi budaya netral. Budaya netral tidak boleh digunakan dengan metode pemformatan seperti String.Format(IFormatProvider, String, Object[]), , DateTime.ToString(String, IFormatProvider)dan Convert.ToString(Char, IFormatProvider). CultureInfo.CreateSpecificCulture Gunakan metode untuk mendapatkan budaya tertentu, atau gunakan CurrentCulture properti .