代码之家  ›  专栏  ›  技术社区  ›  RobertPitt

C应用中的本地化

  •  9
  • RobertPitt  · 技术社区  · 15 年前

    我正在创建一个基于winforms/kryptoforms的C应用程序,由于该应用程序正在开发过程中,我认为自己最好将本地化解决。

    作为一个出生和成长的PHP程序员(我知道C是一个全新的层次),我将创建一个类来检测语言并自动将语言包分配给应用程序。然后使用语言对象访问值。

    我想知道我是否可以在 最容易的 / 最好的 这样做的方法。

    我个人想要一些符合应用程序设置的东西

    我通常会去哪 MyApplication.Properties.Settings.Default.SomeKey 我希望 MyApplication.Languages.Current.ApplicationTitle MyApplication.Languages.en.ApplicationTitle 例如。

    此外,只加载每个语言文件的一个资源来提高速度也是有益的。

    因此,语言加载为英语,西班牙语用户会被提升为该应用程序为英语,是否要将其更改为西班牙语,单击“是”,更新设置,重新启动应用程序并加载单个西班牙语语言包。

    你对此有何看法?


    编辑:

    该应用程序基于XMPP协议并使用AGSXMPP库。根据我的理解,发送给我他们的状态的每个用户也应该发送他们系统所使用的语言。

    所以基本上,如果有任何方法可以“掌握”存储一个单词并使用 __("some string") 在我的应用程序中是可能的,但同时我只是在查看GUI文本。

    3 回复  |  直到 10 年前
        1
  •  7
  •   Christian    15 年前

    我们进行本地化的方式是:

    • 将WinForm的“Localizable”属性设置为“true”(这将为该窗体生成一个新的资源文件,其中包含标签、按钮的文本以及Z顺序等)
    • 创建一个formname.de.resx文件(“de”,因为我们是德国人),然后将需要本地化的字符串存储在其中(通过ResourceManager类可以访问此资源文件)。
    • 对于需要本地化的非WinForms代码,我们只需创建单独的资源文件

    编译应用程序后,将创建appname.resources.dll。该dll包含应用程序的所有资源,然后可以与工具(如可视化本地化)一起使用,将字符串转换为另一种语言(如英语或西班牙语等)。

        2
  •  2
  •   garik    15 年前

    你应该使用资源。

    您可以动态更改语言:

    1)在控制台应用程序中:

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    double a = 100.12;
    Console.WriteLine("{0} - {1}", Thread.CurrentThread.CurrentCulture, a);
    
    Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
    Console.WriteLine("{0} - {1}", Thread.CurrentThread.CurrentCulture, a);
    Console.ReadLine();
    

    2)在winforms应用程序中,我们可以重新打开表单以应用本地化资源。 (使用) 可本地化的 语言 表单设计器中用于为每种语言自动生成资源的属性):

    if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
    }
    else
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
    }
    
    double a = 100.12;
    textBox1.Text = a.ToString(Thread.CurrentThread.CurrentCulture);
    
    Form1 f = new Form1();
    f.ShowDialog();
    

    3)使用多个具有自己本地化功能的线程

    private void button1_Click(object sender, EventArgs e)
    {
        // for example main thread language is en-US        
    
        Thread t = new Thread(StartForm);
        t.CurrentUICulture = new CultureInfo("ru-RU");
        t.Start();
        //t.Join();
    }
    
    public static void StartForm()
    {
        Form1 f = new Form1();
        f.ShowDialog();
    }
    
        3
  •  1
  •   Run CMD    15 年前

    我们使用resources.myresources.somestring,它会自动以正确的语言进行翻译。资源文件名为myresources.de-de.resx、myresources.nl-be.resx等。 方法与项目属性基本相同。

    示例翻译代码:

    public void TranslateForm()
    {
            menuItem11.Text = Resources.MyResources.Nieuw;
            menuItem12.Text = Resources.MyResources.Verwijderen;
            menuItem13.Text = Resources.MyResources.Kopieren;
    }
    

    或者你可以手动操作,比如:

    menuItem11.Text = Translator.GetString("New", "de-DE" );
    

        public static string GetString( string varname )
        {
            string resourceName = typeof(Vertaling).Namespace + ".Resources.MyResources";
            ResourceManager rm = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
            return rm.GetString(varname);
        }
    
        public static string GetString( string varname, string taalCode )
        {
            string resourceName = typeof(Vertaling).Namespace + ".Resources.MyResources";
            ResourceManager rm = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
            return rm.GetString(varname, new CultureInfo(taalCode) );
        }
    
    推荐文章