Hi
I've applied this topic for multilingual in mvc core 2.2 web app.:
The example above is applied for none mvc core web app and I applied it for mvc one.
firstly, I Installed the required packages:
<PackageReference Include="LazZiya.ExpressLocalization" Version="4.0.0" /><PackageReference Include="LazZiya.TagHelpers" Version="4.0.1" />
Then I created new folder 'LocalizationResources' with two new classes in it:
public class ExpressLocalizationResource { }
public class ViewLocalizationResource { }
Then created a source file ViewLocalizationResource.ar.resx inside ViewLocalizationResource class with some strings:
Then configured Startup.cs:
public void ConfigureServices(IServiceCollection services) { //Some code var cultures = new[] { new CultureInfo("tr"), new CultureInfo("ar"), new CultureInfo("hi"), new CultureInfo("en"), }; services.AddMvc() .AddExpressLocalization<ExpressLocalizationResource, ViewLocalizationResource>( ops => { ops.ResourcesPath = "LocalizationResources"; ops.RequestLocalizationOptions = o => { o.SupportedCultures = cultures; o.SupportedUICultures = cultures; o.DefaultRequestCulture = new RequestCulture("en"); }; }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //Some code app.UseRequestLocalization(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });
Then _ViewImports.cs:
@using MultiLang @using MultiLang.Models @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @using System.Globalization @addTagHelper *, LazZiya.TagHelpers @addTagHelper *, LazZiya.ExpressLocalization
Then added the method to HomeController:
public IActionResult SetCultureCookie(string cltr, string returnUrl) { Response.Cookies.Append( CookieRequestCultureProvider.DefaultCookieName, CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(cltr)), new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) } ); return LocalRedirect(returnUrl); }
Then added the rtl.css and ltr.css to css folder then updated shared _Layout:
@using System.Globalization @{ var culture = CultureInfo.CurrentCulture.Name; var dirCss = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? "rtl" : "ltr"; } @*Some Code*@ <link rel="stylesheet" href="~/css/site.css" /><link rel="stylesheet" href="@($"/css/{dirCss}.css")" /> @*Some Code*@ <partial name="_LoginPartial" /><language-nav cookie-handler-url="@Url.Action("SetCultureCookie", "Home", new { area="", cltr="{0}", returnUrl="{1}" })"></language-nav>
Then localized Home/Index view:
<div class="text-center"><h1 class="display-4" localize-content>Welcome</h1><p localize-content>Learn about</p></div>
for now every thing is working fine but when I firstly run the app. it is shown sometimes in Arabic and sometimes in English with successfully localized content according to the language.
Then if I navigate to any language of the four langauges, the page is shown blank with the two letters at the last of the url for example '/ar'.
Why? and How to solve please?