51 lines
2 KiB
C#
51 lines
2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Localization;
|
|
using Volo.Abp.Localization;
|
|
|
|
namespace Sozsoft.Languages.Localization;
|
|
|
|
public class DatabaseLocalizationResourceContributor : ILocalizationResourceContributor
|
|
{
|
|
private LocalizationResourceBase _resource;
|
|
private System.Lazy<IDatabaseResourceLocalizer> _databaseResourceLocalizer;
|
|
private System.Lazy<ILanguageProvider> _languageProvider;
|
|
|
|
public bool IsDynamic => false;
|
|
|
|
public void Initialize(LocalizationResourceInitializationContext context)
|
|
{
|
|
_resource = context.Resource;
|
|
// ABP calls Initialize while AbpStringLocalizerFactory holds its internal lock.
|
|
// Resolving repositories/caches here can request another IStringLocalizer and
|
|
// re-enter the same lock, preventing Kestrel from ever starting.
|
|
_databaseResourceLocalizer = new System.Lazy<IDatabaseResourceLocalizer>(
|
|
() => context.ServiceProvider.GetRequiredService<IDatabaseResourceLocalizer>());
|
|
_languageProvider = new System.Lazy<ILanguageProvider>(
|
|
() => context.ServiceProvider.GetRequiredService<ILanguageProvider>());
|
|
}
|
|
|
|
public LocalizedString GetOrNull(string cultureName, string name)
|
|
{
|
|
return _databaseResourceLocalizer.Value.GetOrNull(_resource, cultureName, name);
|
|
}
|
|
|
|
public async Task FillAsync(string cultureName, Dictionary<string, LocalizedString> dictionary)
|
|
{
|
|
await _databaseResourceLocalizer.Value.FillAsync(_resource, cultureName, dictionary);
|
|
}
|
|
|
|
public void Fill(string cultureName, Dictionary<string, LocalizedString> dictionary)
|
|
{
|
|
_databaseResourceLocalizer.Value.Fill(_resource, cultureName, dictionary);
|
|
}
|
|
|
|
public async Task<IEnumerable<string>> GetSupportedCulturesAsync()
|
|
{
|
|
var langs = await _languageProvider.Value.GetLanguagesAsync();
|
|
return langs.Select(a => a.CultureName).ToList();
|
|
}
|
|
}
|
|
|