1

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.

Thomas Schneiter
  • 1,103
  • 2
  • 19
  • 35
  • What is the value of `lang` when you encounter the error? Are you sure you are using an existing culture code? – Flater May 24 '17 at 11:13
  • I just realized that when using `_localizer.WithCulture(new CultureInfo("de")).GetAllStrings()` the correct .resx is returned without an exception. But how can i get the default .resx with no culture in the name, in the case that the provided culture doesn't exist? – Thomas Schneiter May 24 '17 at 11:51
  • 1
    There is already an answer to that question provided [in this older SO question](https://stackoverflow.com/questions/13723790/how-to-check-exists-culture-in-net). You simply need to provide a default culture and use that if the current one is not valid. I'm voting to close your question as a duplicatie of this. – Flater May 24 '17 at 11:53
  • I'll accept your answer. I excepted that the default .resx would be selected if no culture or a wrong culture is provided but this does not seem to be the case. – Thomas Schneiter May 24 '17 at 12:28
  • I can see why you'd expect that. I like providing fallback values in most of my applications too. However, it seems like good practice in general that if you ask for a **specific** item, you don't want to get something else (other than `null`). At least, this is the most intuitive approach that avoids unexpected magic. – Flater May 24 '17 at 13:04
  • This question should be reopened as it is not answered at all by the one it is supposedly duplicating. – David Pine Apr 23 '19 at 17:37

0 Answers0