1+ Imports System.Globalization
2+ Imports System.Resources
3+ Imports System.Threading
4+ Imports System.Windows.Markup
5+ Imports System.Windows.Data
6+ Imports System.Reflection
7+
8+ Public Class LanguageHelper
9+ ' 支持的语言列表
10+ Private Shared ReadOnly SupportedCultures As String () = { "en-US" , "zh-CN" }
11+ Private Shared resourceManager As ResourceManager = i18n.i18n.ResourceManager
12+ Private Shared currentCulture As CultureInfo = Nothing
13+
14+ Public Shared Function GetText( key As String ) As String
15+ Return GetString( key )
16+ End Function
17+
18+ Public Shared Sub Initialize()
19+ Dim savedLanguage As String = ReadAppConfig( "language" )
20+ If Not String .IsNullOrEmpty(savedLanguage) AndAlso SupportedCultures.Contains(savedLanguage) Then
21+ ApplyCulture(savedLanguage)
22+ Else
23+ SetDefaultLanguage()
24+ End If
25+ End Sub
26+
27+ Public Shared Sub ChangeLanguage()
28+ If currentCulture Is Nothing Then
29+ currentCulture = Thread.CurrentThread.CurrentUICulture
30+ End If
31+ Dim currentLang As String = currentCulture.Name
32+ Dim nextLang As String = GetNextLanguage(currentLang)
33+
34+ ApplyCulture(nextLang)
35+ WriteAppConfig( "language" , nextLang)
36+ End Sub
37+
38+ Public Shared Function GetString( key As String , ParamArray args As Object ()) As String
39+ Try
40+ Dim cultureToUse = If (currentCulture, Thread.CurrentThread.CurrentUICulture)
41+ Dim rawValue As String = resourceManager.GetString( key , cultureToUse)
42+
43+ If String .IsNullOrEmpty(rawValue) Then
44+ Return key
45+ ElseIf args IsNot Nothing AndAlso args.Length > 0 Then
46+ Return String .Format(cultureToUse, rawValue, args)
47+ Else
48+ Return rawValue
49+ End If
50+ Catch ex As Exception
51+ Debug.WriteLine( $"获取多语言文本失败:{key},错误:{ex.Message}" )
52+ Return key
53+ End Try
54+ End Function
55+
56+ Public Shared Sub ApplyCulture(cultureName As String )
57+ Try
58+ Dim culture As New CultureInfo(cultureName)
59+ Thread.CurrentThread.CurrentUICulture = culture
60+ Thread.CurrentThread.CurrentCulture = culture
61+ currentCulture = culture
62+
63+ ' 额外:如果是WPF/WinForms,刷新界面(可选)
64+ ' WinForms示例:Application.CurrentCulture = culture
65+ ' WPF示例:FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(XmlLanguage.GetLanguage(culture.IetfLanguageTag)))
66+ Catch ex As Exception
67+ Debug.WriteLine( $"应用语言失败:{cultureName},错误:{ex.Message}" )
68+ SetDefaultLanguage()
69+ End Try
70+ End Sub
71+
72+ Private Shared Function GetNextLanguage(currentLanguage As String ) As String
73+ Dim currentTwoLetter = New CultureInfo(currentLanguage).TwoLetterISOLanguageName
74+ For i As Integer = 0 To SupportedCultures.Length - 1
75+ Dim langTwoLetter = New CultureInfo(SupportedCultures(i)).TwoLetterISOLanguageName
76+ If langTwoLetter = currentTwoLetter Then
77+ Return SupportedCultures((i + 1 ) Mod SupportedCultures.Length)
78+ End If
79+ Next
80+ Return SupportedCultures( 0 )
81+ End Function
82+
83+ Private Shared Sub SetDefaultLanguage()
84+ ' 根据系统语言设置默认语言
85+ Dim langMapping As New Dictionary( Of String , String ) From {
86+ { "en" , "en-US" },
87+ { "zh" , "zh-CN" }
88+ }
89+ '{"ja", "ja-JP"},
90+ '{"ko", "ko-KR"},
91+ '{"fr", "fr-FR"},
92+ '{"de", "de-DE"},
93+ '{"es", "es-ES"},
94+ '{"ru", "ru-RU"}
95+
96+ Dim systemLang As String = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower()
97+ Dim defaultLang As String = If (langMapping.ContainsKey(systemLang), langMapping(systemLang), "en-US" )
98+
99+ ' 特殊处理中文简/繁
100+ 'If systemLang = "zh" Then
101+ ' If Thread.CurrentThread.CurrentUICulture.Name.StartsWith("zh-TW") Then
102+ ' defaultLang = "zh-TW"
103+ ' ElseIf Thread.CurrentThread.CurrentUICulture.Name.StartsWith("zh-HK") Then
104+ ' defaultLang = "zh-HK"
105+ ' End If
106+ 'End If
107+
108+ '@@@
109+ 'Dim systemLang = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName
110+ 'Dim defaultLang As String = "en-US"
111+
112+ ''匹配多语言
113+ 'Select Case systemLang.ToLower()
114+ ' Case "zh" ' 中文
115+ ' defaultLang = "zh-CN"
116+ ' Case "en" ' 英文
117+ ' defaultLang = "en-US"
118+ ' Case "ja" ' 日语
119+ ' defaultLang = "ja-JP"
120+ ' Case "ko" ' 韩语
121+ ' defaultLang = "ko-KR"
122+ ' Case "fr" ' 法语
123+ ' defaultLang = "fr-FR"
124+ ' Case "de" ' 德语
125+ ' defaultLang = "de-DE"
126+ ' Case "es" ' 西班牙语
127+ ' defaultLang = "es-ES"
128+ ' Case "ru" ' 俄语
129+ ' defaultLang = "ru-RU"
130+ ' Case Else ' 未匹配语言,默认英文
131+ ' defaultLang = "en-US"
132+ 'End Select
133+
134+ ApplyCulture(defaultLang)
135+ WriteAppConfig( "language" , defaultLang)
136+ End Sub
137+
138+ Public Shared Function GetCurrentLanguage() As String
139+ Return If (currentCulture, Thread.CurrentThread.CurrentUICulture).Name
140+ End Function
141+
142+ Public Shared Function ReadAppConfig( key As String ) As String
143+ Try
144+ Dim config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
145+ Return If (config.AppSettings.Settings( key )?.Value, String .Empty)
146+ Catch ex As Exception
147+ Debug.WriteLine( $"读取配置失败:{key},错误:{ex.Message}" )
148+ Return String .Empty
149+ End Try
150+ End Function
151+
152+ Public Shared Sub WriteAppConfig( key As String , value As String )
153+ Try
154+ Dim config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
155+ If config.AppSettings.Settings( key ) IsNot Nothing Then
156+ config.AppSettings.Settings( key ).Value = value
157+ Else
158+ config.AppSettings.Settings.Add( key , value)
159+ End If
160+ config.Save(System.Configuration.ConfigurationSaveMode.Modified)
161+ System.Configuration.ConfigurationManager.RefreshSection( "appSettings" )
162+ Catch ex As Exception
163+ Debug.WriteLine( $"写入配置失败:{key},错误:{ex.Message}" )
164+ End Try
165+ End Sub
166+ End Class
167+
168+ <MarkupExtensionReturnType( GetType ( String ))>
169+ Public Class LocalizeExtension
170+ Inherits MarkupExtension
171+
172+ Private _key As String
173+
174+ Public Sub New ( key As String )
175+ _key = key
176+ End Sub
177+
178+ Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
179+ Return LanguageHelper.GetString(_key)
180+ End Function
181+ End Class
0 commit comments