2012-12-13 222 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