1
Well I have the following environment:
Table:
Livro
id
titulo
tituloEN
descricao
descricaoEN
Controller:
[LivroController]
public ActionResult Index()
{
var livros = bdLivro.ListarTodos();
return View(livros);
}
View:
[Index]
@model List<Dominio.PUPO_Livro>
@foreach (var item in Model)
{
if(languagePT())
{
<p>@item.titulo</p>
<p>@item.descricao</p>
}
else
{
<p>@item.tituloEN</p>
<p>@item.descricaoEN</p>
}
}
I would like to know if there is any other method, other than by if.
Follow this other question already asked here that you should achieve: http://answall.com/questions/17594/internationaliza%C3%A7%C3%A3o-com-c-mvc
– Dante
@Dante, but there he does not deal with database. Until what is quoted in the topic I know, enters the languagePT() method, which is working. I wonder if there is any way to replace my view’s if.
– Diego Zanardo
But that’s it, if you used internationalization, your view would only have the <p>@item</p>... But since it doesn’t use it, you can try treating it in the controller (which would be correct, because the view doesn’t need this responsibility) and send it to view only the result
– Dante
But I can’t understand how I make the controller take on this responsibility. How will the view know which property to choose? @item.?
– Diego Zanardo
Where does this property come from? languagePT()
– Dante
It is a helper that returns me if Session Culture is pt (true/false). Session["Culture"];
– Diego Zanardo
And how does he know if he is PT or not? You know or have the implementation of this?
– Dante
I do the same in the other topic, every time the language is changed I change that Session. By default it is EN.
– Diego Zanardo
Perhaps at the time of the creation of your Actionresult Index() you can pass by parameter the language you want to put and according to what you pass put the values of the books in Portuguese or English. At least the responsibility would go out of view
– Dante
But the problem is that the View receives a list of objects of the Book type, the Book class has both title and title, so the view wouldn’t know which attribute to display.
– Diego Zanardo
Exactly, in your control in the Actionresult Index() method you would type: Actionresult Index(String language) and whoever uses the method would necessarily pass a language...in the control you would check, create 2 items (with title and description) and add to a list that would be passed to View instead of the book view...
– Dante
This link will help you: http://afana.me/post/aspnet-mvc-internationalization-store-strings-in-database-or-xml.aspx Hug
– Eugenio Spolti