问题
I'm developing a ASP.NET Core MVC application and need to design Multilanguage values for entities. There is a lot information how to support localization in UI pages using .resx. I'm looking for some common pattern how to support localization for entities (not for static content on web page), that can be edited by user.
Let say there is a simple dictionary in database table with Statuses
Id Name
----------------------------------------------
1 Not processed
2 To be cancelled
3 To be corrected
4 Processed
5 Rejected
User can create a new status or add translation for other language. The question is how to design tables and how to store translations for other languages?
For now I have a few approaches
1) create table Languages
with all supported languages. Create table Translations
Id LanguageId Key Value
------------------------------------------------
1 en NotProcessed Not processed
2 pl NotProcessed Nie przetworzony
3 de NotProcessed Nicht verarbeitet
4 en ToBeCancelled To be cancelled
5 de ToBeCancelled Zu stornieren
the status table will be
Id Name TranslationKey
----------------------------------------------
1 Not processed NotProcessed
2 To be cancelled ToBeCancelled
then using according to language select proper translations that will show to user.
2) do not create Translations table, but add column
Id Name Translations
----------------------------------------------
1 Not processed en:Not processed;pl:Nie przetworzony;de:Nicht verarbeitet
2 To be cancelled en:To be cancelled;de:Zu stornieren
What do you think about it? I don't know how to keep translations for my entities, may be there are some reliable ways. Please suggest.
回答1:
I do something similar to approach no.1 by creating languages table and translations tables. But the translations tables are entity based, that's mean each entity type has its own entity translation type as well.
The entity translation holds all localizable fields (e.g. Title, Body), and the main entity holds non-localized fields (e.g. publish date, etc.)
public class Language
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Article
{
public int Id { get; set; }
public DateTime PublishDate { get; set; }
public ICollection<ArticleTranslation> Translations { get; set; }
}
public class ArticleTranslation
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public int ArticleId { get; set; }
public Article Article { get; set; }
public int LanguageId { get; set; }
public Language Language { get; set; }
}
You may also use virtual
keyword to support lazy loading of related translations or main entity.
来源:https://stackoverflow.com/questions/59779477/localized-entities-in-database