gpt4 book ai didi

c# - 多语言应用程序,没有定义的项目,C#

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:06 25 4
gpt4 key购买 nike

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

我在不同的网页(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++;
}

最佳答案

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

然后当您创建每个控件时,您可以使用 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");
}

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

简单(我希望 :))

查看 this link一个伟大的演练

关于c# - 多语言应用程序,没有定义的项目,C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com