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"]
     
    测试运行
    搞点。
  • 相关阅读:
    第一章:进销存系统基本功能
    SpringBoot 整合 Docker
    Java的脚本机制、编译器API
    Java 定时任务
    监听文件修改的四种方法
    SpringBoot Actuator — 埋点和监控
    Kafka消息队列
    OpenSSL配置HTTPS
    Java 国际化
    备忘录模式
  • 原文地址:https://www.cnblogs.com/cgzwwy/p/6893674.html
Copyright © 2011-2022 走看看