Abp模块
abp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

85 lines
3.0 KiB

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Sanhe.Abp.Localization.Dynamic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization;
namespace Sanhe.Abp.LocalizationManagement;
[Dependency(ServiceLifetime.Singleton, ReplaceServices = true)]
[ExposeServices(
typeof(ILocalizationStore),
typeof(LocalizationStore))]
public class LocalizationStore : ILocalizationStore
{
protected ILanguageRepository LanguageRepository { get; }
protected ITextRepository TextRepository { get; }
protected IResourceRepository ResourceRepository { get; }
public LocalizationStore(
ILanguageRepository languageRepository,
ITextRepository textRepository,
IResourceRepository resourceRepository)
{
TextRepository = textRepository;
LanguageRepository = languageRepository;
ResourceRepository = resourceRepository;
}
public async virtual Task<List<LanguageInfo>> GetLanguageListAsync(
CancellationToken cancellationToken = default)
{
var languages = await LanguageRepository.GetActivedListAsync(cancellationToken);
return languages
.Select(x => new LanguageInfo(x.CultureName, x.UiCultureName, x.DisplayName, x.FlagIcon))
.ToList();
}
public async virtual Task<Dictionary<string, ILocalizationDictionary>> GetLocalizationDictionaryAsync(
string resourceName,
CancellationToken cancellationToken = default)
{
// TODO: 引用缓存?
var dictionaries = new Dictionary<string, ILocalizationDictionary>();
var resource = await ResourceRepository.FindByNameAsync(resourceName, cancellationToken);
if (resource == null || !resource.Enable)
{
// 资源不存在或未启用返回空
return dictionaries;
}
var texts = await TextRepository.GetListAsync(resourceName, cancellationToken);
foreach (var textGroup in texts.GroupBy(x => x.CultureName))
{
var cultureTextDictionaires = new Dictionary<string, LocalizedString>();
foreach (var text in textGroup)
{
// 本地化名称去重
if (!cultureTextDictionaires.ContainsKey(text.Key))
{
cultureTextDictionaires[text.Key] = new LocalizedString(text.Key, text.Value.NormalizeLineEndings());
}
}
// 本地化语言去重
if (!dictionaries.ContainsKey(textGroup.Key))
{
dictionaries[textGroup.Key] = new StaticLocalizationDictionary(textGroup.Key, cultureTextDictionaires);
}
}
return dictionaries;
}
public async virtual Task<bool> ResourceExistsAsync(string resourceName, CancellationToken cancellationToken = default)
{
return await ResourceRepository.ExistsAsync(resourceName, cancellationToken);
}
}