mirror of https://github.com/abpframework/abp.git
13 changed files with 207 additions and 84 deletions
@ -0,0 +1,54 @@ |
|||
using Volo.Abp.MultiLingualObject; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace AutoMapper |
|||
{ |
|||
public static class AbpAutoMapperMultiLingualDtoExtensions |
|||
{ |
|||
public static CreateMultiLingualMapResult<TSource, TTranslation, TDestination> CreateMultiLingualMap<TSource, TTranslation, TDestination>(this Profile profile) |
|||
where TTranslation : class, IMultiLingualTranslation |
|||
where TSource : IHasMultiLingual<TTranslation> |
|||
{ |
|||
|
|||
return new( |
|||
profile.CreateMap<TSource, TDestination>().BeforeMap<AbpMultiLingualMapperAction<TSource, TTranslation, TDestination>>(), |
|||
profile.CreateMap<TTranslation, TDestination>()); |
|||
} |
|||
} |
|||
|
|||
public class AbpMultiLingualMapperAction<TSource, TTranslation, TDestination> : IMappingAction<TSource, TDestination> |
|||
where TTranslation : class, IMultiLingualTranslation |
|||
where TSource : IHasMultiLingual<TTranslation> |
|||
{ |
|||
private readonly IMultiLingualObjectManager _multiLingualObjectManager; |
|||
|
|||
public AbpMultiLingualMapperAction(IMultiLingualObjectManager multiLingualObjectManager) |
|||
{ |
|||
_multiLingualObjectManager = multiLingualObjectManager; |
|||
} |
|||
|
|||
public void Process(TSource source, TDestination destination, ResolutionContext context) |
|||
{ |
|||
var translation = AsyncHelper.RunSync(() => _multiLingualObjectManager.GetTranslationAsync<TSource, TTranslation>(source)); |
|||
if (translation != null) |
|||
{ |
|||
context.Mapper.Map(translation, destination); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public class CreateMultiLingualMapResult<TSource, TTranslation, TDestination> |
|||
{ |
|||
public IMappingExpression<TSource, TDestination> EntityMap { get; } |
|||
|
|||
public IMappingExpression<TTranslation, TDestination> TranslateMap { get; } |
|||
|
|||
public CreateMultiLingualMapResult( |
|||
IMappingExpression<TSource, TDestination> entityMap, |
|||
IMappingExpression<TTranslation, TDestination> translateMap) |
|||
{ |
|||
EntityMap = entityMap; |
|||
TranslateMap = translateMap; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.MultiLingualObject.TestObjects; |
|||
using Volo.Abp.Testing; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.MultiLingualObject |
|||
{ |
|||
public class MultiLingualObjectManager_Tests : AbpIntegratedTest<AbpMultiLingualObjectTestModule> |
|||
{ |
|||
private readonly IMultiLingualObjectManager _multiLingualObjectManager; |
|||
private readonly MultiLingualBook _book; |
|||
|
|||
public MultiLingualObjectManager_Tests() |
|||
{ |
|||
_multiLingualObjectManager = ServiceProvider.GetRequiredService<IMultiLingualObjectManager>(); |
|||
|
|||
var id = Guid.NewGuid(); |
|||
_book = new MultiLingualBook(id, 100) |
|||
{ |
|||
Translations = new List<MultiLingualBookTranslation>() |
|||
}; |
|||
|
|||
var en = new MultiLingualBookTranslation |
|||
{ |
|||
Language = "en", |
|||
Name = "C# in Depth", |
|||
}; |
|||
var zh = new MultiLingualBookTranslation |
|||
{ |
|||
Language = "zh-Hans", |
|||
Name = "深入理解C#", |
|||
}; |
|||
|
|||
_book.Translations.Add(en); |
|||
_book.Translations.Add(zh); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetTranslation() |
|||
{ |
|||
using (CultureHelper.Use("en-us")) |
|||
{ |
|||
var translation = _multiLingualObjectManager.GetTranslation<MultiLingualBook, MultiLingualBookTranslation>(_book); |
|||
|
|||
translation.Name.ShouldBe("C# in Depth"); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetTranslationAsync() |
|||
{ |
|||
using (CultureHelper.Use("en-us")) |
|||
{ |
|||
var translation = await _multiLingualObjectManager.GetTranslationAsync<MultiLingualBook, MultiLingualBookTranslation>(_book); |
|||
|
|||
translation.Name.ShouldBe("C# in Depth"); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Get_Specified_Language() |
|||
{ |
|||
using (CultureHelper.Use("zh-Hans")) |
|||
{ |
|||
var translation = await _multiLingualObjectManager.GetTranslationAsync<MultiLingualBook, MultiLingualBookTranslation>(_book, culture: "en"); |
|||
|
|||
translation.Name.ShouldBe("C# in Depth"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue