有很多方法可以做到这一点。您没有提到您使用的是哪种数据库技术,所以我的示例是使用实体框架。您可能需要根据自己的情况对其进行自定义。
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;
}
}