123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- // <author></author>
- // <date></date>
- // <description></description>
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Localization;
- using System.Globalization;
- namespace ERP.Framework.Extensions
- {
- public static class I18NExtension
- {
- public static void UseI18N(this IApplicationBuilder app)
- {
- app.UseRequestLocalization(options =>
- {
- var supporttedCultures = new List<CultureInfo>
- {
- new CultureInfo("en-US"),
- new CultureInfo("zh-CN"),
- };
- options.DefaultRequestCulture = new RequestCulture("en-US");
- options.SupportedCultures = supporttedCultures;
- options.SupportedUICultures = supporttedCultures;
- options.RequestCultureProviders = new List<IRequestCultureProvider>
- {
- new LangCultrueProvider(),
- };
- });
- }
- }
- public class LangCultrueProvider : RequestCultureProvider
- {
- /// <inheritdoc />
- public override Task<ProviderCultureResult?> DetermineProviderCultureResult(HttpContext httpContext)
- {
- if (httpContext == null)
- {
- throw new ArgumentNullException(nameof(httpContext));
- }
- var lang = httpContext.Request.Headers["lang"].ToString();
- if (!lang.IsNullOrEmpty())
- {
- return Task.FromResult<ProviderCultureResult?>(new ProviderCultureResult(lang));
- }
- return NullProviderCultureResult;
- }
- }
- }
|