How to create multi language using Resource Manager and Culture Info in C#
By FoxLearn 11/22/2024 1:51:27 PM 13.47K
Implementing Multi-Language Support with Resource Manager and CultureInfo in C#
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CultureExample" and then click OK
Drag and drop the DataGridView, Label and Button controls from the Visual Toolbox onto your form designer, then design your form as shown below.
Create a new dataset, then add a Language table to your dataset as shown below.
Language(ID, English, German)
The btnSave_Click
method saves language-specific data into both an XML file and resource files.
private void btnSave_Click(object sender, EventArgs e) { // Save data to XML file appData.WriteXml(string.Format("{0}/data.xml", Application.StartupPath)); // Create ResourceWriter instances for English and German ResourceWriter ren = new ResourceWriter(Application.StartupPath + "/resource.en-US.resources"); ResourceWriter rde = new ResourceWriter(Application.StartupPath + "/resource.de-DE.resources"); // Iterate through language rows to save data foreach (AppData.LanguagesRow row in appData.Languages.Rows) { ren.AddResource(row.ID, row.English); rde.AddResource(row.ID, row.German); } // Generate and close resource files ren.Generate(); ren.Close(); rde.Generate(); rde.Close(); MessageBox.Show("Successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); }
The appData.WriteXml
method saves the application data, ensuring persistence across sessions.
Use ResourceWriter to create language entries in .resources files for each supported culture
The Form1_Load
method initializes the application by loading resources based on the user's culture.
private void Form1_Load(object sender, EventArgs e) { // Read data from XML file appData.ReadXml(string.Format("{0}/data.xml", Application.StartupPath)); // Set culture to German CultureInfo ci = new CultureInfo("de-DE"); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; // Load resource file ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("resource", Application.StartupPath, null); // Retrieve a localized string and update the UI if (rm.GetString("0001") != null) lblMessage.Text = rm.GetString("0001"); }
The culture (de-DE
) is set for the current thread to determine the language used for resources.
Thread.CurrentThread.CurrentUICulture
ensures that UI components use the specified culture.
The ResourceManager.CreateFileBasedResourceManager
loads the appropriate resource files dynamically at runtime.
The rm.GetString
method retrieves the localized string using its key.
Users can enter language-specific text in the application, then click the Save button, the text is saved to XML and .resources
files.
The application dynamically creates .resources
files for each language.
The UI text, such as the label lblMessage
, is updated based on the selected culture.
This article walks you through implementing a multi-language Windows Forms application using resource files in C#. This implementation provides a robust foundation for multi-language support in Windows Forms applications. By leveraging ResourceManager
, CultureInfo
, and XML files, developers can create scalable and dynamic localization solutions.
VIDEO TUTORIAL
- How to make a File Explorer in C#
- How to Load selected columns data in DataGridView in C#
- How to use Timer control in C#
- How to Search DataGridView by using TextBox in C#
- How to read .rtf file in C#
- How to read text file (*.txt) in C#
- How to make an Alarm clock in C#
- How to Save and Retrieve Image from SQL database in C#