Last active
July 6, 2021 14:28
-
-
Save gunpal5/84beea419558519fe63ac3e5d04a119a to your computer and use it in GitHub Desktop.
Abp.io DatabaseLocalizationResourceContributor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AppLocalizationExtensions | |
{ | |
public static LocalizationResource AddDatabase([NotNull] this LocalizationResource localizationResource,string resourceName = null) | |
{ | |
Check.NotNull(localizationResource, nameof(localizationResource)); | |
localizationResource.Contributors.Add(new DatabaseLocalizationResourceContributor(resourceName)); | |
return localizationResource; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppLocalizationRepository:MongoDbRepository<XXXMongoDbContext, LocalizedStringDbItem, Guid>,IAppLocalizationRepository | |
{ | |
public AppLocalizationRepository(IMongoDbContextProvider<XXXMongoDbContext> dbContextProvider) : base(dbContextProvider) | |
{ | |
} | |
public LocalizedString GetLocalizedText(string cultureName, string name, string resource = null) | |
{ | |
var text = this.FirstOrDefault(s => s.Resource == resource && s.Key == name); | |
if(text == null) | |
{ | |
return null; | |
} | |
return text.GetLocalizedString(cultureName); | |
} | |
public List<LocalizedStringDbItem> GetList(string resource = null) | |
{ | |
return AsyncHelper.RunSync(() => this.GetListAsync(s=>s.Resource == resource)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatabaseLocalizationResourceContributor : ILocalizationResourceContributor | |
{ | |
private IAppLocalizationRepository _localizationRepository; | |
private readonly string _resourceName = null; | |
public DatabaseLocalizationResourceContributor(string resourceName = null) | |
{ | |
_resourceName = resourceName; | |
} | |
public void Initialize(LocalizationResourceInitializationContext context) | |
{ | |
_localizationRepository = context.ServiceProvider.GetRequiredService<IAppLocalizationRepository>(); | |
} | |
public LocalizedString GetOrNull(string cultureName, string name) | |
{ | |
return _localizationRepository.GetLocalizedText(cultureName, name, _resourceName); | |
} | |
public void Fill(string cultureName, Dictionary<string, LocalizedString> dictionary) | |
{ | |
var all = _localizationRepository.GetList(_resourceName); | |
foreach (var item in all) | |
{ | |
var value = item.GetLocalizedString(cultureName); | |
if (!dictionary.ContainsKey(item.Key)) | |
{ | |
dictionary.Add(item.Key, value); | |
} | |
else | |
{ | |
dictionary[item.Key] = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IAppLocalizationRepository:IRepository<LocalizedStringDbItem,Guid> | |
{ | |
LocalizedString GetLocalizedText(string resource, string cultureName, string name); | |
List<LocalizedStringDbItem> GetList(string resource); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocalizedStringDbItem:FullAuditedAggregateRoot<Guid> | |
{ | |
public LocalizedStringDbItem() | |
{ | |
} | |
public LocalizedStringDbItem(string key, string defaultValue) | |
{ | |
this.Key = key; | |
this.DefaultValue = defaultValue; | |
} | |
public string Key { get; set; } | |
public string DefaultValue { get; set; } | |
public Dictionary<string, LocalizedString> LocalizedTexts { get; set; } = | |
new Dictionary<string, LocalizedString>(); | |
public string Resource { get; set; } | |
public LocalizedString GetLocalizedString(string cultureName) | |
{ | |
var value = LocalizedTexts.GetOrDefault(cultureName); | |
return value == null ? new LocalizedString(Key, DefaultValue) : new LocalizedString(Key, value.Value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
...... | |
Configure<AbpLocalizationOptions>(options => | |
{ | |
options.Resources | |
.Add<CustomLocalizationResource>("en") | |
.AddBaseTypes(typeof(AbpValidationResource)) | |
.AddDatabase("Custom"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment