|
|
@ -2,6 +2,7 @@ |
|
|
using Microsoft.Extensions.Localization; |
|
|
using Microsoft.Extensions.Localization; |
|
|
using System; |
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
|
|
|
using System.Globalization; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
using System.Threading; |
|
|
using System.Threading; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
@ -18,29 +19,25 @@ namespace LINGYUN.Abp.LocalizationManagement |
|
|
typeof(LocalizationStore))] |
|
|
typeof(LocalizationStore))] |
|
|
public class LocalizationStore : IExternalLocalizationStore |
|
|
public class LocalizationStore : IExternalLocalizationStore |
|
|
{ |
|
|
{ |
|
|
protected ILanguageRepository LanguageRepository { get; } |
|
|
protected IServiceProvider ServiceProvider { get; } |
|
|
protected ITextRepository TextRepository { get; } |
|
|
protected ILocalizationStoreCache LocalizationStoreCache { get; } |
|
|
protected IResourceRepository ResourceRepository { get; } |
|
|
|
|
|
|
|
|
|
|
|
public LocalizationStore( |
|
|
public LocalizationStore( |
|
|
ILanguageRepository languageRepository, |
|
|
IServiceProvider serviceProvider, |
|
|
ITextRepository textRepository, |
|
|
ILocalizationStoreCache localizationStoreCache) |
|
|
IResourceRepository resourceRepository) |
|
|
|
|
|
{ |
|
|
{ |
|
|
TextRepository = textRepository; |
|
|
ServiceProvider = serviceProvider; |
|
|
LanguageRepository = languageRepository; |
|
|
LocalizationStoreCache = localizationStoreCache; |
|
|
ResourceRepository = resourceRepository; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Obsolete("The framework already supports dynamic languages and will be deprecated in the next release")] |
|
|
[Obsolete("The framework already supports dynamic languages and will be deprecated in the next release")] |
|
|
public async virtual Task<List<LanguageInfo>> GetLanguageListAsync( |
|
|
public async virtual Task<List<LanguageInfo>> GetLanguageListAsync( |
|
|
CancellationToken cancellationToken = default) |
|
|
CancellationToken cancellationToken = default) |
|
|
{ |
|
|
{ |
|
|
var languages = await LanguageRepository.GetActivedListAsync(cancellationToken); |
|
|
var context = new LocalizationStoreCacheInitializeContext(ServiceProvider); |
|
|
|
|
|
await LocalizationStoreCache.InitializeAsync(context); |
|
|
|
|
|
|
|
|
return languages |
|
|
return LocalizationStoreCache.GetLanguages().ToList(); |
|
|
.Select(x => new LanguageInfo(x.CultureName, x.UiCultureName, x.DisplayName, x.FlagIcon)) |
|
|
|
|
|
.ToList(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Obsolete("The framework already supports dynamic languages and will be deprecated in the next release")] |
|
|
[Obsolete("The framework already supports dynamic languages and will be deprecated in the next release")] |
|
|
@ -48,26 +45,31 @@ namespace LINGYUN.Abp.LocalizationManagement |
|
|
string resourceName, |
|
|
string resourceName, |
|
|
CancellationToken cancellationToken = default) |
|
|
CancellationToken cancellationToken = default) |
|
|
{ |
|
|
{ |
|
|
// TODO: 引用缓存?
|
|
|
|
|
|
var dictionaries = new Dictionary<string, ILocalizationDictionary>(); |
|
|
var dictionaries = new Dictionary<string, ILocalizationDictionary>(); |
|
|
var resource = await ResourceRepository.FindByNameAsync(resourceName, cancellationToken); |
|
|
|
|
|
if (resource == null || !resource.Enable) |
|
|
var context = new LocalizationStoreCacheInitializeContext(ServiceProvider); |
|
|
|
|
|
await LocalizationStoreCache.InitializeAsync(context); |
|
|
|
|
|
|
|
|
|
|
|
var resource = LocalizationStoreCache.GetResourceOrNull(resourceName); |
|
|
|
|
|
|
|
|
|
|
|
if (resource == null) |
|
|
{ |
|
|
{ |
|
|
// 资源不存在或未启用返回空
|
|
|
// 资源不存在或未启用返回空
|
|
|
return dictionaries; |
|
|
return dictionaries; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var texts = await TextRepository.GetListAsync(resourceName, null, cancellationToken); |
|
|
var texts = LocalizationStoreCache.GetAllLocalizedStrings(CultureInfo.CurrentCulture.Name); |
|
|
|
|
|
|
|
|
foreach (var textGroup in texts.GroupBy(x => x.CultureName)) |
|
|
foreach (var textGroup in texts) |
|
|
{ |
|
|
{ |
|
|
var cultureTextDictionaires = new Dictionary<string, LocalizedString>(); |
|
|
var cultureTextDictionaires = new Dictionary<string, LocalizedString>(); |
|
|
foreach (var text in textGroup) |
|
|
|
|
|
|
|
|
foreach (var text in textGroup.Value) |
|
|
{ |
|
|
{ |
|
|
// 本地化名称去重
|
|
|
// 本地化名称去重
|
|
|
if (!cultureTextDictionaires.ContainsKey(text.Key)) |
|
|
if (!cultureTextDictionaires.ContainsKey(text.Key)) |
|
|
{ |
|
|
{ |
|
|
cultureTextDictionaires[text.Key] = new LocalizedString(text.Key, text.Value.NormalizeLineEndings()); |
|
|
cultureTextDictionaires[text.Key] = new LocalizedString(text.Key, text.Value.Value.NormalizeLineEndings()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -85,27 +87,28 @@ namespace LINGYUN.Abp.LocalizationManagement |
|
|
public async virtual Task<Dictionary<string, Dictionary<string, ILocalizationDictionary>>> GetAllLocalizationDictionaryAsync(CancellationToken cancellationToken = default) |
|
|
public async virtual Task<Dictionary<string, Dictionary<string, ILocalizationDictionary>>> GetAllLocalizationDictionaryAsync(CancellationToken cancellationToken = default) |
|
|
{ |
|
|
{ |
|
|
var result = new Dictionary<string, Dictionary<string, ILocalizationDictionary>>(); |
|
|
var result = new Dictionary<string, Dictionary<string, ILocalizationDictionary>>(); |
|
|
var textList = await TextRepository.GetListAsync(resourceName: null, cancellationToken: cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var resourcesGroup in textList.GroupBy(x => x.ResourceName)) |
|
|
var context = new LocalizationStoreCacheInitializeContext(ServiceProvider); |
|
|
|
|
|
await LocalizationStoreCache.InitializeAsync(context); |
|
|
|
|
|
|
|
|
|
|
|
var textList = LocalizationStoreCache.GetAllLocalizedStrings(CultureInfo.CurrentCulture.Name); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var resourcesGroup in textList) |
|
|
{ |
|
|
{ |
|
|
var dictionaries = new Dictionary<string, ILocalizationDictionary>(); |
|
|
var dictionaries = new Dictionary<string, ILocalizationDictionary>(); |
|
|
foreach (var textGroup in resourcesGroup.GroupBy(x => x.CultureName)) |
|
|
foreach (var text in resourcesGroup.Value) |
|
|
{ |
|
|
{ |
|
|
var cultureTextDictionaires = new Dictionary<string, LocalizedString>(); |
|
|
var cultureTextDictionaires = new Dictionary<string, LocalizedString>(); |
|
|
foreach (var text in textGroup) |
|
|
|
|
|
{ |
|
|
|
|
|
// 本地化名称去重
|
|
|
// 本地化名称去重
|
|
|
if (!cultureTextDictionaires.ContainsKey(text.Key)) |
|
|
if (!cultureTextDictionaires.ContainsKey(text.Key)) |
|
|
{ |
|
|
{ |
|
|
cultureTextDictionaires[text.Key] = new LocalizedString(text.Key, text.Value.NormalizeLineEndings()); |
|
|
cultureTextDictionaires[text.Key] = new LocalizedString(text.Key, text.Value.Value.NormalizeLineEndings()); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 本地化语言去重
|
|
|
// 本地化语言去重
|
|
|
if (!dictionaries.ContainsKey(textGroup.Key)) |
|
|
if (!dictionaries.ContainsKey(text.Key)) |
|
|
{ |
|
|
{ |
|
|
dictionaries[textGroup.Key] = new StaticLocalizationDictionary(textGroup.Key, cultureTextDictionaires); |
|
|
dictionaries[text.Key] = new StaticLocalizationDictionary(text.Key, cultureTextDictionaires); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -118,40 +121,41 @@ namespace LINGYUN.Abp.LocalizationManagement |
|
|
[Obsolete("The framework already supports dynamic languages and will be deprecated in the next release")] |
|
|
[Obsolete("The framework already supports dynamic languages and will be deprecated in the next release")] |
|
|
public async virtual Task<bool> ResourceExistsAsync(string resourceName, CancellationToken cancellationToken = default) |
|
|
public async virtual Task<bool> ResourceExistsAsync(string resourceName, CancellationToken cancellationToken = default) |
|
|
{ |
|
|
{ |
|
|
return await ResourceRepository.ExistsAsync(resourceName, cancellationToken); |
|
|
var context = new LocalizationStoreCacheInitializeContext(ServiceProvider); |
|
|
|
|
|
await LocalizationStoreCache.InitializeAsync(context); |
|
|
|
|
|
|
|
|
|
|
|
return LocalizationStoreCache.GetResourceOrNull(resourceName) != null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public LocalizationResourceBase GetResourceOrNull(string resourceName) |
|
|
public LocalizationResourceBase GetResourceOrNull(string resourceName) |
|
|
{ |
|
|
{ |
|
|
return GetResourceOrNullAsync(resourceName) |
|
|
return AsyncHelper.RunSync(async () => await GetResourceOrNullAsync(resourceName)); |
|
|
.ConfigureAwait(continueOnCapturedContext: false) |
|
|
|
|
|
.GetAwaiter() |
|
|
|
|
|
.GetResult(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async virtual Task<LocalizationResourceBase> GetResourceOrNullAsync(string resourceName) |
|
|
public async virtual Task<LocalizationResourceBase> GetResourceOrNullAsync(string resourceName) |
|
|
{ |
|
|
{ |
|
|
var resource = await ResourceRepository.FindByNameAsync(resourceName); |
|
|
var context = new LocalizationStoreCacheInitializeContext(ServiceProvider); |
|
|
if (resource == null) |
|
|
await LocalizationStoreCache.InitializeAsync(context); |
|
|
{ |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return new NonTypedLocalizationResource(resource.Name); |
|
|
return LocalizationStoreCache.GetResourceOrNull(resourceName); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async virtual Task<string[]> GetResourceNamesAsync() |
|
|
public async virtual Task<string[]> GetResourceNamesAsync() |
|
|
{ |
|
|
{ |
|
|
var resources = await ResourceRepository.GetListAsync(); |
|
|
var context = new LocalizationStoreCacheInitializeContext(ServiceProvider); |
|
|
|
|
|
await LocalizationStoreCache.InitializeAsync(context); |
|
|
|
|
|
|
|
|
return resources.Select(r => r.Name).ToArray(); |
|
|
return LocalizationStoreCache.GetResources() |
|
|
|
|
|
.Select(x => x.ResourceName) |
|
|
|
|
|
.ToArray(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async virtual Task<LocalizationResourceBase[]> GetResourcesAsync() |
|
|
public async virtual Task<LocalizationResourceBase[]> GetResourcesAsync() |
|
|
{ |
|
|
{ |
|
|
var resources = await ResourceRepository.GetListAsync(); |
|
|
var context = new LocalizationStoreCacheInitializeContext(ServiceProvider); |
|
|
|
|
|
await LocalizationStoreCache.InitializeAsync(context); |
|
|
|
|
|
|
|
|
return resources.Select(r => new NonTypedLocalizationResource(r.Name)).ToArray(); |
|
|
return LocalizationStoreCache.GetResources().ToArray(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|