In my .net core web application i would like to create an Endpoint that returns all translations in a given .resx file.
I use the StringLocalizer Service to get the Strings from the .resx file. When i try to get a single string with _localizer[HelloString];
the correct translation is returned so the injection/translation works.
When i try to use
_localizer.GetAllStrings();
i always get an exception with the message "No manifests exist for the current culture."
The resource files are named Controllers.TranslationController.resx, Controllers.TranslationController.de.resx,...
public class TranslationsController : Controller
{
private readonly IStringLocalizer<TranslationsController> _localizer;
public TranslationsController(IStringLocalizer<TranslationsController> localizer)
{
_localizer = localizer;
}
[HttpGet]
public String Get(string lang = "")
{
var resourceSet = _localizer.GetAllStrings();
return String.Empty;
}
}
The method is not finished so please ignore the return String.Empty;
I inject and configure the StringLocalizer in the Startup.cs as follows:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
// Add framework services.
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
new CultureInfo("de"),
new CultureInfo("en-GB"),
new CultureInfo("en"),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("de-DE"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
I tried to rename the files or choose a specific culture using _localizer.WithCulture(new CultureInfo(lang)).GetAllStrings()
but nothing seems to work.