Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Localization using vs 2017

$
0
0

Greetings,

I'm trying to add localization on .Net Core 1.1 using Visual Studio 2017.

Every time I try to localize from an injected HtmlLocalizer in my view, it returns the global resource value (SharedResources.resx).

I tried with the IViewLocalizer with resx files with the same name as the view without success.

I created a folder Resources, added SharedResources.cs, SharedResources.en.resx, SharedResources.fr.resx and SharedResources.resx in it.

Here is my code :

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options => options.ResourcesPath = "Resources");
            // Add framework services.
            services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            var supportedCultures = new []
            {
                new CultureInfo("fr"),
                new CultureInfo("en"),
                new CultureInfo("en-US")
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("fr"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }


View (About.cshtml)

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@inject IHtmlLocalizer<SharedResources> SharedHtmlLocalizer

@{
    ViewData["Title"] = "About";
}<h2>@ViewData["Title"].</h2><h3>@ViewData["Message"]</h3><p>@SharedHtmlLocalizer["myRes"]</p><p>Use this area to provide additional information.</p>

@await Html.PartialAsync("_SelectLanguagePartial")

SharedResources.cs

namespace TestResx
{
    public class SharedResources
    {
    }
}



Resources folder :

-Resources/
-- SharedResources.cs
-- SharedResources.en.resx
-- SharedResources.fr.resx
-- SharedResources.resx

Is there something I'm doing wrong ? I can't see what's the problem here..

Thanks a lot


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>