Skip to content

Commit

Permalink
wip: 添加本地化描述特性
Browse files Browse the repository at this point in the history
  • Loading branch information
gmf520 committed Sep 6, 2024
1 parent 80c3fc9 commit 24551ae
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
22 changes: 11 additions & 11 deletions build/OSharpNS.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>OSharpNS</id>
<version>8.0.8</version>
<version>8.0.9</version>
<title>OSharpFramework(.NET6.0/.NETCoreApp3.1)</title>
<authors>柳柳软件(66soft.net)</authors>
<owners>LiuliuSoft nnc</owners>
Expand All @@ -16,18 +16,18 @@
<tags>osharp</tags>
<dependencies>
<group targetFramework=".NETCoreApp3.1">
<dependency id="OSharp.Core" version="8.0.8" />
<dependency id="OSharp.EntityFrameworkCore" version="8.0.8" />
<dependency id="OSharp.AutoMapper" version="8.0.8" />
<dependency id="OSharp.AspNetCore" version="8.0.8" />
<dependency id="OSharp.Log4Net" version="8.0.8" />
<dependency id="OSharp.Core" version="8.0.9" />
<dependency id="OSharp.EntityFrameworkCore" version="8.0.9" />
<dependency id="OSharp.AutoMapper" version="8.0.9" />
<dependency id="OSharp.AspNetCore" version="8.0.9" />
<dependency id="OSharp.Log4Net" version="8.0.9" />
</group>
<group targetFramework="net6.0">
<dependency id="OSharp.Core" version="8.0.8" />
<dependency id="OSharp.EntityFrameworkCore" version="8.0.8" />
<dependency id="OSharp.AutoMapper" version="8.0.8" />
<dependency id="OSharp.AspNetCore" version="8.0.8" />
<dependency id="OSharp.Log4Net" version="8.0.8" />
<dependency id="OSharp.Core" version="8.0.9" />
<dependency id="OSharp.EntityFrameworkCore" version="8.0.9" />
<dependency id="OSharp.AutoMapper" version="8.0.9" />
<dependency id="OSharp.AspNetCore" version="8.0.9" />
<dependency id="OSharp.Log4Net" version="8.0.9" />
</group>
</dependencies>
</metadata>
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionMain>8.0</VersionMain>
<VersionPrefix>8</VersionPrefix>
<VersionPrefix>9</VersionPrefix>
<VersionSuffix>-preview.</VersionSuffix>
<VersionSuffixVersion>529</VersionSuffixVersion>
<!--<Version>$(VersionMain).$(VersionPrefix)$(VersionSuffix)$(VersionSuffixVersion)</Version>
Expand Down
44 changes: 44 additions & 0 deletions src/OSharp.Utils/Localized/LocalizedDescriptionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -----------------------------------------------------------------------
// <copyright file="LocalizedDescriptionAttribute.cs" company="OSharp开源团队">
// Copyright (c) 2014-2024 OSharp. All rights reserved.
// </copyright>
// <site>http://www.osharp.org</site>
// <last-editor>gmf</last-editor>
// <last-date>2024-09-06 10:09</last-date>
// -----------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Globalization;
using System.Resources;


namespace OSharp.Localized
{
/// <summary>
/// 本地化描述类特性
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class LocalizedDescriptionAttribute : DescriptionAttribute
{
private readonly string _resourceKey;
private readonly ResourceManager _resourceManager;

public LocalizedDescriptionAttribute(string resourceKey, Type resourceType)
{
_resourceKey = resourceKey;
_resourceManager = new ResourceManager(resourceType);
}

/// <summary>Gets the description stored in this attribute.</summary>
/// <returns>The description stored in this attribute.</returns>
public override string Description
{
get
{
var description = _resourceManager.GetString(_resourceKey, CultureInfo.CurrentCulture);
return string.IsNullOrEmpty(description) ? $"[{_resourceKey}]" : description;
}
}
}
}
10 changes: 8 additions & 2 deletions src/OSharp.Utils/Reflection/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// <copyright file="TypeExtensions.cs" company="OSharp开源团队">
// Copyright (c) 2014-2015 OSharp. All rights reserved.
// </copyright>
Expand All @@ -21,6 +21,7 @@

using OSharp.Data;
using OSharp.Extensions;
using OSharp.Localized;


namespace OSharp.Reflection
Expand Down Expand Up @@ -104,6 +105,11 @@ public static string GetDescription(this Type type, bool inherit = true)
/// <returns>返回Description特性描述信息,如不存在则返回成员的名称</returns>
public static string GetDescription(this MemberInfo member, bool inherit = true)
{
LocalizedDescriptionAttribute localizedDesc = member.GetAttribute<LocalizedDescriptionAttribute>(inherit);
if (localizedDesc != null)
{
return localizedDesc.Description;
}
DescriptionAttribute desc = member.GetAttribute<DescriptionAttribute>(inherit);
if (desc != null)
{
Expand Down Expand Up @@ -443,4 +449,4 @@ private static void ProcessGenericType(StringBuilder builder, Type type, Type[]

#endregion
}
}
}

0 comments on commit 24551ae

Please sign in to comment.