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

Support Live-Refreshing of IStringLocalizer Strings #7572

Closed
bgribaudo opened this issue Feb 14, 2019 · 2 comments
Closed

Support Live-Refreshing of IStringLocalizer Strings #7572

bgribaudo opened this issue Feb 14, 2019 · 2 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@bgribaudo
Copy link

This is a belated response to the following request from #3289 (comment):

@mkArtakMSFT:

@bgribaudo, can you please file a separate issue for the following ask. We would like to review it further:

Any chance that support could be added for refreshing cached localizers so that localization strings can be updated live?

In ASP.Net Core 2.1, changes made to pages (*.cshtml) are reflected on the site as soon as the change is saved and the browser window is refreshed. The same holds true with the IViewLocalizers used by pages—updates to these localization strings are reflected on the site as soon as they are saved and the browser window refreshed.

However, the IStringLocalizer instances used for form validation error message are cached during the first request that accesses a particular localizer and remain cached until the site is restarted.

This latter behavior breaks the ability to fully live edit all the text on a page during development as a restart is required every time an IStringLocalizer’s source data is modified.

It also blocks the ability for custom IStringLocalizers to pull live data from external sources (e.g. a database). Instead, when translation strings are updated in the external database, some process needs to take place that restarts the site so that cached localizer instances are purged and new localizer instances are populated with the revised strings (unless someone pulls off fancy code that goes into ASP.Net Core’s internals and wires in a cache purge feature).

Proposal

The ask is for there to be some way to recreate (or refresh) IStringLocalizers on a per-request basis without restarting the site.

Ideas on how this could be done:

  • A simple way to do this would be to not cache those localizers across requests. Since this is already the way that IViewLocalizers are handled, it doesn’t seem like too terrible an option. 😊
  • Another idea would be to allow specifying a caching stagey when an IStringLocalizerFactory is registered (e.g. an optional argument on the registration method allowing choices like: cache across requests, do not cache, etc.). This would allow the localizer developer to choose the behavior most appropriate for the particular situation.
  • A third option would be to provide a method that purges the cache of localizer instances when called. A developer could then hock in a method that runs on each request which calls this method when appropriate.

Related: aspnet/Mvc#7887 (comment)

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 14, 2019
@hishamco
Copy link
Member

I like the 2nd and 3rd options to get live refreshing resource values, but there's one point we should take care about the save and refresh approach is useful in development, but for production precompiled views are preferred

@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @bgribaudo.
After some review on this issue we believe this is not something we can provide out of the box, based on the existing ResourceManager based implementation.
Your suggestions look good and you can implement your custom IStringLocalizer to solve this issue.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

4 participants