多语言应用程序,无定义项目,C#

时间:2013-09-23 15:50:50

标签: c# multilingual

我一直在开发一个应用。 (它运行正常,没有问题)。 但现在,我的老板需要使用英语,西班牙语和其他语言。

我在不同的网页上看到了一些关于如何在我的应用中更改语言的教程(like thisand this

我的问题是:  我没有定义任何项目,我的意思是,我没有文本框,标签或按钮。我只有一个表格: 当我的应用程序运行时,它会读取文件:如果中有一个“按钮”行,我的应用程序会在我的表单中添加一个按钮,如果有一个“标签”行,它将添加一个新标签。

因此,我不能像教程所说的那样使用文件。 它不起作用。

我不知道我做错了还是根本不行

有什么想法吗? 我不知道该怎么做

我读了。 txt文件(逐行),我分配像这样的属性

public static Label[] LAB = new Label[2560];
public static int indice_LABEL = 0;


    if (TipoElemento == "LABEL")
    {
     LAB[indice_LABEL] = new Label();
     LAB[indice_LABEL].Name = asigna.nombreElemento;
     LAB[indice_LABEL].Left = Convert.ToInt32(asigna.left);//LEFT
     LAB[indice_LABEL].Top = Convert.ToInt32(asigna.top);//TOP
     LAB[indice_LABEL].Width = Convert.ToInt32(asigna.width);
     LAB[indice_LABEL].Height = Convert.ToInt32(asigna.height);
     //and all I need
     ...
     ...
     Formulario.PanelGE.Controls.Add(Herramientas.LAB[Herramientas.indice_LABEL]);
     Herramientas.indice_LABEL++;
    }

1 个答案:

答案 0 :(得分:1)

如果你需要坚持使用这种格式,最好的解决方案是让1个文件包含所有控件定义(名称,尺寸,位置等),另一个文件包含要显示给用户的文本

然后,当您创建每个控件时,不是为其指定标题,而是使用链接到“标题”文件的ResourceManager(每种语言1个)来检索要显示的正确字符串

例如:

语言文字文件

这将是一个简单的文本文件, resource.en-US.txt

在内部,您需要添加简单的键>值对:

label1=Hello world!

要制作另一种语言,只需创建另一个文件 resource.fr-FR.txt ,然后添加:

label1=Bonjour le monde!

申请代码

// Resource path
private string strResourcesPath= Application.StartupPath + "/Resources";

// String to store current culture which is common in all the forms
// This is the default startup value
private string strCulture= "en-US";

// ResourceManager which retrieves the strings
// from the resource files
private static ResourceManager rm;

// This allows you to access the ResourceManager from any form
public static ResourceManager RM
{ 
  get 
  { 
   return rm ; 
   } 
}


private void GlobalizeApp()
{
    SetCulture();
    SetResource();
    SetUIChanges();
}
private void SetCulture()
{
    // This will change the current culture
    // This way you can update it without restarting your app (eg via combobox)
    CultureInfo objCI = new CultureInfo(strCulture);
    Thread.CurrentThread.CurrentCulture = objCI;
    Thread.CurrentThread.CurrentUICulture = objCI;

}
private void SetResource()
{
    // This sets the correct language file to use
    rm = ResourceManager.CreateFileBasedResourceManager
        ("resource", strResourcesPath, null);

}
private void SetUIChanges()
{
    // This is where you update all of the captions
    // eg:
    label1.Text=rm.GetString("label1");
}

然后您需要做的就是将私有字符串strCulture= "en-US"更改为“fr-FR”(例如在组合框中),并调用GlobalizeApp()方法和{{1中的文本将从 Hello world 更改为 Bonjour le monde!

简单(我希望:))

查看this link以获得精彩的演练