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
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<IStringLocalizerFactory, TestStringLocalizerFactory>();
}
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):
/// <summary>
/// Represents a service that provides localized strings.
/// </summary>
public interface IStringLocalizer
{
/// <summary>
/// Gets the string resource with the given name.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
LocalizedString this[string name] { get; }
/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns>
LocalizedString this[string name, params object[] arguments] { get; }
/// <summary>
/// Gets all string resources.
/// </summary>
/// <param name="includeAncestorCultures">
/// A <see cref="System.Boolean"/> indicating whether to include
/// strings from ancestor cultures.
/// </param>
/// <returns>The strings.</returns>
IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures);
/// <summary>
/// Creates a new <see cref="ResourceManagerStringLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
/// <returns>A culture-specific <see cref="IStringLocalizer"/>.</returns>
IStringLocalizer WithCulture(CultureInfo culture);
}
Finally you can inject the IStringLocalizer
into your Controller like so (Note that IHtmlLocalizer<HomeController>
inherits from IStringLocalizer
):
public class HomeController : Controller
{
private readonly IHtmlLocalizer _localizer;
public HomeController(IHtmlLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
public IActionResult Index()
{
return View();
}
public IActionResult Locpage()
{
ViewData["Message"] = _localizer["Learn More"];
return View();
}
}
Things have been changed in mvc 6.0.0-rc1-final. After going through many other forums, below configuration will work if anyone planning to work with latest changes in localization feature.
In startup.cs configure
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization();
services.AddSingleton<IStringLocalizerFactory, CustomStringLocalizerFactory>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var requestLocalizationOptions = new RequestLocalizationOptions
{
SupportedCultures = new List<CultureInfo>{
new CultureInfo("en-US"),
new CultureInfo("fr-CH")
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("fr-CH")
}
};
app.UseRequestLocalization(requestLocalizationOptions, new RequestCulture(new CultureInfo("en-US")));
}
and you can start using IHtmlLocalizer in controller.
and you can test with querystring http://localhost:5000/Home/Contact?culture=fr-CH or change the culture in chrome by adding preferred language under "Language and Input Setting"