2013-02-28 55 views
0

我有一個網站,從數據庫中讀取它的一些內容,我需要這個網站的兩種語言,英語和阿拉伯語。動態數據驅動的網站本地化

所需內容以兩種語言在數據庫中重複。可以說我的數據庫中有一個En_Name和Ar_Name列。

例如對於網站的阿拉伯語版本,鏈接將顯示來自Ar_Name的文本,並且對於英文版本,它應該顯示來自En_Name的文本。

我的網站中的靜態內容我認爲這是一個好主意,使用ASP.NET默認本地化(.resx文件)。但我不知道如何爲網站的動態部分做本地化。

那麼,如何才能使相同的超鏈接從Ar_Name字段中讀取一次,然後從基於用戶選擇(本地化)的En_Name中讀取?

回答

0

有很多方法可以做到這一點。你沒有提到你正在使用哪種數據庫技術,所以我的例子是使用實體框架。您可能需要根據自己的情況進行自定義。

LinqToSql或其他ORM可能有類似的可能性。如果你完全使用其他的東西,那麼關鍵是要有一箇中心類,你傳遞一些與翻譯相關的東西(因此是接口)。

例如,如果我使用實體框架,數據庫中每個有這兩個字段的表我都會添加一個暴露這些字段的接口。然後,我會有一個幫助類,它帶有一個方法,該方法接受具有該接口的任何實體,並檢查當前的本地化並返回正確版本的文本。

public interface IBilingualEntity 
{ 
    // Defines a consistent interface that indicates which language version 
    // each thing is in. 
    string Ar_Name { get; } 
    string En_Name { get; } 
} 

public partial MyEntity : IBilingualEntity 
{ 
    // This is a class generate by Entity Framework. But it could 
    // be anything really if you are using some other framework. 
    // 
    // Nothing to do here as the other side of the partial 
    // should already implement the interface with the code 
    // generated from Entity Framework. If not, implement the 
    // interface and return the correct language version in 
    // the property. 
} 

// This is the helper that works out which language version to use. 
public class BilingualHelper 
{ 
    public string GetName(IBilingualEntity entity) 
    { 
     // NOTE: You may have to strip away the region part of the name 
     // but off the top of my head I can't remember the format. 
     // If you are using something else to store the culture you'll 
     // have to reference that instead. 
     var cultureName = Thread.CurrentThread.CurrentUICulture.Name; 
     if (cultureName == "ar") 
      return entity.Ar_Name; 
     return entity.En_Name; 
    } 
} 
+0

謝謝男人,好主意。我正在使用LINQ,而我使用相同的方式來執行此操作。 – RaedK 2013-02-28 11:22:04