Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to the default culture resource #83

Open
sivashkevych opened this issue Mar 18, 2024 · 15 comments
Open

Fallback to the default culture resource #83

sivashkevych opened this issue Mar 18, 2024 · 15 comments

Comments

@sivashkevych
Copy link

The default implementation of IStringLocalizer returns a string from the default culture resource if the localizer can't find the localization in the current resource.

However, the JsonStringLocalizer returns the localization string key value by default.

I've seen the PR Culture fallback to the rooted resource.
Are you going to merge it?

@hishamco
Copy link
Owner

hishamco commented Mar 18, 2024

Do you mean this PR #41?

@hishamco
Copy link
Owner

Seems the PR is too old, I can confirm that culture fallback is working

[Theory]
[InlineData("fr-FR", "Hello", "Bonjour")]
[InlineData("fr-FR", "Hello, {0}", "Bonjour, {0}")]
[InlineData("fr-FR", "Yes", "Oui")]
[InlineData("fr-FR", "No", "No")]
[InlineData("fr", "Hello", "Bonjour")]
[InlineData("fr", "Yes", "Oui")]
[InlineData("fr", "No", "No")]
public void GetTranslationWithCultureFallback(string culture, string name, string expected)
{
// Arrange
LocalizationHelper.SetCurrentCulture(culture);
// Act
string translation = _localizer[name];
// Assert
Assert.Equal(expected, translation);
}

@sivashkevych
Copy link
Author

Yes. I mean PR #41.

It seems that culture fallback isn't functioning correctly for the default culture.
In our project, we've included es.json and en-US.json, setting the default culture to "en-US".

Typically, with the standard IStringLocalizer, if a string isn't found in es.json, it falls back to en-US.json. However, with JsonStringLocalizer, it shows the string key instead.

I noticed in the previous PR that you implemented fallback to the default culture.
Merging PR #41 would enable us to have the same fallback behavior as with the original IStringLocalizer.

Thank you for considering this.

@hishamco
Copy link
Owner

Typically, with the standard IStringLocalizer, if a string isn't found in es.json, it falls back to en-US.json. However, with JsonStringLocalizer, it shows the string key instead.

Please show me the localization setup in your code

@sivashkevych
Copy link
Author

Here is the localization setup:

builder.Services.AddJsonLocalization(options => options.ResourcesPath = "JsonResources");
builder.Services.Configure<RequestLocalizationOptions>(options =>
    {
        var supportedCultures = new[]
        {
            new CultureInfo("en-US"),
            new CultureInfo("es"),
        };
        options.DefaultRequestCulture = new RequestCulture("en-US");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
        options.ApplyCurrentCultureToResponseHeaders = true;
});

builder.Services.AddMvc()
    .AddViewLocalization()
    .AddDataAnnotationsLocalization();
    

@hishamco
Copy link
Owner

As I understand from you the fallback culture in the same culture set is working fine, but the fallback to the default culture is not working, right?

@sivashkevych
Copy link
Author

Correct. Fallback from Spain to English (US) doesn't work.

@hishamco
Copy link
Owner

I will create a unit test, and then check

@sivashkevych
Copy link
Author

I will create a unit test, and then check

Thank you, sir.

@hishamco
Copy link
Owner

Please check one case before I move further make sure that the requested resource is available in the default culture JSON file in your case en-US.json otherwise showing the resource name itself it make sense to me because it's not localized

@sivashkevych
Copy link
Author

We have a "PagesHome" localized string in both en-US.json and es.json.

I removed it from es.json.
After that I can see PagesHome when I add ?culture=es to the URL
and Home when I add ?culture=en-US or by default without specifying culture.

@hishamco
Copy link
Owner

Let me double-check or write a functional test for this case

@sivashkevych
Copy link
Author

Let me double-check or write a functional test for this case

Of course.

@hishamco
Copy link
Owner

@sivashkevych seems you confused with the culture fallback. If the culture is not exists in the supported culture then the fallback will be applied in your case if the culture is fr it SHOULD work, but if the localization find the resource on the supported culture it will show the same resource name if the value is not found

Hope it solved your problem

@hishamco
Copy link
Owner

hishamco commented Jan 5, 2025

Anything to add here @sivashkevych?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants