Add translation service for web api

This commit is contained in:
Maxime Adler
2025-02-17 13:49:01 +01:00
parent 7a87e3457b
commit fc7cce4ce6
13 changed files with 81 additions and 68 deletions

View File

@@ -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();