Add translation service for web api
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
using GameIdeas.Resources;
|
||||
using System.Resources;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
@@ -6,6 +9,9 @@ builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.AddSingleton<TranslationService>();
|
||||
builder.Services.AddSingleton<Translations>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -14,6 +20,21 @@ if (app.Environment.IsDevelopment())
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files");
|
||||
var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
|
||||
var dictionary = new Dictionary<string, string>();
|
||||
foreach (var file in translationFiles)
|
||||
{
|
||||
var name = file.Split('.');
|
||||
var culture = name[^2];
|
||||
var content = await File.ReadAllTextAsync(file);
|
||||
dictionary.Add(culture, content);
|
||||
}
|
||||
|
||||
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
|
||||
var resourcesKey = app.Services.GetRequiredService<Translations>();
|
||||
ResourcesKey.Initialize(resourcesKey);
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
Reference in New Issue
Block a user