Skip to content

Commit 8f2c9aa

Browse files
author
blazej.kuhajda
committed
localization support
added top row with combobox for localization
1 parent 21a703f commit 8f2c9aa

7 files changed

Lines changed: 107 additions & 27 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Localization;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Globalization;
4+
using AXSharp.Connector;
5+
6+
namespace ix_integration_blazor;
7+
8+
[Route("/[controller]")]
9+
[ApiController]
10+
public class CultureController : ControllerBase
11+
{
12+
public async Task<ActionResult> ChangeCulture([FromQuery] string culture)
13+
{
14+
if (culture != null)
15+
{
16+
HttpContext.Response.Cookies.Append(
17+
CookieRequestCultureProvider.DefaultCookieName,
18+
CookieRequestCultureProvider.MakeCookieValue(
19+
new RequestCulture(culture, culture)));
20+
21+
CultureExtensions.Culture = new CultureInfo(culture);
22+
Connector.SetCulture(CultureExtensions.Culture);
23+
24+
}
25+
26+
return Redirect("/");
27+
}
28+
}
29+
30+
public static class CultureExtensions
31+
{
32+
public static CultureInfo Culture { get; set; } = CultureInfo.CurrentCulture ?? CultureInfo.DefaultThreadCurrentCulture ?? new CultureInfo("en-US");
33+
}

src/sanbox/integration/ix-integration-blazor/Pages/Localizations.razor

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,3 @@
44

55
<RenderableContentControl Presentation="Display"
66
Context="@Entry.Plc.test_example.primitives_stack" />
7-
8-
<button @onclick="SetSk"></button>
9-
10-
@code {
11-
12-
protected void SetSk()
13-
{
14-
Program.SetCulture("sk-SK");
15-
}
16-
}

src/sanbox/integration/ix-integration-blazor/Program.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Program
2121
private static WebApplication App { get; set; }
2222

2323
public static void Main(string[] args)
24-
{
24+
{
2525
//ix_integration_plc.PlcTranslator.Instance.SetLocalizationResource(typeof(ix_integration_plc.ResourcesOverrride.OverridePlcStringResources));
2626

2727
var builder = WebApplication.CreateBuilder(args);
@@ -33,43 +33,38 @@ public static void Main(string[] args)
3333
builder.Services.AddIxBlazorServices();
3434
builder.Services.AddLocalization();
3535

36-
3736
var app = builder.Build();
3837

3938
App = app;
4039

4140
Entry.Plc.Connector.BuildAndStart().ReadWriteCycleDelay = 10;
4241
Entry.Plc.Connector.BuildAndStart().SubscriptionMode = ReadSubscriptionMode.Polling;
4342
Entry.Plc.Connector.ExceptionBehaviour = CommExceptionBehaviour.Ignore;
43+
4444
//Entry.Plc.Connector.Translator.SetLocalizationResource(Entry.Plc.GetType(), "Properties.PlcStringResources");
45-
45+
4646
// Configure the HTTP request pipeline.
4747
if (!app.Environment.IsDevelopment())
4848
{
4949
app.UseExceptionHandler("/Error");
5050
}
5151

52+
var supportedCultures = new[] { "en-US", "sk-SK", "es-ES" };
53+
var localizationOptions = new RequestLocalizationOptions()
54+
.AddSupportedCultures(supportedCultures)
55+
.AddSupportedUICultures(supportedCultures);
5256

53-
54-
//app.UseRequestLocalization(new RequestLocalizationOptions()
55-
// .AddSupportedCultures(new[] { "en-US", "sk-SK" })
56-
// .AddSupportedUICultures(new[] { "en-US", "sk-SK" }));
57-
58-
App.UseRequestLocalization("sk-SK");
57+
app.UseRequestLocalization(localizationOptions);
5958

6059
app.UseStaticFiles();
6160

6261
app.UseRouting();
6362

63+
app.MapControllers();
6464
app.MapBlazorHub();
6565
app.MapFallbackToPage("/_Host");
6666

6767
app.Run();
6868
}
69-
70-
public static void SetCulture(string culture)
71-
{
72-
App.UseRequestLocalization(culture);
73-
}
7469
}
7570
}

src/sanbox/integration/ix-integration-blazor/Shared/MainLayout.razor

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
</div>
99

1010
<main>
11-
<div class="top-row px-4">
12-
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
13-
</div>
11+
<TopRow></TopRow>
1412

1513
<article class="content px-4">
1614
@Body
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@using System.Globalization
2+
@inject NavigationManager NavigationManager
3+
4+
<div class="top-row px-2">
5+
6+
<div class="ms-auto">
7+
<select class="form-control" @bind="Culture" @bind:event="oninput">
8+
@foreach (var culture in supportedCultures)
9+
{
10+
<option data value="@culture">@culture.NativeName</option>
11+
}
12+
</select>
13+
</div>
14+
15+
16+
</div>
17+
18+
@code {
19+
private CultureInfo[] supportedCultures = new[]
20+
{
21+
new CultureInfo("en-US"),
22+
new CultureInfo("sk-SK"),
23+
new CultureInfo("es-ES")
24+
};
25+
26+
private CultureInfo Culture
27+
{
28+
get => CultureInfo.CurrentCulture;
29+
set
30+
{
31+
// Prevent unnecessary navigation
32+
if (!Equals(CultureInfo.CurrentCulture, value) && !Equals(CultureInfo.CurrentUICulture, value))
33+
{
34+
var cultureEscaped = Uri.EscapeDataString(value.Name);
35+
NavigationManager.NavigateTo($"/culture?culture={cultureEscaped}", true);
36+
}
37+
}
38+
}
39+
40+
protected override void OnInitialized()
41+
{
42+
Culture = CultureInfo.CurrentCulture;
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.top-row {
2+
background-color: #f7f7f7;
3+
border-bottom: 1px solid #d6d5d5;
4+
height: 3.5rem;
5+
display: flex;
6+
align-items: center;
7+
}
8+
9+
@media (max-width: 769px) {
10+
.IAmHereIndicator {
11+
display: none;
12+
}
13+
}

src/sanbox/integration/ix-integration-blazor/_Imports.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@
1111
@using AXSharp.Presentation.Blazor.Controls.RenderableContent
1212
@using AXSharp.Connector
1313
@using ix_integration_plc
14+
15+
@using Microsoft.Extensions.Localization
16+
@using System.Globalization
17+
18+
@using ix_integration_plc.Resources
19+
@inject IStringLocalizer<PlcStringResources> PlcLocalizer
20+

0 commit comments

Comments
 (0)