MVC 6 : how to use RESX files?

前端 未结 2 1838
忘了有多久
忘了有多久 2021-01-01 17:49

I am trying to migrate my existing ASP.NET MVC 5 project to MVC 6 vNext project , while I have been able to get through and resolve most of the issues , I cant seem to find

2条回答
  •  耶瑟儿~
    2021-01-01 18:18

    You can take a look at a full sample on the ASP.NET MVC GitHub project here. At the time of writing this is all very new code and subject to change. You need to add the following into your startup:

    public class Startup
    {
        // Set up application services
        public void ConfigureServices(IServiceCollection services)
        {
            // Add MVC services to the services container
            services.AddMvc();
            services.AddMvcLocalization();
    
            // Adding TestStringLocalizerFactory since ResourceStringLocalizerFactory uses ResourceManager. DNX does
            // not support getting non-enu resources from ResourceManager yet.
            services.AddSingleton();
        }
    
        public void Configure(IApplicationBuilder app)
        {
            app.UseCultureReplacer();
    
            app.UseRequestLocalization();
    
            // Add MVC to the request pipeline
            app.UseMvcWithDefaultRoute();
        }
    }
    

    The IStringLocalizerFactory seems to be used to create instances of IStringLocalizer from resx types. You can then use the IStringLocalizer to get your localized strings. Here is the full interface (LocalizedString is just a name value pair):

    /// 
    /// Represents a service that provides localized strings.
    /// 
    public interface IStringLocalizer
    {
        /// 
        /// Gets the string resource with the given name.
        /// 
        /// The name of the string resource.
        /// The string resource as a .
        LocalizedString this[string name] { get; }
    
        /// 
        /// Gets the string resource with the given name and formatted with the supplied arguments.
        /// 
        /// The name of the string resource.
        /// The values to format the string with.
        /// The formatted string resource as a .
        LocalizedString this[string name, params object[] arguments] { get; }
    
        /// 
        /// Gets all string resources.
        /// 
        /// 
        /// A  indicating whether to include
        /// strings from ancestor cultures.
        /// 
        /// The strings.
        IEnumerable GetAllStrings(bool includeAncestorCultures);
    
        /// 
        /// Creates a new  for a specific .
        /// 
        /// The  to use.
        /// A culture-specific .
        IStringLocalizer WithCulture(CultureInfo culture);
    }
    

    Finally you can inject the IStringLocalizer into your Controller like so (Note that IHtmlLocalizer inherits from IStringLocalizer):

    public class HomeController : Controller
    {
        private readonly IHtmlLocalizer _localizer;
    
        public HomeController(IHtmlLocalizer localizer)
        {
            _localizer = localizer;
        }
    
        public IActionResult Index()
        {
            return View();
        }
    
        public IActionResult Locpage()
        {
            ViewData["Message"] = _localizer["Learn More"];
            return View();
        }
    }
    

提交回复
热议问题