0

Situation

We are rebuilding a web application (winforms), from the ground up. The old application will keep running, when the new one is released. This means that the new application needs to talk to an old database, I can't change anything there. The new application will be build with .NET Core 2.0 MVC.

The old application uses a custom translation database table, based on a language-id and translation-id. Very straighforward.

Problem

The old application made a database call for each translation, over and over again. Because the new application needs to reuse this table, i'd rather have a smarter system. Because of the amount of translations, I don't want to get all data on each page request. I also don't want to make a database-call for each translation.

Idea

My idea is to create a custom HtmlHelper (or dependency injected translation instance, but that might be harder in this situation) which only needs the translation ID. The helper will get the language from a cookie. This way, we can simply use the following code in our views:

@Html.Translate(1337)

What i'd like to achieve, is to use the first Translate-call to scan the RazorView for all Translate-calls (with their parameters). This way, I can use a Dependency Injected Scoped instance to save all the translations to. Which means that I only have to make one database call for each requested view.

Actual question

Does this sound like a good idea, and will this be doable? I searched through the MVC source code, but can't find a way to achieve something like this. The RazorView instance in the IHtmlHelper does not seem to have list of IHtmlHelper's or something...

McGuireV10
  • 9,572
  • 5
  • 48
  • 64
  • Have you ever thought of caching? Translations are rather static, so you could just store them in memory in the most simple case – Marco Mar 22 '18 at 13:33
  • We will probably use caching, but I want to prevent the initial page requests from sending way too much database requests... – Arjen L Mar 22 '18 at 13:41
  • The just use one request per view and resolve it into a dictionary on the server. – Marco Mar 22 '18 at 13:44
  • That's what I try to accomplish. For the sake of easier programming, I want to scan the Razor pages to index all the translation ID's. I don't want to have to manually keep a list/dict of ID's. – Arjen L Mar 22 '18 at 13:46
  • Don't scan the view. I'd rather go for a distributed key like `` so you could retrieve all keys for your Products controller and then access the `IndexPageTitle` key. Your localization service, which gets injected into the controller, could take the resourceKey as a paramter or generic type parameter to load up all specific keys for this class. – Marco Mar 22 '18 at 13:50
  • And here's even a ready to use package: https://damienbod.com/2016/01/29/asp-net-core-1-0-using-sql-localization/ – Marco Mar 22 '18 at 13:51

0 Answers0