C# 无需重新编译项目的本地化
我正在建设一个项目,我需要使它非常定制。我正试图建立它支持4种语言。用户将拥有一个管理面板,他/她可以在其中更改标签文本或按钮文本。我希望用户转到该管理面板并更改按钮的文本,而无需呼叫我:) 我使用了旧但很好的本地化风格,即.resx文件。我有下面的示例代码C# 无需重新编译项目的本地化,c#,xml,winforms,localization,C#,Xml,Winforms,Localization,我正在建设一个项目,我需要使它非常定制。我正试图建立它支持4种语言。用户将拥有一个管理面板,他/她可以在其中更改标签文本或按钮文本。我希望用户转到该管理面板并更改按钮的文本,而无需呼叫我:) 我使用了旧但很好的本地化风格,即.resx文件。我有下面的示例代码 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedItem.ToStri
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString().Equals("en-GB"))
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-GB");
label1.Text= FormLabels.test1;
label2.Text = FormLabels.test2;
}
else if (comboBox1.SelectedItem.ToString().Equals("de-DE"))
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
label1.Text = FormLabels.test1;
label2.Text = FormLabels.test2;
}
}
如果我允许用户在“FormLabels.en GB.resx”文件中更改按钮的文本。必须重新编译项目才能看到更改
我需要找到一个解决方案,用户可以通过重新编译来更改按钮的文本。我如何才能做到这一点?资源管理器仅适用于嵌入式资源。 您必须自己编写ResourceManager 您可以使用resxResourceSet(),然后围绕它编写自己的ResourceManager。 这并不难做到。 然后,您可以自己读取resx文件,并在resx扩展之前预先添加区域性
祝你好运我唯一能想到的就是在外部文件中进行本地化。 创建一个xml文件,如: ex:languagesSupported.xml
<Languages>
<language name="English" file="en.dat" />
<language name="French" file="fr.dat" />
<language name="Japanese" file="jp.dat" />
</Languages>
像这样,您可以在以后添加更多语言
现在,在每个文件中,您需要执行以下操作:
(例如:en.dat)
(例如:fr.dat)
在代码中,您可以执行以下操作:
private Dictionary<string, Dictionary<string, string>> _localizations = new Dictionary<string, Dictionary<string, string>>();
private string _currentLocalization = "English";
private bool LoadLocalizations()
{
try
{
if (File.Exists("languagesSupported.xml") == false)
{
return false;
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("languagesSupported.xml");
XmlNodeList nodeList = xmldoc.SelectNodes("languages/language");
if (nodeList.Count > 0)
{
foreach (XmlNode node in nodeList)
{
LoadLocalization(node.Attributes["name"].Value, node.Attributes["file"].Value);
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}
private bool LoadLocalization(string pLang, string pFile)
{
try
{
if (File.Exists(pFile) == false)
{
return false;
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(pFile);
XmlNodeList nodeList = xmldoc.SelectNodes("language/localized");
_localizations.Add(pLang, new Dictionary<string,string>());
if (nodeList.Count > 0)
{
foreach (XmlNode node in nodeList)
{
_localizations[pLang].Add(node.Attributes["name"].Value, node.Attributes["value"].Value);
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}
private void SetLocalization()
{
labelHello.text = _localizations[_currentLocalization]["hello"];
labelGoodbye.text = _localizations[_currentLocalization]["goodbye"];
}
private Dictionary\u localizations=new Dictionary();
私有字符串_currentLocalization=“English”;
私有bool LoadLocalizations()
{
尝试
{
if(File.Exists(“languagesSupported.xml”)==false)
{
返回false;
}
XmlDocument xmldoc=新的XmlDocument();
Load(“languagesSupported.xml”);
XmlNodeList nodeList=xmldoc.SelectNodes(“语言/语言”);
如果(nodeList.Count>0)
{
foreach(节点列表中的XmlNode节点)
{
LoadLocalization(node.Attributes[“name”].Value,node.Attributes[“file”].Value);
}
}
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
私有布尔加载本地化(字符串规划、字符串文件)
{
尝试
{
if(File.Exists(pFile)==false)
{
返回false;
}
XmlDocument xmldoc=新的XmlDocument();
xmldoc.Load(pFile);
XmlNodeList nodeList=xmldoc.SelectNodes(“语言/本地化”);
_添加(pLang,newdictionary());
如果(nodeList.Count>0)
{
foreach(节点列表中的XmlNode节点)
{
_本地化[pLang].Add(节点.属性[“名称”].Value,节点.属性[“值”].Value);
}
}
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
私有void SetLocalization()
{
labelHello.text=_本地化[_currentLocalization][“hello”];
labeldebye.text=_本地化[_currentLocalization][“再见”];
}
之后,每次用户更改语言时,只需更新_currentLocalization并调用SetLocalization()
您甚至可以使用_本地化中的键填充语言的下拉列表
这样可以使本地化完全动态化
如果您真的想使用CultureInfo,只需将区域性映射到语言名称。可能会有帮助。您能解释一下“FormLabels.test1”指的是什么吗?我认为这部分与你的代码有关。我想在我的代码中实现这个应用程序。如果你解释一下,我会很高兴的。
<Language name="French">
<Localized name="hello" value="Bonjour">
<Localized name="goodbye" value="Au revoir">
</Language>
private Dictionary<string, Dictionary<string, string>> _localizations = new Dictionary<string, Dictionary<string, string>>();
private string _currentLocalization = "English";
private bool LoadLocalizations()
{
try
{
if (File.Exists("languagesSupported.xml") == false)
{
return false;
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("languagesSupported.xml");
XmlNodeList nodeList = xmldoc.SelectNodes("languages/language");
if (nodeList.Count > 0)
{
foreach (XmlNode node in nodeList)
{
LoadLocalization(node.Attributes["name"].Value, node.Attributes["file"].Value);
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}
private bool LoadLocalization(string pLang, string pFile)
{
try
{
if (File.Exists(pFile) == false)
{
return false;
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(pFile);
XmlNodeList nodeList = xmldoc.SelectNodes("language/localized");
_localizations.Add(pLang, new Dictionary<string,string>());
if (nodeList.Count > 0)
{
foreach (XmlNode node in nodeList)
{
_localizations[pLang].Add(node.Attributes["name"].Value, node.Attributes["value"].Value);
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}
private void SetLocalization()
{
labelHello.text = _localizations[_currentLocalization]["hello"];
labelGoodbye.text = _localizations[_currentLocalization]["goodbye"];
}