请仔细关注this walkthrough。我在下面的 VS 2012 中进行的实验运行良好。
第一步。
在 Form1 上放置标签
设置 Form1.Localizable = true
设置 Form1.Language = 默认
设置标签的文本 = "Hello world!"
第二步。
设置 Form1.Language = 俄语
设置标签的文本 = "Привет мир!"
在这些步骤之后,资源文件在解决方案资源管理器中变得可见
现在将以下代码添加到 Form1 的构造函数中
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
switch (MessageBox.Show(
"Press 'Yes' for default language, 'No' for Russian.",
"Language Option", MessageBoxButtons.YesNo))
{
case System.Windows.Forms.DialogResult.Yes:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("");
break;
case System.Windows.Forms.DialogResult.No:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("ru");
break;
}
InitializeComponent();
}
}
}
运行应用程序并查看结果。
代码的主要目的是说明必须在调用方法 InitializeComponent 之前设置 CurrentUICulture。然而,在实际应用程序中,设置 CurrentUICulture 属性通常发生在程序启动时。所以代码必须移到程序开始的地方。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
switch (MessageBox.Show(
"Press 'Yes' for default language, 'No' for Russian.",
"Language Option", MessageBoxButtons.YesNo))
{
case System.Windows.Forms.DialogResult.Yes:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("");
break;
case System.Windows.Forms.DialogResult.No:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("ru");
break;
}
Application.Run(new Form1());
}
}
}
如果您为应用程序定义 UI 语言设置,那么您可以使用此处设置的值并设置 UI 语言。它将影响您在应用程序中定义的所有表单。