Where does the language translation match the MVC pattern?

I am creating a multilingual web application using the MVC template as the starting position. The application has a number of forms that users will interact with, and many of these forms will have fields that search from a database table, for example, "Region."

If I need parameters in these lists that will be displayed in the language of users on the screen, I can see a couple of ways to do this:

  • In the model. When querying a model, I can provide the language in which I want to return the results. This will allow you to use it everywhere where the data from the model will be displayed without change. However, this also means that the provincial model in my example (plus all other application models) should now know how to perform language translations.
  • In the controller. I can query the model in the controller action as usual, and then create a Translator object with which I can pass the results before completing the action. This would mean that each controller action would potentially duplicate the same translation code, violating the DRY principle.
  • In view . Since the representation of representations is usually assumed in representations, and the user's language does not affect the business logic of the system, there may be an argument that the language translations belong here. Especially considering that the page may also contain static content that will need to be translated. The disadvantage of this is that it will somewhat complicate the views, especially for interface designers who will have to work on a new translation code.

Is there an acceptable good practice in which text translations belong to the MCV template for web applications? Will this even change if I have to load picklist options via an AJAX call instead of page load time?

Thanks for the help!

+6
source share
4 answers

If you need to translate part of the user interface, I will create a helper method that will read the resource file and display the translated string for these resources. For instance.

@Translate("NewUserHeading") 

So, with regard to the user interface, it makes sense to handle this in the user interface.

If the data you are about to transfer at some point can be displayed in the Flash client or mobile application, then it must be translated by the server and should not have anything to do with your MVC application.

+1
source

The best place to process is the view. Your question concerns only dynamic data from the database, but you should also process static information in your views. It is best to treat those who are in the same place. A common practice in MVC for processing multiple languages ​​is resource strings, separate representations for each language, or a combination thereof. For information from resource string databases will work well. You must store the token in the database for options in the list (this token can be a translation into English), and the view will receive the corresponding translation from the resource for the specified country / region. There is a good detailed explanation of this approach in this blog post .

+5
source

Honestly, any interaction with the database must be processed in the model, and this is the only thing that is processed in the model. The interpretation / organization of this data must be done in the controller. I suppose that more information is needed on where this translation comes from and how it works in order to really give a solid answer.

0
source

Only rows from the resource file will be displayed in the view. Including the correct locale resource file should do this. In web applications, it is often a monolingual JS file that defines the user interface lines for each locale, for example strings.en-us.js, strings.pt-br.js, etc.

Some lines come from the server dynamically, the dispatcher does not need to know about this, the model should just capture the localized lines, and the return should be part of the data request.

So, this is either in the view (if it is static) or in the model (if it is dynamic). Controllers should simply translate data from view to model and from model to view

0
source

Source: https://habr.com/ru/post/912852/


All Articles