locked
Routing to a selected language returns a blank page! RRS feed

  • Question

  • User991566988 posted

    Hi

    I've applied this topic for multilingual in mvc core 2.2 web app.:

    https://medium.com/swlh/step-by-step-tutorial-to-build-multi-cultural-asp-net-core-web-app-3fac9a960c43

    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?

    Tuesday, June 9, 2020 10:17 AM

Answers

  • User711641945 posted

    Hi musbah7@hotmail.com,

    musbah7@hotmail.com

    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'.

    I could get the same url like yours,but I do not get the blank page.I get the 404 error page.

    Mvc is different from Razor Pages,you need to change like below:

    public IActionResult SetCultureCookie(string cltr, string returnUrl)
    {
        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(cltr)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        );
        var query = returnUrl.Split("/");
        return LocalRedirect("/?culture="+query[1]);
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2020 8:57 AM

All replies

  • User711641945 posted

    Hi musbah7@hotmail.com,

    musbah7@hotmail.com

    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'.

    I could get the same url like yours,but I do not get the blank page.I get the 404 error page.

    Mvc is different from Razor Pages,you need to change like below:

    public IActionResult SetCultureCookie(string cltr, string returnUrl)
    {
        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(cltr)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        );
        var query = returnUrl.Split("/");
        return LocalRedirect("/?culture="+query[1]);
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2020 8:57 AM
  • User991566988 posted

    Thanks

    Wednesday, June 10, 2020 10:36 AM