-
Notifications
You must be signed in to change notification settings - Fork 156
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
Add support for ASP.NET Core #293
Comments
100% agree... I will be very happy with this enhancement... +1 |
I have spent some evenings working on i18n and ASP.NET Core and have a prototype working. There is quite a lot to be done and one must decide on whether to embrace the new IoC approach or do the minimal work and keep the static and singelton classes. Some the required changes is:
Basically - the i18n middleware must add it's own MemoryStream into the pipeline only for the URI's to be translated. This will result in i18n having a buffer of the whole output. I do wonder whether this should be added into the existing branch with asp.net support or if this should be a separate branch for asp.net core. I would like to chip in and help make the code as we use i18n in our apps. |
Would you elaborate on this? Are the nuggets translated somewhere else? Are you meaning a duplication of the response buffer? Are there performance implications? Is this a fair summation of the situation: Path 1: Keep as much as possible from current codebase (e.g. static singletons rather than embrace new IoC/DI model) and merge into current project. Path 2: As Path 1 but fork. Path 3: Start new project from scratch, adopt .NET Core / ASP.NET Core best practices and borrow wherever appropriate from current project. If path 3 is chosen, we can link to it from here wherever it ends up. I have to say I'm not currently working on ASP.NET Core but fully appreciate the need to do this (esp. as ASP.NET Core still seems to be in love with resource files!). |
In order to minimize risk in the existing codebase I would choose either 2 or 3. Nugget is translated with the standard NuggetLocalizerForApp but to intercept Response.Body the middleware must use it's own MemoryStream, like this:
See also this blog post: https://www.billbogaiv.com/posts/using-aspnet-cores-middleware-to-modify-response-body So to minimize performance issues the test for whether content should be localized or not should be done before this code block. |
It is not that hard to load Resources from another storage type than .resx files in ASP.NET Core but I really like the PostBuild to extract the nesources and the simplicity of translation with .po files. In Our apps we load translations from database and I have created our own DbTranslationRepository with EF. |
Agree with ruling out path 1. I'm worried Path 2 could end up pretty messy. What does your prototype look like in that regard? |
I think it would be easiest to start with a new repo (doesn't need all history) and it would be possible to do a very similar approach to use the LocalizedApplication.Current and the other static helpers. My prototype goes further and use the builtin IoC container and keeps a Singeton instance of
All that is needed to use i18n in MVC Core is 2 lines with AddI18N/UseI18N in Startup.cs:
Example configuration in appsettings.json:
There is still work to be done on some of the static members used for configuration and the helper extension methods. My code compiles and runs with ASP.NET Core in DOTNETCORE. I havent tried to add Projects for NET451 yet (but should be fairly easy). The idea was to have most of the code in "Shared Projects" and then add the most platform specifc implementation in targeted projects. IE: code may be in a "Shared Project" but is actually included and compiled in each Project. There is also the event hooks that either must be kept or rewritten to an interface so that devs can create and register their own implementation to override the default. |
Do you mean a shared/common i18n.Common project, then dependent projects off of that, say i18n.ForCore and i18n.ForNet4x (or whatever)? If so, that means re-factoring what we have now, right? Then if so, that means going from i18n v2 to i18n v3? |
Yes, Shared Projects is feature that first came in VS2013 Update 2. Previously you could "share" code by linking files into separate Projects (where each Project was compiled to a dll). A Shared Project is never compiled, it doean't have any References. I have a i18n.Domain.Shared project that just contains code (and has no references - is never compiled to dll). Then I reference this project into i18n.Domain.netstandard to compile the code for DOTNETCORE 1.0. I can also reference this project into i18n.Domain.Net that targets .net 4.5.1 framework. So it is a useful feature for having code in just one "shared project" and create "thin" projects for each specific target or place some platform specific code in that project. You can also use conditional compilation symbols to "tailor" the shared code for each target. in i18n.netstandard I would reference i18n.Shared but may also place the code for i18nTranlationMiddleware in the i18n.netstandard project. F.ex: In DOTNETCORE there is no Thread.CurrentThread.CurrentCulture/CurrentUiCulture.
And may be placed in a Shared project. In terms of i18n for ASP.NET Core there must be 2 folders in the nuget package: The net451 folder will contain i18n.dll and i18n.Domain.dll compiled for .NET 4.5.1 Yes it means re-factoring what we have now and could possibly also be used for support of the standard ASP.NET MVC and ASP.NET WebPages. Yes, I would put his into a v3. |
I notice you reference HttpContext in your above code snippet. It was with this class that my concerns about the complexity of all this began. My understanding is that the |
I focused only on ASP.NET CORE and used HttpContext directly. And yes there are some differences such as HttpContext Request.Uri does not exist. BTW, there is no HttpContextBase in ASP.NET Core. You get access to the HttpContext in the middleware. This is the signatur for the middlevare:
And from the middleware I would pass it into the EarlyUrlLocalizer. |
In terms of code I would have some specific code in each target project. Like for ASP.NET Core use HttpContext and for ASP.NET MVC use HttpContextBase. For ASP.NET Core I would have EarlyUrlLocalizer implemented as its own middleware class. |
But |
I'm keen to test/contribute; is there a branch/fork/nuget somewhere I can grab to try it out? |
Where does this enhancement stand? I'd like to use this feature in a project I'm developing in ASP.Net Core. Is this the version at https://github.com/jonnybee/i18n? If not, is there a fork of the project with the work you've done so far that I can get a look at? |
My proof-of-concept is not in a published branch. The fork at https://github.com/jonnybee/i18n does not contain these changes. Support for i18n in ASP.NET Core needs some careful design and guidance as to how big changes to make (minimalistic or refactor/rewrite) and whether code/docs should be put into it's own repo or to be a part of the existing repo. There will also be a need for several new published NuGet packages and documentation. Remember: ASP.NET Core supports both .NetCore and .Net Full Framework 4.5.1 and newer runtime. |
We're building a new ASP.NET Core project now and would really like to use i18n. Is there anything I can do to raise the priority of this enhancement? An increasing number of virtual machines running on Azure are Linux and Microsoft's interest in migrating to ASP.NET Core is vital to the future of .NET. Since i18n is the preeminent internationalization library for .NET, I think this enhancement would be a necessary step in its evolution. jonnybee - I appreciate the significance of the architectural changes that you and Martin discussed last fall. Nevertheless, you indicated you had a prototype back in October. Is there some way we can get a copy of it? |
@amainiii I agree with your comment. From my own situation just now all I can say is sponsorship would raise the priority. |
@turquoiseowl Please contact me privately to explore sponsorship options. My email is in my profile, or if you prefer, Twitter or LinkedIn |
Hi @turquoiseowl & @jonnybee I would like to somehow contribute to porting i18n to core 2, however, I have no idea where the project stands and what is needed. Could you share some info, please? Thanks in advance Regards Morten |
Hi, Are there some news on asp.net core support? Thanks, |
I love TurquoiseoOwl i18n, but I don't know the inner workings of this project enough to modify it. Especially since it seem like it would require quite a rewrite of alot of things. So I chose to do my own custom solution instead. Thought it might help someone, or help this project along, since nothing seems to have happend in several years regarding this issue. First the Middleware:
Then we have a translator class, and the NuggetReplacer that does the actual translations. My custom NuggetReplacer uses a component called Karambolo to extract the translations from the Po file.
I then configure the middleware in Startup.cs -> Configure at the top of the method, before any other middleware, and add it so the translator can be injected aswell in the ConfigureServices method (optional)
Combine this with the i18n.PostBuild.exe for pot-extraction - and you've got a complete working solution. [Updated 2020-03-13, because I discovered the previous code had problems with error/excption handling, not passing exceptions down to the exception handler middlewares, and not restoring the Response.Body when exceptions occurred. Also added the i18nPoTranslator middle step, to enable translation both through middleware and injection. It makes the code listings a bit longer, but also alot more versitile] |
@HenricObjektvision A .NET core version is available at: https://github.com/fintermobilityas/i18n.core It does not support all the features this project does but it made it possible for us to migrate a large MVC enterprise application without replacing the underlying localization technology. |
@turquoiseowl where are we at with this one? I'd be willing to put something towards sponsorship for this task if it meant it got things moving. @peters awesome! Would be nice to get an idea of what is or isn't supported compared to this version. |
i18n should also support ASP.NET Core on both .NET Core (netstandard1.6) and .NET 4.5.1 and higher (the same platforms supported by ASP.NET Core). These apps will always run in Kestrel and i18n should plug in as middleware, f.ex:
I wonder if this would be best suited to do as a major overhaul as i18n should publish NuGet packages with support for (at least) 2 different target framework (net451 and netandard1.6) and webframeworks like ASP.NET and AspNetCore.
The text was updated successfully, but these errors were encountered: