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 MVC view localization #71

Open
superware opened this issue Apr 7, 2024 · 2 comments
Open

Support MVC view localization #71

superware opened this issue Apr 7, 2024 · 2 comments

Comments

@superware
Copy link

Hello,

After configuring:

services.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
services.AddRazorTemplating();
...
app.UseRequestLocalization("fr");

MVC will locate and use the view Views/Home/Index.fr.cshtml since the locating mechanism is searching for the correct view file using the Controller and View names, but _razorTemplateEngine.RenderAsync("/Views/Home/Index.cshtml") obviously doesn't support localization.

So one way is to explicitly render /Views/EmailTemplates/ConfirmEmail.fr.cshtml which is totally lame, but a better way will be to do all that automatically. So a French user registering the web app will receive the French confirmation email.

It will be awesome if localization support can be added, my guess by internally detecting AddViewLocalization and LanguageViewLocationExpanderFormat.Suffix, and when rendering maybe use LanguageViewLocationExpander to yield all possible localized views and FindView until found. I guess /Views/Home/Index.cshtml can be internally transformed to /Views/Home/{0}.cshtml for this case (ExpandViewLocations).

Any thoughts?

@soundaranbu
Copy link
Owner

Hi @superware, thanks for raising this issue. Yes, this may make sense for the web apps. I haven't worked on localization before. So I'll need to play around with this a bit to see how this can be incorporated into this library.

@superware
Copy link
Author

Hi @soundaranbu, that will be great. IMHO it should be quite easy, the bottom line is looping through possible view-expanded locations until the first one is found - and use that one for rendering.

So if view-localization is enabled (either through the framework or through configuring Razor.Templating.Core), then the view name should be extracted ([^/]+)\.cshtml, templated /Views/Home/{0}.cshtml, expanded /Views/EmailTemplates/{0}.fr.cshtml, formatted /Views/EmailTemplates/ConfirmEmail.fr.cshtml and checked for existance.

Shouldn't be a breaking change since if no localized/expanded view is found then the original view will be used.

Thank you for your great work.

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

No branches or pull requests

2 participants