VS 多语言版本开发与实现

来源:https://blog.csdn.net/hansonjan/article/details/4795263

 在.net中实现多国语言的方法其实是很简单的。

下面就做一个示例跟大家一步一步地介绍:

首先新建一个VB.net的项目。项目名称随意。

建立一个MDI表单FrmMain和一个Form1表单。

然后新建一个铺助类LanguageHelper.vb

 

表单设计如下:

 

 

 中文语言特性界面设计如下:

 

 

英文语言特性界面设计如下:

 

最后看一下资源文件:

 

下面来看实现的代码:

首先,我们创建一个铺助类,为切换语言提供一些方法。

 1 Imports System
 2 Imports System.Collections.Generic
 3 Imports System.Text
 4 Imports System.ComponentModel
 5 Imports System.Threading
 6 Imports System.Windows.Forms
 7 Imports System.Globalization
 8 
 9 Public Class LanguageHelper
10 
11     ''' <summary>  
12     ''' 改变程序当前使用的语言特性  
13     ''' </summary>  
14     ''' <param name="languageName">语言名称</param>  
15     ''' <param name="frm">Form类别</param>  
16     ''' <returns></returns>  
17     ''' <remarks></remarks>  
18     Public Shared Function SetLanguage(ByVal languageName As String, ByVal frm As Form) As Boolean
19         Try
20             '改变当前线程的UI资源文件  
21             Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(languageName)
22             '改变当前线程的资源文件  
23             Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(languageName)
24             '创建一个资源管理器  
25             Dim res As ComponentResourceManager = New ComponentResourceManager(frm.GetType)
26             '将资源文件的内容更新至表单上 $this在资源文件中表示窗体本身。  
27             res.ApplyResources(frm, "$this")
28             '递归更新资源文件的内容到表单控件上  
29             ApplyResouce(frm, res)
30             Return True
31         Catch ex As Exception
32             Return False
33         End Try
34     End Function
35 
36 
37     ''' <summary>  
38     ''' 更新资源文件的内容到表单控件上  
39     ''' </summary>  
40     ''' <param name="control">要更新的控件</param>  
41     ''' <param name="res">资源文件管理器</param>  
42     '''   
43     Private Shared Sub ApplyResouce(ByVal control As Control, ByVal res As ComponentResourceManager)
44         For Each c As Control In control.Controls
45             res.ApplyResources(c, c.Name)
46             ApplyResouce(c, res)
47             If c.GetType().IsSubclassOf(GetType(ToolStrip)) Then
48                 ApplyResouce(CType(c, ToolStrip), res)
49             End If
50         Next
51     End Sub
52 
53     ''' <summary>  
54     ''' set language for toolstrip all child item  
55     ''' </summary>  
56     ''' <param name="toolstrip"></param>  
57     ''' <param name="resources"></param>  
58     Private Shared Sub ApplyResouce(ByVal toolstrip As ToolStrip, ByVal resources As ComponentResourceManager)
59         If toolstrip.Items.Count > 0 Then
60             For Each item As ToolStripItem In toolstrip.Items
61                 ApplyResouce(item, resources)
62             Next
63         End If
64     End Sub
65 
66     ''' <summary>  
67     ''' 递归更新Menu的子菜单  
68     ''' </summary>  
69     ''' <param name="item"></param>  
70     ''' <param name="resources"></param>  
71     Private Shared Sub ApplyResouce(ByVal item As ToolStripItem, ByVal resources As ComponentResourceManager)
72 
73         resources.ApplyResources(item, item.Name)
74         If (item.GetType.IsSubclassOf(GetType(ToolStripDropDownItem))) Then
75             Dim ditem As ToolStripDropDownItem = CType(item, ToolStripDropDownItem)
76             If ditem.DropDownItems.Count > 0 Then
77                 For Each sitem As ToolStripItem In ditem.DropDownItems
78                     ApplyResouce(sitem, resources)
79                 Next
80             End If
81         End If
82     End Sub
83 End Class

 

模块添加函数

    ''' <summary>  
    ''' 切换语言的时候,如果有打开子窗体,那么我们需要同时更新MDI子窗体  
    ''' </summary>  
    ''' <param name="languageName"></param>  
    ''' <remarks></remarks>  
    Public Sub changeLanguage(ByRef MyForm As Form, ByVal languageName As String)
        Try
            LanguageHelper.SetLanguage(languageName, MyForm)
            For Each frm As Form In MyForm.MdiChildren
                LanguageHelper.SetLanguage(languageName, frm)
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

 

窗体启动事件添加:

        '系统语言设置
        changeLanguage(Me, DefaultLanguage)

 

DefaultLanguage为系统语言,可将修改记录到注册表中进行存储
        DefaultLanguage = "zh-cn"

        Dim LG As New JudgeEdition("")
        LG.WriteLanguageRegedit(DefaultLanguage)

Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports System.Windows.Forms

Public Class JudgeEdition
    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

    Private LanguageMode As String  '注册表 - 系统语言

    Public Function GetLanguage() As String
        Try
            Dim rLanguage As Object
            rLanguage = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\######", "LanguageMode", "zh-cn")
            If rLanguage = Nothing Then
                '注册信息损坏
                Call WriteLanguageRegedit("zh-cn")
                Return "zh-cn"
            Else
                Return rLanguage
            End If
        Catch ex As Exception
            MessageBox.Show("注册信息损坏" & vbCrLf & ex.Message, StrAppname, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return "zh-cn"
        End Try
    End Function


    Public Function WriteLanguageRegedit(Optional ByVal rLanguage As String = "zh-cn") As Boolean
        Try
            My.Computer.Registry.CurrentUser.CreateSubKey("LanguageMode")
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\######", _
         "LanguageMode", rLanguage)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

End Class
'###### 为自定义名称


posted @ 2018-09-20 17:55  久_久  阅读(1297)  评论(0编辑  收藏  举报