2012-12-13 223 views
5

我知道大多数人使用下面的方法并为需要翻译的特定表创建翻译表,但这可能相当于表格的负荷。表格的语言翻译

CREATE TABLE Product 
(
    Product_id 
    ,ProductTrans_id -- FK 
) 

CREATE TABLE ProductTranslation 
(
    ProductTrans_id 
    ,Product_id 
    ,Name 
    ,Descr 
    ,lang_code 
) 

下面的方法是否可行?假设您有许多表格需要翻译超过1列。你能否做以下事项,将所有翻译保留在1个表格中?我想这个表格会随着时间的推移而大幅增长。

CREATE TABLE translation_entry (
      translation_id  int, 
      language_id   int, 
      table_name   nvarchar(200), 
      table_column_name  nvarchar(200), 
      table_row_id   bigint, 
      translated_text  ntext 
     ) 

    CREATE TABLE translation_language (
      id int, 
      language_code CHAR(2) 
     ) 

所以使用第二种方法,你会得到像这样

select 
    product.name 
    ,translation_entry.translated_text 
from product 
inner join translation_entry on product.product_id = translation_entry.table_row_id 
and translation_entry.table_name = 'Product' and translation_entry.table_column_name = 'Name' 
and language_id = 3 
+1

第二种方法似乎是一个很大的开销,并将涉及多个获取单个产品的翻译列..如果我正确地理解它..? +1,因为这是个好问题! –

+0

我想一个好方法是首先考虑你的查询将看起来像什么..这将要求表设计 –

+0

在第二种方法,你可以筛选TableName和ColumnName,然后通过table_row_id链接。也许很慢查询。只是想办法做到这一点,当需要翻译新表或列时,不需要模式更改。 – davey

回答

2

文字我不知道为什么你担心表的数目:具有更少的表并不意味着你的数据库更小,更高效或更好的设计。特别是如果减少表的数量会增加查询的复杂性,那么我会非常小心地做到这一点。

无论如何,我会去每个'基'表的一个翻译表。主要原因是你的第二个解决方案不灵活:如果主键不是一个整数,那么实现和使用就变得非常困难。查询翻译也更加复杂,并且根据表格和数据的大小,可能难以有效地对其进行索引。

不清楚为什么你在Products表上有TranslationID;一般的关系是周围的其他方式:

create table dbo.Products (
    ProductCode char(10) not null primary key, 
    ProductName nvarchar(50) not null, 
    ProductDescription nvarchar(100) not null, 
    -- other columns 
) 

create table dbo.ProductsTranslations (
    ProductCode char(10) not null, 
    LanguageCode char(2) not null, 
    ProductName nvarchar(50) not null, 
    ProductDescription nvarchar(100) not null, 
    -- other translations 
    constraint FK1 foreign key (ProductCode) 
     references dbo.Products (ProductCode), 
    constraint FK2 foreign key (LanguageCode) 
     references dbo.Languages (LanguageCode), 
    constraint PK primary key (ProductCode, LanguageCode) 
) 

根据您的工具集,你可能想直接从基地的人产生转换表作为数据库构建的一部分部署过程。您可以使用视图来提供方便的“完全翻译”版本的基表。

一个有趣的问题是Products中的列使用什么语言,以及如果不需要翻译时可以直接使用它们。我的建议是所有的产品代码都应该传递一个语言参数,并且仅从ProductsTranslations表中获取文本,即使是英文版(或任何您的公司内部语言版本)也是如此。通过这种方式,您可以确保在同一个表中找到所有“官方”名称,并且基表上的列出现在清单和数据模型的完整性以及开发人员便利性和(可能)在特定内部使用报告等。

+0

感谢Pondlife,我只是在寻找其他方法来实现语言表。你所描述的设计似乎是最好的方法。 – davey