数据库中的本地化实体 [英] Localized entities in database

查看:63
本文介绍了数据库中的本地化实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发ASP.NET Core MVC应用程序,需要为实体设计多语言值.有关如何使用.resx在UI页面中支持本地化的信息很多.我正在寻找一种常见的模式,该模式如何支持实体(而不是网页上的静态内容)的本地化,用户可以对其进行编辑.

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.

让我们说数据库表中有一个带有Statuses的简单字典

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?

目前我有几种方法

1)使用所有受支持的语言创建表 Languages .创建表翻译

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

状态表将为

    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)不创建Translations表,而是添加列

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; }

}

您还可以使用 virtual 关键字来支持相关翻译或主要实体的延迟加载.

You may also use virtual keyword to support lazy loading of related translations or main entity.

这篇关于数据库中的本地化实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆