mirror of https://github.com/abpframework/abp.git
10 changed files with 202 additions and 0 deletions
@ -0,0 +1,99 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Globalization; |
|||
using System.Linq; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.MultiLingualEntities; |
|||
using Volo.Abp.Settings; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace AutoMapper |
|||
{ |
|||
public static class AbpAutoMapperMultiLingualDtoExtensions |
|||
{ |
|||
public static CreateMultiLingualMapResult<TMultiLingualEntity, TTranslation, TDestination> CreateMultLingualMap< |
|||
TMultiLingualEntity, |
|||
TTranslation, TDestination>( |
|||
this Profile profile, |
|||
ISettingProvider serviceProvider, |
|||
bool fallbackToParentCultures = false) |
|||
where TMultiLingualEntity : class, IMultiLingualEntity<TTranslation> |
|||
where TTranslation : class, IEntityTranslation |
|||
{ |
|||
return new CreateMultiLingualMapResult<TMultiLingualEntity, TTranslation, TDestination> |
|||
{ |
|||
TranslationMap = profile.CreateMap<TTranslation, TDestination>(), |
|||
EntityMap = profile.CreateMap<TMultiLingualEntity, TDestination>().BeforeMap( |
|||
(source, destination, context) => |
|||
{ |
|||
if (source.Translations == null || !source.Translations.Any()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var translation = |
|||
source.Translations.FirstOrDefault(pt => pt.Language == CultureInfo.CurrentUICulture.Name); |
|||
if (translation != null) |
|||
{ |
|||
context.Mapper.Map(translation, destination); |
|||
return; |
|||
} |
|||
|
|||
if (fallbackToParentCultures) |
|||
{ |
|||
translation = |
|||
GeTranslationBasedOnCulturalRecursive<TMultiLingualEntity, TTranslation>( |
|||
CultureInfo.CurrentUICulture.Parent, source.Translations, 0); |
|||
if (translation != null) |
|||
{ |
|||
context.Mapper.Map(translation, destination); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
var defaultLanguage = AsyncHelper.RunSync(() => |
|||
serviceProvider.GetOrNullAsync(LocalizationSettingNames.DefaultLanguage)); |
|||
|
|||
translation = source.Translations.FirstOrDefault(pt => pt.Language == defaultLanguage); |
|||
if (translation != null) |
|||
{ |
|||
context.Mapper.Map(translation, destination); |
|||
return; |
|||
} |
|||
|
|||
translation = source.Translations.FirstOrDefault(); |
|||
if (translation != null) |
|||
{ |
|||
context.Mapper.Map(translation, destination); |
|||
} |
|||
}) |
|||
}; |
|||
} |
|||
|
|||
private const int MaxCultureFallbackDepth = 5; |
|||
|
|||
private static TTranslation GeTranslationBasedOnCulturalRecursive<TMultiLingualEntity, TTranslation>( |
|||
CultureInfo culture, ICollection<TTranslation> translations, int currentDepth) |
|||
where TTranslation : class, IEntityTranslation |
|||
{ |
|||
if (culture == null || culture.Name.IsNullOrWhiteSpace() || translations.IsNullOrEmpty() || |
|||
currentDepth > MaxCultureFallbackDepth) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
var translation = translations.FirstOrDefault(pt => |
|||
pt.Language.Equals(culture.Name, StringComparison.OrdinalIgnoreCase)); |
|||
return translation ?? |
|||
GeTranslationBasedOnCulturalRecursive<TMultiLingualEntity, TTranslation>(culture.Parent, |
|||
translations, currentDepth + 1); |
|||
} |
|||
} |
|||
|
|||
public class CreateMultiLingualMapResult<TMultiLingualEntity, TTranslation, TDestination> |
|||
{ |
|||
public IMappingExpression<TTranslation, TDestination> TranslationMap { get; set; } |
|||
|
|||
public IMappingExpression<TMultiLingualEntity, TDestination> EntityMap { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -0,0 +1,30 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
|||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
|||
<xs:element name="Weavers"> |
|||
<xs:complexType> |
|||
<xs:all> |
|||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
|||
<xs:complexType> |
|||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:all> |
|||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
|||
<xs:annotation> |
|||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:schema> |
|||
@ -0,0 +1,11 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\configureawait.props" /> |
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,19 @@ |
|||
namespace Volo.Abp.MultiLingualEntities |
|||
{ |
|||
public interface IEntityTranslation |
|||
{ |
|||
string Language { get; set; } |
|||
} |
|||
|
|||
public interface IEntityTranslation<TEntity, TPrimaryKeyOfMultiLingualEntity> : IEntityTranslation |
|||
{ |
|||
TEntity Core { get; set; } |
|||
|
|||
TPrimaryKeyOfMultiLingualEntity CoreId { get; set; } |
|||
} |
|||
|
|||
public interface IEntityTranslation<TEntity>: IEntityTranslation<TEntity, int> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.MultiLingualEntities |
|||
{ |
|||
public interface IMultiLingualEntity<TTranslation> |
|||
where TTranslation : class, IEntityTranslation |
|||
{ |
|||
ICollection<TTranslation> Translations { get; set; } |
|||
} |
|||
} |
|||
Loading…
Reference in new issue