【问题标题】:Localization in winformswinforms本地化
【发布时间】:2017-05-20 11:17:53
【问题描述】:

当本地化属性在 VS 2012 中设置为 true 时,不会为新添加的表单创建资源文件。

当我向项目添加新表单时,将 Localizable 属性设置为 true 并构建应用程序,不会创建 .resx 文件。

【问题讨论】:

  • 您应该同时设置LocalizableLanguage 属性。可能你还没有设置Language 属性。 ResX 文件位于解决方案资源管理器中的 Form 节点下。看看这个帖子How to make multi language app in winforms
  • 我已经设置了语言属性,然后构建了应用程序。仍然没有创建 resx 文件。不仅如此,我还尝试过马拉地语、Telegu 和法语。但是没有一个 .resx 文件被创建。
  • 我相信您必须先用新语言修改资源,然后才能创建 ResX 文件。

标签: winforms localization


【解决方案1】:

请仔细关注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 语言。它将影响您在应用程序中定义的所有表单。

【讨论】:

    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多