I have been trying to follow these two tutorials to add Localization to my .Net Core Razor web app.
http://ziyad.info/en/articles/36-Develop_Multi_Cultural_Web_Application_Using_ExpressLocalization
I have tried creating projects from scratch. I have tried adding to my existing projects. I have tried using the LocalizeTagHelper and SharedCultureLocalizer options without success.
I just cant get any text such as 'Home' or 'myApp' below to change.
When I select a language in my dropdown, the language is specified in the URL (See below), but my text just wont change.
Dropdown component & Home text x 2:
Url:
Index.cshtml
@page
@model IndexModel
@using LazZiya.ExpressLocalization
@inject ISharedCultureLocalizer _loc
@{
ViewData["Title"] = @_loc["myApp"];
}
<body>
<h1 class="display-4" localize-content>Home</h1>
<header>
<div class="bg-img">
<div class="container-title">
<div class="block-title block-title1">
<language-nav cookie-handler-url="@Url.Page("/Index", "SetCultureCookie", new { area="", cltr="{0}", returnUrl="{1}" })"></language-nav>
<br>
</div>
<div class="block-title block-title2 d-none d-md-block d-lg-block d-xl-block"><img src="/image/title_image.png" class="img-fluid"></div>
</div>
</div>
</header>
<main>
<div class="row_primary">
</div>
</main>
</body>
Index.cshtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace myApp.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
public IActionResult OnGetSetCultureCookie(string cltr, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(cltr)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
}
}
Startup.cs
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using LazZiya.ExpressLocalization;
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using myApp.wwwroot.LocalizationResources;
namespace myApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
var cultures = new[]
{
new CultureInfo("de"),
new CultureInfo("fr"),
new CultureInfo("en"),
};
services.AddRazorPages().AddExpressLocalization<ExpressLocalizationResource, ViewLocalizationResource >( ops =>
{
ops.ResourcesPath = "LocalizationResources";
ops.RequestLocalizationOptions = o =>
{
o.SupportedCultures = cultures;
o.SupportedUICultures = cultures;
o.DefaultRequestCulture = new RequestCulture("en");
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseRequestLocalization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
wwwroot
resx example
resx properties
My resx file had its Build Action property as 'Content' instead of 'Embedded Resource'. And my LocSource.cs had its Build Action property as 'Embedded Resource' instead of 'C# compiler'.
Really looking forward to using this package now, looks like it will make life much easier.