|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using LocalizationWebsite.Models; |
| 5 | +using Microsoft.AspNetCore.Builder; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.AspNetCore.Localization; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Localization; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | + |
| 12 | +namespace LocalizationWebsite |
| 13 | +{ |
| 14 | + public class StartupBuilderAPIs |
| 15 | + { |
| 16 | + public void ConfigureServices(IServiceCollection services) |
| 17 | + { |
| 18 | + services.AddLocalization(options => options.ResourcesPath = "Resources"); |
| 19 | + } |
| 20 | + |
| 21 | + public void Configure( |
| 22 | + IApplicationBuilder app, |
| 23 | + ILoggerFactory loggerFactory, |
| 24 | + IStringLocalizer<Customer> customerStringLocalizer) |
| 25 | + { |
| 26 | + var supportedCultures = new[] { "en-US", "fr-FR" }; |
| 27 | + app.UseRequestLocalization(options => |
| 28 | + options |
| 29 | + .AddSupportedCultures(supportedCultures) |
| 30 | + .AddSupportedUICultures(supportedCultures) |
| 31 | + .SetDefaultCulture("ar-YE") |
| 32 | + ); |
| 33 | + |
| 34 | + app.Run(async (context) => |
| 35 | + { |
| 36 | + var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); |
| 37 | + var requestCulture = requestCultureFeature.RequestCulture; |
| 38 | + await context.Response.WriteAsync(customerStringLocalizer["Hello"]); |
| 39 | + }); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments