gpt4 book ai didi

c# - 全局化架构

转载 作者:太空狗 更新时间:2023-10-30 01:28:40 25 4
gpt4 key购买 nike

我需要在数据库中存储电子商务解决方案的产品。每个产品都应该有描述性信息,例如名称、描述等。

我需要将任何产品本地化为 x 种语言。

到目前为止,我所做的是制作任何应该本地化的列和 nvarchar(MAX)然后我像这样存储一个 XML 字符串:

<cultures>
<culture code="en-us">Super fast laptop</culture>
<culture code="da-dk">Super hurtig bærbar</culture>
</cultures>

当我将它从数据库加载到我的业务逻辑对象中时,我将 XML 字符串解析为 Dictionary<string, string>关键是文化/语言代码。

所以当我想显示产品名称时,我会这样做:

lblName.Text = product.Name["en-us"];

有没有人有更好的解决方案?

最佳答案

您应该将当前语言存储在某处(例如,在 singleton 中)并在 product.Name 属性中使用语言设置来获取正确的字符串。这样,您只需为每个字段编写一次特定于语言的代码,而不用考虑使用该字段的所有地方的语言。

例如,假设您的单例是在存储与当前语言对应的枚举的 Localizer 类中定义的:

public class Product
{
private idType id;
public string Name
{
get
{
return Localizer.Instance.GetLocalString(id, "Name");
}
}
}

GetLocalString 看起来像这样:

  public string GetLocalString(idType objectId, string fieldName)
{
switch (_currentLanguage)
{
case Language.English:
// db access code to retrieve your string, may need to include the table
// the object is in (e.g. "Products" "Orders" etc.)
db.GetValue(objectId, fieldName, "en-us");
break;
}
}

关于c# - 全局化架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com