2

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

我在不同的网页上看到了一些关于如何在我的应用程序中更改语言的教程(比如这个这个

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

所以,我不能像教程所说的那样使用它不起作用。

我不知道我做错了还是根本不起作用

任何想法?我不知道该怎么办

我读了。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++;
    }
4

1 回答 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()方法,其中的文本label1将从Hello world更改为Bonjour le monde!

简单(我希望:))

查看此链接以获得出色的演练

于 2013-09-23T17:18:41.467 回答