Created
May 26, 2021 09:49
-
-
Save MoazAlkharfan/86ee6563c80727d215e9e64827add688 to your computer and use it in GitHub Desktop.
Net core Portable object localization reload when translation file changes.
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
using System; | |
using System.IO; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Caching.Memory; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.FileProviders; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Localization; | |
using Microsoft.Extensions.Options; | |
using Microsoft.Extensions.Primitives; | |
public class PoFileRefresherService : BackgroundService | |
{ | |
private const string cacheKeyPrefix = "CultureDictionary-"; | |
private const string poFileWileCard = "*.po"; | |
private readonly IServiceProvider provider; | |
private readonly IWebHostEnvironment environment; | |
private readonly LocalizationOptions options; | |
private IChangeToken changeToken; | |
private readonly object state = new object(); | |
private readonly string poFilesPath; | |
public PoFileRefresherService(IOptions<LocalizationOptions> options, IServiceProvider provider, IWebHostEnvironment environment) | |
{ | |
this.provider = provider; | |
this.environment = environment; | |
this.options = options.Value; | |
this.poFilesPath = Path.Combine(this.options.ResourcesPath, poFileWileCard); | |
} | |
private static string GetCacheKey(string culture) => cacheKeyPrefix + culture; | |
private IDirectoryContents DirectoryContent => environment.ContentRootFileProvider.GetDirectoryContents(this.options.ResourcesPath); | |
private void SetChangeToken() | |
{ | |
changeToken = environment.ContentRootFileProvider.Watch(poFilesPath); | |
changeToken.RegisterChangeCallback(ChangeCallBack, state); | |
} | |
private void ChangeCallBack(object _) | |
{ | |
// Stop change call back being called multiple times. | |
Thread.Sleep(5000); | |
ProcessChanged(); | |
SetChangeToken(); | |
} | |
private void ProcessChanged() | |
{ | |
using IServiceScope scope = provider.CreateScope(); | |
IMemoryCache cache = scope.ServiceProvider.GetRequiredService<IMemoryCache>(); | |
foreach (IFileInfo fileInfo in DirectoryContent) | |
{ | |
string? name = Path.GetFileNameWithoutExtension(fileInfo.Name); | |
cache.Remove(GetCacheKey(name)); | |
} | |
} | |
protected override Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
SetChangeToken(); | |
return Task.CompletedTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment