Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

RequestCultureProvider sets an unsupported culture when the request is not supported and the default is not defined or not supported. #70

Closed
@Bartmax

Description

@Bartmax

Helping someone with an issue on Jabbr about localization, I found this strange (at least for me) behavior.

RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions()
{
  SupportedCultures = new List<CultureInfo>
  {
    new CultureInfo("nl-NL")
  },
  SupportedUICultures = new List<CultureInfo>
  {
    new CultureInfo("nl-NL")
  }
  //, DefaultRequestCulture = new RequestCulture(new CultureInfo("nl-NL"), new CultureInfo("nl-NL"))
};
app.UseRequestLocalization(localizationOptions);

------------------------
services.AddLocalization();
services.AddMvc().AddViewLocalization();

note the commented line

With this cshtml (in layout or index):

<h1>UICulture: @System.Globalization.CultureInfo.CurrentUICulture.Name</h1> // outputs: en-US 
<h1>Culture: @System.Globalization.CultureInfo.CurrentCulture.Name</h1> // outputs: es-UY

According to the documentation here: https://github.com/aspnet/Localization/blob/dev/src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs#L38-L54

This is not expected behavior. Those cultures are not supposed to be supported by the application.

Uncommenting the commented line (DefaultRequestCulture) I get the expected output:

DefaultRequestCulture = new RequestCulture(new CultureInfo("nl-NL"), new CultureInfo("nl-NL"))
UICulture: nl-NL
Culture: nl-NL

Looking at source code, I found that a 'premature' fallback to default culture.
https://github.com/aspnet/Localization/blob/dev/src/Microsoft.AspNet.Localization/RequestCultureProvider.cs#L42-L50 without checking if the Default is supported.

I think the workflow should be more like (not real code)

var culture = Options.SupportedCultures == null || !Options.SupportedCultures.Any() || Options.SupportedCultures.Contains(result.Culture)
    ? result.Culture
    : Options.SupportedCultures.Contains(Options.DefaultRequestCulture.Culture)
    ? Options.DefaultRequestCulture.Culture
    : Options.SupportedCultures.First();

var uiCulture =  Options.SupportedUICultures == null || !Options.SupportedUICultures.Any() || Options.SupportedUICultures.Contains(result.UICulture)
    ? result.UICulture
    : Options.SupportedUICultures.Contains(Options.DefaultRequestCulture.UICulture)
    ? Options.DefaultRequestCulture.UICulture
    : Options.SupportedUICultures.First();

 result = new RequestCulture(culture, uiCulture);

I'm assuming Empty List = null here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions