我有一个应用程序,我需要在该应用程序中使用两种语言(例如,英语和阿拉伯语),但我不知道我该怎么做 . 有人可以帮我这个吗?我需要在C#Windows窗体中使用一些示例 .
Using Localizable and Language Property of Form
Form 类有 Localizable 和 Language 属性 . 如果将 Localizable 属性设置为true,则可以为默认语言的表单添加控件,并为默认语言设置属性 . 然后,您可以选择其他语言并更改这些语言的属性 . 这样,值或可本地化的属性将存储在不同文化的单独资源文件中 .
Form
Localizable
Language
注意:如果属性使用Localizable(true)属性进行修饰,则该属性被视为可本地化 . 例如,BackColor属性不可本地化,但Text属性是可本地化的 .
Localizing Messages and Images using Resx Resource Files
该项目在 Properties 文件夹下有一个 Rseources.Resx 文件,可用于本地化图像和消息 . 您还可以将.resx资源文件添加到项目中 . 例如,您可以创建 Strings.resx 文件并向其添加一些字符串键和值,然后将其复制为 strings.en.resx 和 strings.fa.resx 并编辑这些语言的值 . 然后您可以使用这些资源值,例如:
Properties
Rseources.Resx
Strings.resx
strings.en.resx
strings.fa.resx
MessageBox.Show(Properties.Resources.AreYouSure);
将使用当前的UI文化语言显示 Resources.Resx 文件中 AreYouSure 的值 .
Resources.Resx
AreYouSure
如果未找到资源文件的文化或指定区域性的资源键,将使用 Resx 文件的中性区域中的键值 .
Resx
Change the language at Run-time
您可以使用以下方法将应用程序的区域性设置为 Persian :
Persian
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("fa"); System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("fa");
您应该将上述代码放在应用程序的开头或显示表单之前 .
More information
有关更多信息和示例:
Globalizing Windows Forms
Walkthrough: Localizing Windows Forms
在某些情况下,使用资源文件可能会更容易 .
在Visual Studio中向项目添加新资源文件 . 例如 . en.resx for english fr.resx for french .
en.resx
fr.resx
打开资源文件,在字符串中为您的字符串命名,并在值单元格中添加不同的翻译 . 例如: next station 在 en.resx 中的值是 next station 但在 fr.resx 中可以是 Prochaine station . 示例如下:
next station
Prochaine station
在代码中,使用 public static ResourceManager rm = new ResourceManager("WindowsFormsApp1.en_local", Assembly.GetExecutingAssembly()); 选择语言资源 .
public static ResourceManager rm = new ResourceManager("WindowsFormsApp1.en_local", Assembly.GetExecutingAssembly());
当您需要将任何字符串输出到应用程序时,请使用函数 GetString() ,例如 label1.Text = rm.GetString("welcome");
GetString()
label1.Text = rm.GetString("welcome");
在wwjih123的答案中有一些缺失的部分 .
在VS2017
1 - 首先在项目根文件夹中创建资源(不在Resources文件夹中) . 将其命名为lang_en,lang_tr,lang_fr等...
2 - 然后对象属性窗口将构建操作保留为嵌入式资源
3 - 在lang_tr.resx文件中添加新字符串lbl_error并在土耳其语中添加值“Hata”(无论你喜欢什么)
4-在类中定义变量为:
ResourceManager res_man; // declare Resource manager to access to specific cultureinfo
InitializeComponent()之后的5-in类初始化;
Console.WriteLine("You are speaking {0}", System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName); res_man = new ResourceManager("MyApp.lang_"+ System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, Assembly.GetExecutingAssembly()); lblError.Text = res_man.GetString("lbl_error");
如果您的ui语言是土耳其语,它将自动加载lang_tr.resx,如果英语将加载lang_en.resx文件等...
祝好运
3 回答
Using Localizable and Language Property of Form
Form
类有Localizable
和Language
属性 . 如果将Localizable
属性设置为true,则可以为默认语言的表单添加控件,并为默认语言设置属性 . 然后,您可以选择其他语言并更改这些语言的属性 . 这样,值或可本地化的属性将存储在不同文化的单独资源文件中 .注意:如果属性使用Localizable(true)属性进行修饰,则该属性被视为可本地化 . 例如,BackColor属性不可本地化,但Text属性是可本地化的 .
Localizing Messages and Images using Resx Resource Files
该项目在
Properties
文件夹下有一个Rseources.Resx
文件,可用于本地化图像和消息 . 您还可以将.resx资源文件添加到项目中 . 例如,您可以创建Strings.resx
文件并向其添加一些字符串键和值,然后将其复制为strings.en.resx
和strings.fa.resx
并编辑这些语言的值 . 然后您可以使用这些资源值,例如:将使用当前的UI文化语言显示
Resources.Resx
文件中AreYouSure
的值 .如果未找到资源文件的文化或指定区域性的资源键,将使用
Resx
文件的中性区域中的键值 .Change the language at Run-time
您可以使用以下方法将应用程序的区域性设置为
Persian
:您应该将上述代码放在应用程序的开头或显示表单之前 .
More information
有关更多信息和示例:
Globalizing Windows Forms
Walkthrough: Localizing Windows Forms
在某些情况下,使用资源文件可能会更容易 .
在Visual Studio中向项目添加新资源文件 . 例如 .
en.resx
for englishfr.resx
for french .打开资源文件,在字符串中为您的字符串命名,并在值单元格中添加不同的翻译 . 例如:
next station
在en.resx
中的值是next station
但在fr.resx
中可以是Prochaine station
. 示例如下:在代码中,使用
public static ResourceManager rm = new ResourceManager("WindowsFormsApp1.en_local", Assembly.GetExecutingAssembly());
选择语言资源 .当您需要将任何字符串输出到应用程序时,请使用函数
GetString()
,例如label1.Text = rm.GetString("welcome");
在wwjih123的答案中有一些缺失的部分 .
在VS2017
1 - 首先在项目根文件夹中创建资源(不在Resources文件夹中) . 将其命名为lang_en,lang_tr,lang_fr等...
2 - 然后对象属性窗口将构建操作保留为嵌入式资源
3 - 在lang_tr.resx文件中添加新字符串lbl_error并在土耳其语中添加值“Hata”(无论你喜欢什么)
4-在类中定义变量为:
InitializeComponent()之后的5-in类初始化;
如果您的ui语言是土耳其语,它将自动加载lang_tr.resx,如果英语将加载lang_en.resx文件等...
祝好运