当前文件路径:DNN/Library/Controls/LanguageSelector.vb 
1
'
2
' DotNetNuke?- http://www.dotnetnuke.com
3
' Copyright (c) 2002-2008
4
' by DotNetNuke Corporation
5
'
6
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
'
11
' The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
' of the Software.
13
'
14
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
' DEALINGS IN THE SOFTWARE.
19
'
20
21
Imports System
22
Imports System.Drawing
23
Imports System.Web.UI
24
Imports System.Web.UI.WebControls
25
Imports System.IO
26
Imports System.Collections
27
Imports System.Data
28
29
Imports DotNetNuke.Security.Roles
30
Imports DotNetNuke.Services.Localization
31
Imports DotNetNuke.UI.WebControls
32
Imports System.Globalization
33
34
Namespace DotNetNukeNamespace DotNetNuke.UI.WebControls
35
36
/**/''' -----------------------------------------------------------------------------
37
''' Project: DotNetNuke
38
''' Namespace: DotNetNuke.UI.WebControls
39
''' Class: LanguageSelector
40
''' -----------------------------------------------------------------------------
41
''' <summary>
42
''' Language Selector control
43
''' </summary>
44
''' <history>
45
''' [sleupold] 2007-11-10 created
46
''' [sleupold] 2007-12-08 Support for Languages ("de", "en") and Locales ("de-DE", "en-US") added
47
''' </history>
48
49
Public Class LanguageSelectorClass LanguageSelector
50
Inherits Control
51
Implements INamingContainer
52
53
Private pnlControl As Panel
54
55
Enums#Region "Enums"
56
/**/''' <summary>
57
''' Language Selection mode, offered to the user: single select or multiple select.
58
''' </summary>
59
Public Enum LanguageSelectionModeEnum LanguageSelectionMode
60
Multiple = 1
61
[Single] = 2
62
End Enum
63
64
/**/''' <summary>
65
''' Selection object: Language ("de", "en") or Locale ("de-DE", "en-US")
66
''' </summary>
67
Public Enum LanguageSelectionObjectEnum LanguageSelectionObject
68
NeutralCulture = 1
69
SpecificCulture = 2
70
End Enum
71
72
/**/''' <summary>
73
''' orientation, how elements (text or flags) are arranged
74
''' </summary>
75
Public Enum LanguageListDirectionEnum LanguageListDirection
76
Horizontal = 1
77
Vertical = 2
78
End Enum
79
80
/**/''' <summary>
81
''' display style for each item: Text, Flag, Both
82
''' </summary>
83
Public Enum LanguageItemStyleEnum LanguageItemStyle
84
FlagOnly = 1
85
FlagAndCaption = 2
86
CaptionOnly = 3
87
End Enum
88
#End Region
89
90
Public Properties#Region "Public Properties"
91
92
/**/''' <summary>
93
''' Gets or sets selection mode (single, multiple)
94
''' </summary>
95
Public Property SelectionMode()Property SelectionMode() As LanguageSelectionMode
96
Get
97
If ViewState("SelectionMode") Is Nothing Then
98
Return LanguageSelectionMode.Single
99
Else
100
Return CType(ViewState("SelectionMode"), LanguageSelectionMode)
101
End If
102
End Get
103
Set(ByVal value As LanguageSelectionMode)
104
If SelectionMode <> value Then
105
ViewState("SelectionMode") = value
106
If Me.Controls.Count > 0 Then CreateChildControls() 'Recreate if already created
107
End If
108
End Set
109
End Property
110
111
112
'' <summary>
113
'' Gets or sets the type of objects to be selectable: NeutralCulture ("de") or SpecificCulture ("de-DE")
114
'' </summary>
115
Public Property SelectionObject()Property SelectionObject() As LanguageSelectionObject
116
Get
117
If viewstate("SelectionObject") Is Nothing Then
118
Return LanguageSelectionObject.SpecificCulture
119
Else
120
Return CType(viewstate("SelectionObject"), LanguageSelectionObject)
121
End If
122
End Get
123
Set(ByVal Value As LanguageSelectionObject)
124
If SelectionMode <> Value Then
125
viewstate("SelectionObject") = Value
126
If Me.Controls.Count > 0 Then CreateChildControls() 'Recreate if already created
127
End If
128
End Set
129
End Property
130
131
/**/''' <summary>
132
''' Gets or sets the style of the language items
133
''' </summary>
134
Public Property ItemStyle()Property ItemStyle() As LanguageItemStyle
135
Get
136
If ViewState("ItemStyle") Is Nothing Then
137
Return LanguageItemStyle.FlagAndCaption
138
Else
139
Return CType(ViewState("ItemStyle"), LanguageItemStyle)
140
End If
141
End Get
142
Set(ByVal value As LanguageItemStyle)
143
If ItemStyle <> value Then
144
ViewState("ItemStyle") = value
145
If Me.Controls.Count > 0 Then CreateChildControls() 'Recreate if already created
146
End If
147
End Set
148
End Property
149
150
/**/''' <summary>
151
''' Gets or sets the direction of the language list
152
''' </summary>
153
Public Property ListDirection()Property ListDirection() As LanguageListDirection
154
Get
155
If ViewState("ListDirection") Is Nothing Then
156
Return LanguageListDirection.Vertical
157
Else
158
Return CType(ViewState("ListDirection"), LanguageListDirection)
159
End If
160
End Get
161
Set(ByVal value As LanguageListDirection)
162
If ListDirection <> value Then
163
ViewState("ListDirection") = value
164
If Me.Controls.Count > 0 Then CreateChildControls() 'Recreate if already created
165
End If
166
End Set
167
End Property
168
169
/**/''' <summary>
170
''' Gets or sets the list of selected languages
171
''' </summary>
172
Public Property SelectedLanguages()Property SelectedLanguages() As String()
173
Get
174
EnsureChildControls()
175
Dim a As New ArrayList
176
If GetCultures(SelectionObject = LanguageSelectionObject.SpecificCulture).Length < 2 Then
177
'return single language
178
For Each strLocale As String In Localization.GetEnabledLocales
179
a.Add(strLocale)
180
Next
181
Else
182
'create list of selected languages
183
For Each c As CultureInfo In GetCultures(SelectionObject = LanguageSelectionObject.SpecificCulture)
184
If SelectionMode = LanguageSelectionMode.Single Then
185
If CType(pnlControl.FindControl("opt" & c.Name), RadioButton).Checked Then a.Add(c.Name)
186
Else
187
If CType(pnlControl.FindControl("chk" & c.Name), CheckBox).Checked Then a.Add(c.Name)
188
End If
189
Next
190
End If
191
Return CType(a.ToArray(GetType(String)), String())
192
End Get
193
Set(ByVal value As String())
194
EnsureChildControls()
195
If SelectionMode = LanguageSelectionMode.Single And value.Length > 1 Then Throw New ArgumentException("Selection mode 'single' cannot have more than one selected item.")
196
For Each c As CultureInfo In GetCultures(SelectionObject = LanguageSelectionObject.SpecificCulture)
197
If SelectionMode = LanguageSelectionMode.Single Then
198
CType(pnlControl.FindControl("opt" & c.Name), RadioButton).Checked = False
199
Else
200
CType(pnlControl.FindControl("chk" & c.Name), CheckBox).Checked = False
201
End If
202
Next
203
For Each strLocale As String In value
204
If SelectionMode = LanguageSelectionMode.Single Then
205
Dim ctl As Control = pnlControl.FindControl("opt" & strLocale)
206
If Not ctl Is Nothing Then CType(ctl, RadioButton).Checked = True
207
Else
208
Dim ctl As Control = pnlControl.FindControl("chk" & strLocale)
209
If Not ctl Is Nothing Then CType(ctl, CheckBox).Checked = True
210
End If
211
Next
212
End Set
213
End Property
214
#End Region
215
216
Protected Methods#Region "Protected Methods"
217
218
/**/''' <summary>
219
''' Create Child Controls
220
''' </summary>
221
Protected Overrides Sub CreateChildControls()Sub CreateChildControls()
222
Me.Controls.Clear()
223
pnlControl = New Panel
224
Me.Controls.Add(pnlControl)
225
226
For Each c As CultureInfo In GetCultures(SelectionObject = LanguageSelectionObject.SpecificCulture)
227
Dim lblLocale As New HtmlControls.HtmlGenericControl("label")
228
If SelectionMode = LanguageSelectionMode.Single Then
229
Dim optLocale As New RadioButton
230
optLocale.ID = "opt" & c.Name
231
optLocale.GroupName = pnlControl.ID & "_Locale"
232
If c.Name = Localization.SystemLocale Then optLocale.Checked = True
233
pnlControl.Controls.Add(optLocale)
234
lblLocale.Attributes("for") = optLocale.ClientID
235
Else
236
Dim chkLocale As New CheckBox
237
chkLocale.ID = "chk" & c.Name
238
pnlControl.Controls.Add(chkLocale)
239
lblLocale.Attributes("for") = chkLocale.ClientID
240
End If
241
pnlControl.Controls.Add(lblLocale)
242
If ItemStyle <> LanguageItemStyle.CaptionOnly Then
243
Dim imgLocale As New System.Web.UI.WebControls.Image
244
imgLocale.ImageUrl = ResolveUrl("~/images/Flags/" & c.Name & ".gif")
245
imgLocale.AlternateText = c.DisplayName
246
imgLocale.Style("vertical-align") = "middle"
247
lblLocale.Controls.Add(imgLocale)
248
End If
249
If ItemStyle <> LanguageItemStyle.FlagOnly Then
250
lblLocale.Attributes("class") = "Normal"
251
lblLocale.Controls.Add(New LiteralControl(" " & c.DisplayName))
252
End If
253
If ListDirection = LanguageListDirection.Vertical Then
254
pnlControl.Controls.Add(New LiteralControl("<br />"))
255
Else
256
pnlControl.Controls.Add(New LiteralControl(" "))
257
End If
258
Next
259
'Hide if not more than one language
260
If GetCultures(SelectionObject = LanguageSelectionObject.SpecificCulture).Length < 2 Then
261
Me.Visible = False
262
End If
263
End Sub
264
265
#End Region
266
267
Private Methods#Region " Private Methods "
268
/**/''' <summary>
269
''' retrieve the cultures, currently supported by the portal
270
''' </summary>
271
''' <param name="specific">true: locales, false: neutral languages</param>
272
''' <returns>Array of cultures</returns>
273
Private Function GetCultures()Function GetCultures(ByVal specific As Boolean) As System.Globalization.CultureInfo()
274
Dim a As New ArrayList
275
For Each l As String In DotNetNuke.Services.Localization.Localization.GetEnabledLocales
276
Dim c As New System.Globalization.CultureInfo(l)
277
If specific Then
278
a.Add(c)
279
Else
280
Dim p As System.Globalization.CultureInfo = c.Parent
281
If Not a.Contains(p) Then a.Add(p)
282
End If
283
Next
284
Return CType(a.ToArray(GetType(CultureInfo)), System.Globalization.CultureInfo())
285
End Function
286
#End Region
287
288
End Class