我已经阅读了有关如何在.Net中创建多语言程序的教程,并且它运行良好,但在这里我需要一个想法,使运行时的所有内容更容易。 在运行时,用户单击该语言。我将文化改为选择的正确语言,例如:
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
然后调用一个函数,为我的表单布局设置Text:
private System.Resources.ResourceManager rm;
fileToolStripMenuItem1.Text = rm.GetString("fileToolStripMenuItem1.Text");
settingsToolStripMenuItem.Text = rm.GetString("settingsToolStripMenuItem.Text");
因为看起来我在为程序的每个组件设置文本时由.Net构建的查找表等于应该设置的属性。换句话说,“fileToolStripMenuItem1.Text”传递给GetString()函数,结果应该设置为fileToolStripMenuItem1.Text,所以我不知道我该怎么做,甚至不知道哪个工具可以迭代在rm的每个属性上,然后通过反射或其他东西将键的值赋给键。也就是说,假设“fileToolStripMenuItem1.Text”是查找表中的键,值为“A”,那么我该怎么做呢:将“fileToolStripMenuItem1.Text”的值“a”分配给fileToolStripMenuItem1.Text
答案 0 :(得分:0)
我已经编写了一些测试winforms应用程序并尝试它并可以动态更改控件Text属性。如果需要,您可以扩展此解决方案。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//main logic of switching language of UI
void ChangeCulture_Handler(CultureInfo culture)
{
//getting relative path of resource file for specific culture
var resourcePath = GetLocalizedResourceFile(culture);
//initialize new reader of resource file
var reader = new ResXResourceReader(resourcePath);
//getting enumerator
var resourceEnumerator = reader.GetEnumerator();
//enumerate each record in resource file
while (resourceEnumerator.MoveNext())
{
string resKey = Convert.ToString(resourceEnumerator.Key);
//we can add here some check if need
//(for example if in resource file exists not only controls resources with format <Control Name>.<Property>
//if( resKey.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length == 2)
string resValue = Convert.ToString(resourceEnumerator.Value);
//actually update property
UpdateControl(resKey, resValue);
}
}
//main logic of updating property of one control
private void UpdateControl(string resKey, string resValue)
{
//we suppose that format of keys in resource file is <Control Name>.<Property>
var strs = resKey.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
var controlName = strs[0];
var controlProp = strs[1];
//find control of form by its name
var controls = this.Controls.Find(controlName, true);
if (controls.Length > 0)
{
//select first control
var control = controls[0];
//getting type of it
var t = control.GetType();
//getting property
var props = t.GetProperty(controlProp);
if (props != null)
{
//setting localized value to property
props.SetValue(control, resValue, null);
}
}
}
//build resource file path
string GetLocalizedResourceFile(CultureInfo ci)
{
string cultureCode = ci.TwoLetterISOLanguageName;
//for english language is default, so we don't have a need to add "en" part in path
return cultureCode != "en" ? string.Format("Resource1.{0}.resx", cultureCode) : "Resource1.resx";
}
private void button1_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-MX");
ChangeCulture_Handler(Thread.CurrentThread.CurrentCulture);
}
private void button2_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
ChangeCulture_Handler(Thread.CurrentThread.CurrentCulture);
}
}
}
英语资源(Resource1.resx)
button1.Text Change language to es
button2.Text Change language to en
label1.Text label1
label2.Text label2
西班牙语资源(Resource1.es.resx)
button1.Text cambiar el idioma to es
button2.Text cambiar el idioma to en
label1.Text lalble1
label2.Text lalble2