[VB.Net] UI介面多國語系切換功能

UI介面多國語系切換功能 

Imports System.ComponentModel
Imports System.Globalization
Imports System.Resources

'UI介面語系轉換
Public Class C_UILanguage

    Private systemResourceManager As ResourceManager
    Private errorResourceManager As ResourceManager
    Dim CRM As System.ComponentModel.ComponentResourceManager

    Public Sub New()
        systemResourceManager = New ResourceManager("專案名稱.Resources", GetType(C_UILanguage).Assembly)
        errorResourceManager = New ResourceManager("專案名稱.Resources.ErrorMessages", GetType(C_UILanguage).Assembly)
    End Sub
    Public Sub _Main(ByVal _Form As Windows.Forms.Form)

        '檢查引數
        If _Form Is Nothing Then
            _Form.Text = "Form"
            Throw New ArgumentNullException(_Form.Text)
        End If

        CRM = Nothing

        '切換介面顯示的語言
        Try
            CRM = New ComponentResourceManager(_Form.GetType)
            ' 設置表單的標題
            CRM.ApplyResources(_Form, "$this")
            ' 設置所有控件
            For Each Control As Control In _Form.Controls
                Control.Text = "Control"
                _Control_Resource(Control, CRM)
            Next
        Finally
            CRM = Nothing
        End Try

    End Sub

    Private Sub _Control_Resource(ByRef _Control As Control, ByRef _CRM As ComponentResourceManager)

        '檢查引數
        If _Control Is Nothing Then
            Throw New ArgumentNullException("Control")
        End If
        If _CRM Is Nothing Then
            Throw New ArgumentNullException("CRM")
        End If

        _CRM.ApplyResources(_Control, _Control.Name)

        If TypeOf _Control Is MenuStrip Then
            Dim MS As MenuStrip = CType(_Control, System.Windows.Forms.MenuStrip)
            For Each TSI As ToolStripItem In MS.Items
                _Strip_Resource(TSI, _CRM)
            Next
        ElseIf TypeOf _Control Is ToolStrip Then
            Dim TS As ToolStrip = CType(_Control, ToolStrip)
            For Each TSI As ToolStripItem In TS.Items
                _Strip_Resource(TSI, _CRM)
            Next
        ElseIf _Control.Controls.Count > 0 Then
            For Each Control As Control In _Control.Controls
                _Control_Resource(Control, _CRM)
            Next
        End If
    End Sub

    Private Sub _Strip_Resource(ByRef _TSI As ToolStripItem, ByRef _CRM As ComponentResourceManager)

        '檢查引數
        If _TSI Is Nothing Then
            Throw New ArgumentNullException("TSI")
        End If

        If _CRM Is Nothing Then
            Throw New ArgumentNullException("CRM")
        End If

        _CRM.ApplyResources(_TSI, _TSI.Name)

        If TypeOf _TSI Is ToolStripMenuItem Then
            Dim TSM As ToolStripMenuItem = CType(_TSI, ToolStripMenuItem)
            For Each TS As ToolStripItem In TSM.DropDownItems
                _Strip_Resource(TS, _CRM)
            Next
        End If

    End Sub

    Public Sub _DataGridLanguage(dgv As DataGridView)
        Try
            Dim CultureInfo As CultureInfo = New CultureInfo(UserInfo.Language)
            'Dim resourceSet = systemResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
            'For Each Entry In resourceSet
            '    Console.WriteLine(Entry.Key) ' 列出所有資源鍵
            'Next

            For Each column As DataGridViewColumn In dgv.Columns
                Dim colName As String = systemResourceManager.GetString(column.Name, CultureInfo)
                If Not String.IsNullOrWhiteSpace(colName) Then
                    column.HeaderText = colName
                End If
            Next
        Catch ex As Exception
            ' 錯誤處理
        End Try
    End Sub

    Public Function _MessageLanguage(message As String) As String
        Try
            Dim cultureInfo As New CultureInfo(UserInfo.Language)
            Dim msg As String = errorResourceManager.GetString(message, cultureInfo)

            If Not String.IsNullOrWhiteSpace(msg) Then
                Return msg
            Else
                Return message
            End If
        Catch ex As Exception
            ' 錯誤處理
            Return message
        End Try
    End Function

End Class
參考網址: 

By Hao★

留言

熱門文章

[C#] 將DataTable轉換成Html格式表格

[MS SQL] 查詢所有Procedure