Resource with database

Asked

Viewed 141 times

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, 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.

  • 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

  • But I can’t understand how I make the controller take on this responsibility. How will the view know which property to choose? @item.?

  • Where does this property come from? languagePT()

  • It is a helper that returns me if Session Culture is pt (true/false). Session["Culture"];

  • And how does he know if he is PT or not? You know or have the implementation of this?

  • I do the same in the other topic, every time the language is changed I change that Session. By default it is EN.

  • 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

  • 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.

  • 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...

  • This link will help you: http://afana.me/post/aspnet-mvc-internationalization-store-strings-in-database-or-xml.aspx Hug

Show 7 more comments

1 answer

1

I think you’re looking for internationalization. Take a look at this answer here.

Edited:

Regarding database internationalization what you can do is:

1 - Replicate the entire database and leave each language with its own database. In this case, when you change language, you will need to change the connection string and runtime. A possible problem with this approach is the timing of the data, which may or may not cause any problems;

2 - Another solution you can adopt is simply adds a new ID value for each table that needs to be internationalized called "Masterid" and another field that specifies the language. The ID points back to the parent record. The rest of the structure remains the same.

I found this information here. Try to read to get more details of how to do.

  • But there it 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.