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

Enhancement: return placeholder in GetValue instead of exception #11

Closed
droegelor opened this issue Sep 1, 2023 · 4 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@droegelor
Copy link

Hello,

I'd like to suggest a modification to the GetValue(string text) method in the LocalizationResourceManager.

Current Behavior:

The method throws a NullReferenceException when a localized string isn't found:

return value ?? throw new NullReferenceException($"{nameof(text)}: {text} not found!");

when I use it in xaml like that:
xmlns:loc="clr-namespace:LocalizationResourceManager.Maui;assembly=LocalizationResourceManager.Maui"
<Label Text="{loc:Translate helloWorld}" />

Proposed Change:

Instead of throwing an exception, it might be more user-friendly to return a placeholder string:

return value ?? $"Text:{text}";

This approach would help avoid application crashes due to missing localization strings and instead display a placeholder, aiding in identifying the missing items.

I believe this change would enhance the overall usability of the library. Your feedback on this suggestion would be greatly appreciated.

Thank you!

@SirJohnK
Copy link
Owner

SirJohnK commented Sep 4, 2023

Hi! Yes, you have a good point. I will look into this and will come back with feedback/solution.

SirJohnK pushed a commit that referenced this issue Sep 6, 2023
- Add SuppressTextNotFoundException setting to suppress/deactive throwing the text not found exception.
- Add option to set a placeholder text displayed if text is not found.
@SirJohnK
Copy link
Owner

SirJohnK commented Sep 6, 2023

See the above commit to development branch for my suggested solution for this.

  • Add SuppressTextNotFoundException setting to suppress/deactive throwing the text not found exception.
  • Add option to set a PlaceholderText displayed if text is not found.

With the new SuppressTextNotFoundException setting you can deactivate the exception and have the option to set a custom PlaceholderText.

Example (will return string.Empty if resource is not found):

.UseLocalizationResourceManager(settings =>
{
    settings.AddResource(AppResources.ResourceManager);
    settings.RestoreLatestCulture(true);
    settings.SuppressTextNotFoundException();
});

...or use default placeholder text (name of resource text)...

settings.SuppressTextNotFoundException(true);

...or use custom placeholder text...

settings.SuppressTextNotFoundException(true, "Text:{0}");

@SirJohnK SirJohnK self-assigned this Sep 6, 2023
@SirJohnK SirJohnK added the enhancement New feature or request label Sep 6, 2023
@droegelor
Copy link
Author

Thank you! This is even better.

@SirJohnK
Copy link
Owner

SirJohnK commented Sep 7, 2023

Thanks!👍 This will soon come in version 1.0.5!😀

@SirJohnK SirJohnK closed this as completed Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants