对于一个项目,我们希望有不同的语言版本,对于这种需求,我们可以将界面上所有的文本框文本和图片的信息保存在一些配置文件中,通过程序运行过程中的参数,进行动态加载。
在.net下面(是结合winform来说的),实现国际化的方式有四种
–借助于config文件
–借助于ini文件
–借助于txt文件
–借助于resources文件(推荐)
借助于config文件
使用config文件方式需要注意几点:
a 若是资源文件的内容不是很多,而且不是很重要的信息数据,推荐使用此方法
b 如果资源文件内容很多,特别是数据非常重要的话(如数据库联接字符串),不推荐使用此方法,请使用Resources的方法
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Name-txt" value="(c)用户名"></add>
<add key="Name-value" value="(c)张三"></add>
<add key="desc-txt" value="(c)简介"></add>
<add key="desc-value" value="(c)他这个人很懒,什么都不作"></add>
</appSettings>
</configuration>
代码中读取config文件的例子:
'read config
Private Sub readConfig()
Me.Label1.Text = ConfigurationSettings.AppSettings("Name-txt")
Me.TextBox1.Text = ConfigurationSettings.AppSettings("Name-value")
Me.Label2.Text = ConfigurationSettings.AppSettings("desc-txt")
Me.TextBox2.Text = ConfigurationSettings.AppSettings("desc-value")
End Sub
借助于ini文件
[common]
Name-txt=(i)用户名
Name-value=(i)张三
desc-txt=(i)简介
desc-value=(i)他这个人很懒,什么都不作
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'注意
' a 此方法为vb6下面的方法,不推荐此方法
' b 必须注意Ini文件的路径,要判断文件是否存在(如果使用config文件,系统会自动在bin目录下面复制一份)
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'read ini
Private Sub readIni()
Dim path As String
path = Application.StartupPath + "\app.ini"
If System.IO.File.Exists(path) = False Then
MsgBox("File not exist")
Exit Sub
End If
Me.Label1.Text = GetINI("common", "Name-txt", "", path)
Me.TextBox1.Text = GetINI("common", "Name-value", "", path)
Me.Label2.Text = GetINI("common", "desc-txt", "", path)
Me.TextBox2.Text = GetINI("common", "desc-value", "", path)
End Sub
'声明INI配置文件读写API函数
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
'定义读取配置文件函数
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
'定义写入配置文件函数
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
借助于txt文件
;==========================================
; Common
; =========================================
Name-txt=(t)用户名
Name-value=(t)张三
desc-txt=(t)简介
desc-value=(t)他这个人很懒,什么都不作
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'注意
' a 若是资源文件的内容不是很多,而且不是很重要的信息数据,推荐使用config方法
' b 如果资源文件内容很多,特别是数据非常重要的话,请使用Resources的方法
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'read txt
Private Sub readTxt()
readTxtFile()
Me.Label1.Text = Me.htb("Name-txt")
Me.TextBox1.Text = Me.htb("Name-value")
Me.Label2.Text = Me.htb("desc-txt")
Me.TextBox2.Text = Me.htb("desc-value")
End Sub
'from txt
Sub readTxtFile()
'直接读文本文件
Dim path As String
Dim strCurrLine As String
Dim reader As StreamReader
Try
path = Application.StartupPath + "\app.txt"
If System.IO.File.Exists(path) = False Then
MsgBox("File not exist")
Exit Sub
End If
reader = New StreamReader(path)
Me.htb = New Hashtable
While reader.Peek <> -1
strCurrLine = reader.ReadLine
If (strCurrLine.IndexOf("=") <> -1 And strCurrLine.IndexOf("=") = strCurrLine.LastIndexOf("=")) Then
htb.Add(strCurrLine.Split("=")(0).Trim, strCurrLine.Split("=")(1).Trim)
End If
End While
Catch ex As Exception
Finally
reader.Close()
End Try
End Sub借助于resources文件
要使用 命令行 将txt文件编译成 resources文件,如:
resgen app.txt app.resources
(C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\resgen.exe 2003生成的和2005生成的不通用)
ResourceReader rd = new ResourceReader("D:\\my ducument\\Facet document\\Hand_Trainning\\Facet_Demo\\bin\\app.resources");
IDictionaryEnumerator en = rd.GetEnumerator();
Hashtable htb = new Hashtable();
while (en.MoveNext())
{
htb.Add(en.Key, en.Value);
}
en = htb.GetEnumerator();
StringWriter sw = new StringWriter();
while (en.MoveNext())
{
sw.WriteLine("[key]=" +en.Key +" "+"[value]="+en.Value);
}
MessageBox.Show(sw.ToString());


浙公网安备 33010602011771号