/
Startup.cs
129 lines (110 loc) · 4.62 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.IO;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Localization;
using System.Globalization;
using Microsoft.Extensions.Options;
using Localization.SqlLocalizer.DbStringLocalizer;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Swagger;
using System.Reflection;
using Microsoft.Extensions.Hosting;
namespace AspNetCoreLocalization
{
public class Startup
{
private bool _createNewRecordWhenLocalisedStringDoesNotExist = false;
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
if (env.IsDevelopment())
{
_createNewRecordWhenLocalisedStringDoesNotExist = true;
}
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// services.AddLocalization(options => options.ResourcesPath = "Resources");
var sqlConnectionString = Configuration["DbStringLocalizer:ConnectionString"];
services.AddDbContext<LocalizationModelContext>(options =>
options.UseSqlite(
sqlConnectionString,
b => b.MigrationsAssembly("AspNetCoreLocalization")
),
ServiceLifetime.Singleton,
ServiceLifetime.Singleton
);
var useTypeFullNames = false;
var useOnlyPropertyNames = false;
var returnOnlyKeyIfNotFound = false;
// Requires that LocalizationModelContext is defined
// _createNewRecordWhenLocalisedStringDoesNotExist read from the dev env.
services.AddSqlLocalization(options => options.UseSettings(
useTypeFullNames,
useOnlyPropertyNames,
returnOnlyKeyIfNotFound,
_createNewRecordWhenLocalisedStringDoesNotExist));
// services.AddSqlLocalization(options => options.ReturnOnlyKeyIfNotFound = true);
// services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddScoped<LanguageActionFilter>();
services.Configure<RequestLocalizationOptions>(
options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("de-CH"),
new CultureInfo("fr-CH"),
new CultureInfo("it-CH")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddControllersWithViews()
.AddNewtonsoftJson()
.AddViewLocalization()
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
return factory.Create("SharedResource", assemblyName.Name);
};
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "v1",
Title = "API",
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
});
}
}
}