zoukankan      html  css  js  c++  java
  • 读取资源文件里的值---来源practical-aspnetcore项目

        MvcLocalization.HomeController.fr.resx

     资源文件内容

        public class Startup
        {
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });//1、配置读取本地文件中间件
                services.AddControllersWithViews();
            }
    
            public void Configure(IApplicationBuilder app)
            {
                app.UseRouting();
                app.UseStaticFiles();
    
                var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("fr-FR")
                };
    
                var options = new RequestLocalizationOptions  //2、配置本地资源option
                {
                    DefaultRequestCulture = new RequestCulture("fr-FR"),
                    SupportedCultures = supportedCultures,
                    SupportedUICultures = supportedCultures
                };
    
                app.UseRequestLocalization(options); //3、注册请求本地资源文件中间件
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapDefaultControllerRoute();
                });
            }
        }
    
        public class HomeController : Controller
        {
            readonly IStringLocalizer<HomeController> _local;
    
            public HomeController(IStringLocalizer<HomeController> local)
            {
                _local = local;
            }
            public ActionResult Index()
            {
                var culture = this.HttpContext.Features.Get<IRequestCultureFeature>();
    
                return new ContentResult
                {
                    Content = $@"<html><body>
                    <h1>MVC Localization Resource File Naming - Dot Naming Convention</h1>
                    <p>
                    <i><b>Resources are named for the full type name of their class minus the assembly name</b>. For example, a French resource in a project whose main assembly is LocalizationWebsite.Web.dll for the class LocalizationWebsite.Web.Startup would be named Startup.fr.resx. A resource for the class LocalizationWebsite.Web.Controllers.HomeController would be named Controllers.HomeController.fr.resx. If your targeted class's namespace isn't the same as the assembly name you will need the full type name. For example, in the sample project a resource for the type ExtraNamespace.Tools would be named ExtraNamespace.Tools.fr.resx.
                    </i>
                    <a href=""https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1"">Doc</a>
                    </p>
                    <p>
                        In this sample, the assembly name is ""mvc-localization"" however the namespace used is ""MvcLocalization"". Since the namespace differs from the Assembly Name, we use full type path.
                    </p>
                    <b>Culture requested</b> {culture.RequestCulture.Culture} <br/>
                    <b>UI Culture requested</b> {culture.RequestCulture.UICulture} <br/>
                    Text: {_local["Hello"]}<br/>
                    Text: {_local["Goodbye"]}</body></html>",
                    ContentType = "text/html"
                };
            }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                        webBuilder.UseStartup<Startup>()
                    );
        }

     上面采用的控制器依赖注入,还可以采用Razor页面依赖注入

    Razor组件——依赖注入

    https://blog.csdn.net/flyingdream123/article/details/98882746

  • 相关阅读:
    Vue项目入门实例
    批量生成删除表数据的SQL语句
    收集的一个可多选日期的日期插件,带日历、农历
    .net core EF,多个dbcontext时,迁移数据方法
    【NET】雪花算法
    URL地址中使用中文作为的参数【转】
    C# 继承 base
    SQL Like
    SecureCRT 8.5 配置自动记录日志
    CentOS 7下Samba服务器的安装与配置
  • 原文地址:https://www.cnblogs.com/CelonY/p/13718873.html
Copyright © 2011-2022 走看看