User-854763662 posted
Hi TechAspirant ,
Here is a working demo :
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Resource files path and SharedResource.cs which is created in the root of the project

Controller
[Produces("application/json")]
[Route("api/[controller]")]
public class TranslationsController : Controller
{
private readonly IStringLocalizer<SharedResource> _localizer;
public TranslationsController(IStringLocalizer<SharedResource> localizer)
{
_localizer = localizer;
}
[HttpGet]
public ActionResult List()
{
Dictionary<string, string> translations = _localizer.GetAllStrings().ToDictionary(x => x.Name, x => x.Value);
return new JsonResult(translations);
}
}
Here is my
demo link, you could refer to.
Best Regards,
Sherry