CultureInfo.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 objek CultureInfo yang mewakili budaya antarmuka pengguna saat ini yang digunakan oleh Resource Manager untuk mencari sumber daya khusus budaya pada waktu proses.
public:
static property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public:
static property System::Globalization::CultureInfo ^ CurrentUICulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo CurrentUICulture { get; set; }
public static System.Globalization.CultureInfo CurrentUICulture { get; }
static member CurrentUICulture : System.Globalization.CultureInfo with get, set
static member CurrentUICulture : System.Globalization.CultureInfo
Public Shared Property CurrentUICulture As CultureInfo
Public Shared ReadOnly Property CurrentUICulture As CultureInfo
Nilai Properti
Budaya yang digunakan oleh Resource Manager untuk mencari sumber daya khusus budaya pada waktu proses.
Pengecualian
Properti diatur ke null.
Properti diatur ke nama budaya yang tidak dapat digunakan untuk menemukan file sumber daya. Nama file sumber daya hanya dapat menyertakan huruf, angka, tanda hubung, atau garis bawah.
Contoh
Contoh kode berikut menunjukkan cara mengubah CurrentCulture dan CurrentUICulture utas saat ini.
using System;
using System.Globalization;
public class Example0
{
public static void Main()
{
// Display the name of the current culture.
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);
// Change the current culture to th-TH.
CultureInfo.CurrentCulture = new CultureInfo("th-TH", false);
Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);
// Display the name of the current UI culture.
Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name);
// Change the current UI culture to ja-JP.
CultureInfo.CurrentUICulture = new CultureInfo( "ja-JP", false );
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
}
// The example displays the following output:
// CurrentCulture is en-US.
// CurrentCulture is now th-TH.
// CurrentUICulture is en-US.
// CurrentUICulture is now ja-JP.
Imports System.Globalization
Imports System.Threading
Public Module Example2
Public Sub Run()
' Display the name of the current culture.
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name)
' Change the current culture to th-TH.
CultureInfo.CurrentCulture = New CultureInfo("th-TH", False)
Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name)
' Display the name of the current UI culture.
Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name)
' Change the current UI culture to ja-JP.
CultureInfo.CurrentUICulture = New CultureInfo("ja-JP", False)
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name)
End Sub
End Module
' The example displays the following output:
' CurrentCulture is en-US.
' CurrentCulture is now th-TH.
' CurrentUICulture is en-US.
' CurrentUICulture is now ja-JP.
Keterangan
Properti CurrentUICulture adalah properti per alur. Artinya, setiap utas memiliki budaya UI-nya sendiri. Properti ini setara dengan mengambil atau mengatur objek CultureInfo yang ditetapkan ke properti System.Threading.Thread.CurrentThread.CurrentUICulture. Ketika utas dimulai, budaya antarmuka penggunanya awalnya ditentukan sebagai berikut:
Dengan mengambil kultur yang ditentukan oleh properti DefaultThreadCurrentUICulture di domain aplikasi tempat utas dijalankan, jika nilai properti bukan
null.Jika utas adalah utas kumpulan utas yang menjalankan operasi asinkron berbasis tugas, budaya UI-nya ditentukan oleh budaya UI dari utas panggilan. Contoh berikut mengubah budaya UI saat ini menjadi Portugis (Brasil) dan meluncurkan enam tugas, yang masing-masing menampilkan ID utasnya, ID tugasnya, dan budaya UI-nya saat ini. Setiap tugas (dan utas) telah mewarisi kultur antarmuka pengguna dari utas pemanggil.
using System; using System.Collections.Generic; using System.Globalization; using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; public class Example { public static async Task Main() { var tasks = new List<Task>(); Console.WriteLine($"The current UI culture is {Thread.CurrentThread.CurrentUICulture.Name}"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR"); // Change the current UI culture to Portuguese (Brazil). Console.WriteLine($"Current UI culture changed to {Thread.CurrentThread.CurrentUICulture.Name}"); Console.WriteLine($"Application thread is thread {Thread.CurrentThread.ManagedThreadId}"); // Launch six tasks and display their current culture. for (int ctr = 0; ctr <= 5; ctr++) tasks.Add(Task.Run(() => { Console.WriteLine($"UI Culture of task {Task.CurrentId} on thread {Thread.CurrentThread.ManagedThreadId} is {Thread.CurrentThread.CurrentUICulture.Name}"); })); await Task.WhenAll(tasks.ToArray()); } } // The example displays output like the following: // The current UI culture is en-US // Current UI culture changed to pt-BR // Application thread is thread 9 // UI Culture of task 2 on thread 11 is pt-BR // UI Culture of task 1 on thread 10 is pt-BR // UI Culture of task 3 on thread 11 is pt-BR // UI Culture of task 5 on thread 11 is pt-BR // UI Culture of task 6 on thread 11 is pt-BR // UI Culture of task 4 on thread 10 is pt-BRImports System.Globalization Imports System.Threading Module Example1 Public Sub Main() Dim tasks As New List(Of Task) Console.WriteLine("The current UI culture is {0}", Thread.CurrentThread.CurrentUICulture.Name) Thread.CurrentThread.CurrentUICulture = New CultureInfo("pt-BR") ' Change the current UI culture to Portuguese (Brazil). Console.WriteLine("Current UI culture changed to {0}", Thread.CurrentThread.CurrentUICulture.Name) Console.WriteLine("Application thread is thread {0}", Thread.CurrentThread.ManagedThreadId) ' Launch six tasks and display their current culture. For ctr As Integer = 0 To 5 tasks.Add(Task.Run(Sub() Console.WriteLine("Culture of task {0} on thread {1} is {2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.CurrentUICulture.Name) End Sub)) Next Task.WaitAll(tasks.ToArray()) End Sub End Module ' The example displays output like the following: ' The current UI culture is en-US ' Current UI culture changed to pt-BR ' Application thread is thread 9 ' Culture of task 2 on thread 11 is pt-BR ' Culture of task 1 on thread 10 is pt-BR ' Culture of task 3 on thread 11 is pt-BR ' Culture of task 5 on thread 11 is pt-BR ' Culture of task 6 on thread 11 is pt-BR ' Culture of task 4 on thread 10 is pt-BRUntuk informasi selengkapnya, lihat Budaya dan operasi asinkron berbasis tugas.
Dengan memanggil fungsi Windows
GetUserDefaultUILanguage.
Untuk mengubah kultur antarmuka pengguna yang digunakan oleh thread, atur properti Thread.CurrentUICulture ke kultur baru. Jika Anda secara eksplisit mengubah budaya antarmuka pengguna (UI) dari suatu utas dengan cara ini, pengubahan tersebut akan terus berlanjut jika utas tersebut melintasi batas domain aplikasi.
Note
Jika Anda mengatur nilai properti ke objek CultureInfo yang mewakili budaya baru, nilai properti Thread.CurrentThread.CurrentCulture juga berubah.
Dapatkan budaya UI saat ini
Properti CultureInfo.CurrentUICulture adalah pengaturan per utas; artinya, setiap utas dapat memiliki budaya UI sendiri. Anda memperoleh kultur antarmuka pengguna dari utas saat ini dengan mengambil nilai properti CultureInfo.CurrentUICulture, seperti yang diilustrasikan dalam contoh berikut.
using System;
using System.Globalization;
public class Example2
{
public static void Main()
{
CultureInfo culture = CultureInfo.CurrentUICulture;
Console.WriteLine($"The current UI culture is {culture.NativeName} [{culture.Name}]");
}
}
// The example displays output like the following:
// The current UI culture is English (United States) [en-US]
Imports System.Globalization
Module Example3
Public Sub Main()
Dim culture As CultureInfo = CultureInfo.CurrentUICulture
Console.WriteLine("The current UI culture is {0} [{1}]",
culture.NativeName, culture.Name)
End Sub
End Module
' The example displays output like the following:
' The current UI culture is English (United States) [en-US]
Anda juga dapat mengambil nilai budaya UI utas saat ini dari properti Thread.CurrentUICulture.
Secara eksplisit mengatur budaya UI saat ini
Anda dapat mengubah budaya UI saat ini dengan menetapkan CultureInfo objek yang mewakili budaya baru ke CultureInfo.CurrentUICulture properti . Budaya UI saat ini dapat diatur ke budaya tertentu (seperti en-US atau de-DE) atau ke budaya netral (seperti en atau de). Contoh berikut menetapkan budaya UI saat ini ke fr-FR atau Prancis (Prancis).
using System;
using System.Globalization;
public class Example1
{
public static void Main()
{
Console.WriteLine($"The current UI culture: {CultureInfo.CurrentUICulture.Name}");
CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine($"The current UI culture: {CultureInfo.CurrentUICulture.Name}");
}
}
// The example displays output like the following:
// The current UI culture: en-US
// The current UI culture: fr-FR
Imports System.Globalization
Module Example2
Public Sub Main()
Console.WriteLine("The current UI culture: {0}",
CultureInfo.CurrentUICulture.Name)
CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine("The current UI culture: {0}",
CultureInfo.CurrentUICulture.Name)
End Sub
End Module
' The example displays output like the following:
' The current UI culture: en-US
' The current UI culture: fr-FR
Dalam aplikasi multithread, Anda dapat secara eksplisit mengatur budaya UI dari utas apa pun dengan menetapkan objek CultureInfo yang mewakili budaya tersebut ke properti Thread.CurrentUICulture utas. Jika utas yang ingin Anda tetapkan kebudayaannya adalah utas saat ini, Anda dapat menetapkan kebudayaan baru ke properti CultureInfo.CurrentUICulture. Ketika budaya UI utas diatur secara eksplisit, utas tersebut mempertahankan budaya yang sama meskipun melewati batas domain aplikasi dan menjalankan kode di domain aplikasi lain.
Secara implisit mengatur budaya UI saat ini
Ketika sebuah utas, termasuk utas aplikasi utama, dibuat untuk pertama kali, secara default budaya UI saat ini ditetapkan sebagai berikut:
- Dengan menggunakan budaya yang telah ditentukan oleh properti DefaultThreadCurrentUICulture untuk domain aplikasi saat ini apabila nilai properti bukan
null. - Dengan menggunakan budaya default sistem. Pada sistem yang menggunakan sistem operasi Windows, runtime bahasa umum memanggil fungsi Windows
GetUserDefaultUILanguageuntuk mengatur budaya UI saat ini.GetUserDefaultUILanguagemengembalikan budaya UI default yang ditetapkan oleh pengguna. Jika pengguna belum mengatur bahasa UI default, pengguna mengembalikan budaya yang awalnya diinstal pada sistem.
Jika utas melewati batas aplikasi dan menjalankan kode di domain aplikasi lain, budayanya ditentukan dengan cara yang sama seperti utas yang baru dibuat.
Perhatikan bahwa jika Anda menetapkan budaya UI tertentu yang berbeda dari budaya UI yang diinstal sistem atau budaya UI pilihan pengguna, dan aplikasi Anda memulai beberapa utas, budaya UI saat ini dari utas tersebut akan menjadi budaya yang dikembalikan oleh fungsi GetUserDefaultUILanguage, kecuali Anda menetapkan budaya ke properti DefaultThreadCurrentUICulture di domain aplikasi tempat utas dijalankan.
Pertimbangan keamanan
Mengubah budaya utas saat ini memerlukan izin SecurityPermission dengan kumpulan nilai ControlThread.
Perhatian
Memanipulasi utas berbahaya karena kondisi keamanan yang terkait dengan utas. Oleh karena itu, izin ini harus diberikan hanya untuk kode yang dapat dipercaya, dan kemudian hanya seperlunya. Anda tidak dapat mengubah kultur utas dalam kode semi-tepercaya.