zoukankan      html  css  js  c++  java
  • 用app.net Core搞掂多国语言网站

    Asp.net Core 中文文档很少,你可以看英文的,不过英文的也是说的有点乱。这篇文章是干货。
    1. 配置好你的WebApplication,使他可以支持国际化语言,修改文档Startup.cs
            publicvoid ConfigureServices(IServiceCollection services)
            {
                services.AddLocalization(options => options.ResourcesPath = "Resources");
                services.AddMvc()
                  .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                  .AddDataAnnotationsLocalization();
            }
    2.修改好你的配置
     
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {...
    这里面写你的网站需要支持的语言。
                var supportedCultures = new[]
                {
                    new CultureInfo("en-US"),
                    new CultureInfo("zh-CN")
                };
     
    这里是写你的默认语言的
                app.UseRequestLocalization(new RequestLocalizationOptions
                {
                    DefaultRequestCulture = new RequestCulture("en-US"),
                    // Formatting numbers, dates, etc.
                    SupportedCultures = supportedCultures,
                    // UI strings that we have localized.
                    SupportedUICultures = supportedCultures
                });
     
    }
     
    3.给你的视图增加资源文档
    a.增加Resources目录
    b.按你的视图路径,给出资源文档的结构 例如你的视图ViewsHomeIndex.cshtml 你的资源ResourcesViewsHomeIndex.zh-CN.resx  或 ResourcesViewsHomeIndex.en-US.resx
    c.在资源文件中添加Key 和Value  
    d.视图顶部
    @using Microsoft.AspNetCore.Mvc.Localization
    @inject IViewLocalizer Localizer
     
    需要显示的字符串中
    Learn More   修改 @Localizer["Learn More"]
     
    测试运行
    搞点。
  • 相关阅读:
    Android_SQLite标准版
    AutoMapper
    ext.net实例
    Winform Ribbon开发
    数据仓库建设之《元数据管理》
    数据仓库建设之维度建模
    数据仓库建设之总方案
    一点感想
    详细探秘Linux 和 Window 双系统访问Windows 磁盘需要输入密码问题解决过程分析
    如何把数据放到C#的心里之 DB2实例
  • 原文地址:https://www.cnblogs.com/cgzwwy/p/6893674.html
Copyright © 2011-2022 走看看