| 1 | #Region "Microsoft.VisualBasic::fcbf335bc09ce18a54db64acd24da7d4, Microsoft.VisualBasic.Core\ApplicationServices\LanguageHelper\StringResources.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 StringResources |
| 35 | ' |
| 36 | ' Properties: [Default], Name, Resources |
| 37 | ' |
| 38 | ' Constructor: (+1 Overloads) Sub New |
| 39 | ' Function: __getLanguageResources, __getValue, SafelyGenerates, ToString |
| 40 | ' |
| 41 | ' |
| 42 | ' /********************************************************************************/ |
| 43 | |
| 44 | #End Region |
| 45 | |
| 46 | Imports System.Reflection |
| 47 | Imports Microsoft.VisualBasic.Linq.Extensions |
| 48 | |
| 49 | Namespace ApplicationServices.Globalization |
| 50 | |
| 51 | Public Class StringResources(Of TLanguage) |
| 52 | |
| 53 | Public ReadOnly Property Resources As Dictionary(Of TLanguage, LanguageAttribute) |
| 54 | Public ReadOnly Property Name As String |
| 55 | ''' <summary> |
| 56 | ''' 从属性或者域上面解析出来的默认的语言值 |
| 57 | ''' </summary> |
| 58 | ''' <returns></returns> |
| 59 | Public ReadOnly Property [Default] As String |
| 60 | |
| 61 | Sub New(name$, resources As LanguageAttribute()) |
| 62 | Me.Name = name |
| 63 | Me.Resources = resources _ |
| 64 | .ToDictionary(Function(lang) CTypeDynamic(Of TLanguage)(lang.Language), |
| 65 | Function(lang) lang) |
| 66 | End Sub |
| 67 | |
| 68 | Public Overrides Function ToString() As String |
| 69 | Return $"{Name}({Resources.Count}) = {[Default]}" |
| 70 | End Function |
| 71 | |
| 72 | Public Shared Function SafelyGenerates(member As MemberInfo) As StringResources(Of TLanguage) |
| 73 | Dim langResources = __getLanguageResources(member) |
| 74 | |
| 75 | If langResources.IsNullOrEmpty Then |
| 76 | Return Nothing |
| 77 | Else |
| 78 | Return New StringResources(Of TLanguage)(member.Name, langResources) With { |
| 79 | ._Default = __getValue(member) |
| 80 | } |
| 81 | End If |
| 82 | End Function |
| 83 | |
| 84 | Private Shared Function __getValue(member As MemberInfo) As String |
| 85 | Dim value As Object |
| 86 | |
| 87 | If (member.MemberType = MemberTypes.Property) Then |
| 88 | value = DirectCast(member, PropertyInfo).GetValue(Nothing, Nothing) |
| 89 | Else |
| 90 | value = DirectCast(member, FieldInfo).GetValue(Nothing) |
| 91 | End If |
| 92 | |
| 93 | Return Scripting.ToString(value) |
| 94 | End Function |
| 95 | |
| 96 | Private Shared Function __getLanguageResources(member As MemberInfo) As LanguageAttribute() |
| 97 | Dim attrs As Object() = member.GetCustomAttributes(attributeType:=GetType(LanguageAttribute), inherit:=True) |
| 98 | |
| 99 | If attrs.IsNullOrEmpty Then |
| 100 | Return Nothing |
| 101 | Else |
| 102 | Return attrs _ |
| 103 | .Select(Function(attr) DirectCast(attr, LanguageAttribute)) _ |
| 104 | .ToArray |
| 105 | End If |
| 106 | End Function |
| 107 | End Class |
| 108 | End Namespace |