1 #Region "Microsoft.VisualBasic::ce29b6690c41276a786f009ed03b2233, Microsoft.VisualBasic.Core\ApplicationServices\LanguageHelper\LanguageHelper.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class LanguageHelper
35     
36     '         Properties: CurrentLanguage, DeclaringType, LanguageResources
37     
38     '         Constructor: (+1 OverloadsSub New
39     
40     '         FunctionToString
41     
42     '         Sub: __init
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.Reflection
50 Imports System.Runtime.CompilerServices
51 Imports Microsoft.VisualBasic.Language
52
53 Namespace ApplicationServices.Globalization
54
55     ''' <summary>
56     ''' 
57     ''' </summary>
58     ''' <typeparam name="TLanguage">必须是枚举类型</typeparam>
59     Public Class LanguageHelper(Of TLanguage)
60
61         Public ReadOnly Property LanguageResources As SortedDictionary(Of StringStringResources(Of TLanguage))
62         Public ReadOnly Property DeclaringType As Type
63
64         Public Property CurrentLanguage As TLanguage
65
66         ''' <summary>
67         ''' Get the string that store as specific language.
68         ''' (获取指定语言的字符串)
69         ''' </summary>
70         ''' <param name="Language">字符串的语言</param>
71         ''' <value></value>
72         ''' <param name="res">请使用 NameOf 操作符来获取</param>
73         ''' <returns></returns>
74         ''' <remarks></remarks>
75         Default Public ReadOnly Property Text(Language As TLanguage, res As StringAs String
76             Get
77                 If Not _LanguageResources.ContainsKey(res) Then
78                     Return ""
79                 Else
80                     Dim resource = _LanguageResources(res)
81
82                     If resource.Resources.ContainsKey(Language) Then
83                         Return resource.Resources(Language).Text
84                     Else
85                         Return resource.Default
86                     End If
87                 End If
88             End Get
89         End Property
90
91         Default Public ReadOnly Property Text(res As StringAs String
92             <MethodImpl(MethodImplOptions.AggressiveInlining)>
93             Get
94                 Return Text(CurrentLanguage, res)
95             End Get
96         End Property
97
98         Public Overrides Function ToString() As String
99             Return $"{DeclaringType.FullName}@{DirectCast(DirectCast(CurrentLanguage, Object), [Enum]).Description}"
100         End Function
101
102         ''' <summary>
103         ''' 解析出所有属性,域上面的语言定义
104         ''' </summary>
105         ''' <param name="type">必须是模块类型或者属性或者域是共享类型的</param>
106         Sub New(type As Type)
107             Dim Propertys = type.GetProperties(BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static)
108             Dim Fields = type.GetFields(BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static)
109             Dim Members As New List(Of MemberInfo)
110
111             Call Members.AddRange(Propertys)
112             Call Members.AddRange(Fields)
113             Call __init(Members)
114         End Sub
115
116         ''' <summary>
117         ''' 
118         ''' </summary>
119         ''' <param name="members"></param>
120         Private Sub __init(members As IEnumerable(Of MemberInfo))
121             Dim LQuery = LinqAPI.Exec(Of StringResources(Of TLanguage)) _
122  _
123                 () <= From member As MemberInfo
124                       In members
125                       Let res As StringResources(Of TLanguage) = StringResources(Of TLanguage).SafelyGenerates(member)
126                       Where Not res Is Nothing
127                       Select res
128
129             _LanguageResources = New SortedDictionary(Of StringStringResources(Of TLanguage))(LQuery.ToDictionary(Function(res) res.Name))
130             _DeclaringType = members(Scan0).DeclaringType
131         End Sub
132     End Class
133 End Namespace