Salve a tutti,
da 2gg sbatto la testa su questa cosa senza venirne a capo.
Con .NET Core 3.1 su un progetto pulito funziona perfettamente, all'interno di un progetto gli stessi file risorse e le stesse classi non restituiscono il valore della risorsa (nessun errore di runtime o compilazione). Ho provato anche a togliere tutte le referenze diverse dal progetto pulito ma niente..
Grazie a chi saprà darmi qualche consiglio!
public void ConfigureServices(IServiceCollection services)
{
// Dependency Injection per leggere Localizzazione File Resources delle lingue
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("de"),
new CultureInfo("fr"),
new CultureInfo("es"),
new CultureInfo("ru"),
new CultureInfo("ja"),
new CultureInfo("ar"),
new CultureInfo("zh"),
new CultureInfo("en-GB")
};
options.DefaultRequestCulture = new RequestCulture("en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddSingleton<CommonLocalizationService>();
services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
return factory.Create(nameof(CommonResources), assemblyName.Name);
};
});
services.AddControllersWithViews();
// Add MVC - Imposta automaticamente un Token Antiforgery CSRF su tutte le Form del progetto
// If you need to ignore Anti forgery validation you can add [IgnoreAntiforgeryToken] attribute to the action.
services.AddMvc(options => {
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
});
//services.AddDistributedMemoryCache(); // Abilita Cache Distribuita (Sessioni) su altri Server e non sullo stesso
// Aggiungi Supporto Sessioni
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(20);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
// File di Configurazione JSON
//services.AddOptions(); // Add functionality to inject IOptions<T>
// File di Configurazione JSON - Add our Config object so it can be injected
services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
services.Configure<Smtp>(Configuration.GetSection("Smtp"));
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
// Dependency Injection per parametri ConfigurazioneECommerce
services.AddSingleton<IMyConfigurationECommerce, MyConfigurationECommerce>();
}
// 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("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSession(); // Aggiungi Supporto Sessioni
// Dependency Injection Option per leggere Localizzazione File Resources delle lingue
var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(localizationOptions);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
public class CommonLocalizationService
{
private readonly IStringLocalizer localizer;
public CommonLocalizationService(IStringLocalizerFactory factory)
{
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
localizer = factory.Create(nameof(CommonResources), assemblyName.Name);
}
public string Get(string key)
{
return localizer[key];
}
}
@inject CommonLocalizationService localizer
@model WebApplication6.Models.ContactModel
<h1>@localizer.Get("Contact")</h1>
Nella Root del Progetto ho:
/Resources/CommonResources.cs
/Resources/CommonResources.de.resx
/Resources/CommonResources.en.resx
Modificato da diego78 il 25 aprile 2020 15:58 -