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

  • 相关阅读:
    用电脑给手机安装App
    切换皮肤的实现
    瀑布流的简单实现
    HTML5的实用
    HTML5的特性,发展,及使用
    录音的使用步骤
    支付宝集成步骤
    美团(iPad)顶部界面的简单实现, 及开发时常见bug
    真机调试/打包测试/程序发布/内购的具体操作流程
    IOS 触摸事件的处理
  • 原文地址:https://www.cnblogs.com/CelonY/p/13718873.html
Copyright © 2011-2022 走看看