-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
68 lines (59 loc) · 2.91 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace AspNetLocalization03
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "resources");
}
public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> stringLocalizer, IStringLocalizerFactory stringLocalizerFactory)
{
app.UseRequestLocalization(BuildLocalizationOptions());
app.Use(async (context, next) =>
{
context.Response.StatusCode = 200;
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.WriteAsync(BuildResponse(stringLocalizer, stringLocalizerFactory));
});
}
private RequestLocalizationOptions BuildLocalizationOptions()
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES"),
new CultureInfo("de-DE"),
new CultureInfo("fr-FR"),
new CultureInfo("ko-KR")
};
return new RequestLocalizationOptions {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
}
private string BuildResponse(IStringLocalizer stringLocalizer, IStringLocalizerFactory stringLocalizerFactory)
{
var currentCultureName = CultureInfo.CurrentCulture.EnglishName;
var currentUICultureName = CultureInfo.CurrentUICulture.EnglishName;
var sharedStringLocalizer = stringLocalizerFactory.Create("Shared", null);
return "<html><body>"
+ $"<h2>{stringLocalizer["Hello"]}!</h2><table border=\"1\" cellpadding=\"5\" style=\"border-collapse:collapse;\">"
+ $"<tr><td>{stringLocalizer["Current Culture"]}</td><td>{currentCultureName}</td></tr>"
+ $"<tr><td>{stringLocalizer["Current UI Culture"]}</td><td>{currentUICultureName}</td></tr>"
+ $"<tr><td>{stringLocalizer["The Current Date"]}</td><td>{DateTime.Now.ToString("D")}</td></tr>"
+ $"<tr><td>{stringLocalizer["A Formatted Number"]}</td><td>{(1234567.89).ToString("n")}</td></tr>"
+ $"<tr><td>{stringLocalizer["A Currency Value"]}</td><td>{(42).ToString("C")}</td></tr></table>"
+ $"<h2>{sharedStringLocalizer["Goodbye"]}</h2>"
+ "</body></html>";
}
}
}