Feature: Implement translation service #1

Merged
Egamorf merged 2 commits from feature/implement-translation-service into main 2025-02-17 22:12:56 +01:00
7 changed files with 60 additions and 36 deletions
Showing only changes of commit 68c56ca05f - Show all commits

View File

@@ -4,11 +4,17 @@
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>5637e3c4-2341-4bdb-85ec-c75faeee9847</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\GameIdeas.Resources\GameIdeas.Resources.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,4 +1,5 @@
@inherits LayoutComponentBase
<div class="page">
<div class="sidebar">
<NavMenu />

View File

@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;
using GameIdeas.Resources;
namespace GameIdeas.BlazorApp.Layout;
public partial class MainLayout(
IHttpClientFactory HttpClientFactory,
TranslationService TranslationService,
Translations Translations) : LayoutComponentBase
{
protected override async Task OnInitializedAsync()
{
var client = HttpClientFactory.CreateClient("GameIdeas.WebAPI");
var response = await client.GetAsync("api/Translations");
var dictionary = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
if (dictionary != null)
{
TranslationService.Initialize(dictionary);
ResourcesKey.Initialize(Translations);
}
}
}

View File

@@ -1,4 +1,5 @@
using GameIdeas.WebApp;
using GameIdeas.BlazorApp;
using GameIdeas.Resources;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
@@ -6,6 +7,20 @@ var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
UriBuilder uriBuilder = new(builder.HostEnvironment.BaseAddress)
{
Port = 8000
};
builder.Services.AddHttpClient(
"GameIdeas.WebAPI",
client =>
{
client.BaseAddress = uriBuilder.Uri;
client.Timeout = TimeSpan.FromMinutes(3);
});
builder.Services.AddSingleton<TranslationService>();
builder.Services.AddSingleton<Translations>();
await builder.Build().RunAsync();

View File

@@ -6,5 +6,5 @@
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using GameIdeas.WebApp
@using GameIdeas.WebApp.Layout
@using GameIdeas.BlazorApp
@using GameIdeas.BlazorApp.Layout

View File

@@ -1,27 +0,0 @@
[
{
"date": "2022-01-06",
"temperatureC": 1,
"summary": "Freezing"
},
{
"date": "2022-01-07",
"temperatureC": 14,
"summary": "Bracing"
},
{
"date": "2022-01-08",
"temperatureC": -13,
"summary": "Freezing"
},
{
"date": "2022-01-09",
"temperatureC": -16,
"summary": "Balmy"
},
{
"date": "2022-01-10",
"temperatureC": -2,
"summary": "Chilly"
}
]

View File

@@ -9,6 +9,11 @@ builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddCors(option => option.AddDefaultPolicy(policy =>
policy.WithOrigins("http://localhost:5172")
.AllowAnyHeader()
.WithMethods("GET", "POST")));
builder.Services.AddSingleton<TranslationService>();
builder.Services.AddSingleton<Translations>();
@@ -32,9 +37,9 @@ foreach (var file in translationFiles)
}
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
var resourcesKey = app.Services.GetRequiredService<Translations>();
ResourcesKey.Initialize(resourcesKey);
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());
app.UseCors();
app.UseAuthorization();
app.MapControllers();