有没有办法检索资源文件中所有可用语言的字符串?我只是要求同时显示 2 种语言的标签。
问问题
1860 次
1 回答
2
如果您想使用标准的 VB6 资源文件,您有两种方法。
第一种是通过资源文件中的范围来定义字符串的每个语言版本。所以说你有一个类来包装这样的字符串:
'In Class clsLocalizedStrings
Public Enum StringIds
UserNameCaption = 1
PasswordCaption
OkayCaption
CancelCaption
End Enum
Public Enum LocaleIds
English = 1000
French = 2000
Spanish = 3000
End Enum
Private mLangId As LocaleIds
Public Property Get CurrentLanguge() As LocaleIds
CurrentLanguge = mLangId
End Property
Public Property Let CurrentLanguge(ByVal newVal As LocaleIds)
mLangId = newVal
End Property
Public Function GetLocalString(ByVal id As StringIds)
Dim lResStrId As Long
lResStrId = mLangId + id
GetLocalString = LoadResString(lResStrId)
End Function
然后,您可以随意设置 CurrentLanguge 并获取字符串值。
每种语言都有自己的资源文件的另一种方法是为每种语言创建一个 ActiveX dll 项目。这些项目中的每一个都将只公开一个与上述类似的类。理想情况下,您有一个默认语言用作基础,而另一个引用它来实现该类:
'In Class LocalizedStrings in Project DefaultResources (with its own English resource file)
Public Enum StringIds
UserNameCaption = 1
PasswordCaption
OkayCaption
CancelCaption
End Enum
Public Function GetLocalString(ByVal id As StringIds)
GetLocalString = LoadResString(id)
End Function
'In Class FrenchStrings in Project FrenchResources (with its own French resource file)
Implements DefaultResources
Public Function DefaultResources_GetLocalString(ByVal id As DefaultResources.StringIds)
DefaultResources_GetLocalString= LoadResString(id)
End Function
然后在您的主应用程序中引用这些 dll 并使它们在类或模块中可用:
'In Class clsLocalization in main app project
Public Enum LocaleIds
English = 1
French = 2
Spanish = 3
End Enum
Private mLangId As LocaleIds
Private mResources() as DefaultResources
Private Sub Class_Initialize()
Redim mResources (1 to 3)
Set mResources(1) = DefaultResources.LocalizedStrings 'assumes english is default, easy to change though
Set mResources(2) = FrenchResources.FrechStrings
Set mResources(3) = SpanishResources.SpanishStrings
End Sub
Public Property Get CurrentLanguge() As LocaleIds
CurrentLanguge = mLangId
End Property
Public Property Let CurrentLanguge(ByVal newVal As LocaleIds)
mLangId = newVal
End Property
Public Function GetLocalString(ByVal id As StringIds)
GetLocalString = mResources(mLangId).GetLocalString(id)
End Function
Public Propert Get Langauge(ByVal langId As LocaleIds)
Return mResources(langId)
End Property
这在一个模块中:
Global Localization As clsLocalization
然后你像这样使用它:
'show caption for current languge
MsgBox Localization.GetLocalString(StringIds.UserCaption)
'show caption for explicit languge
MsgBox Localization.Langauge(French).GetLocalString(StringIds.UserCaption)
MsgBox Localization.Langauge(Spanish).GetLocalString(StringIds.UserCaption)
希望有帮助!
于 2012-11-09T15:01:08.073 回答