EF Code First 多语言支持
假设您必须使用 EF Code First 和 POCO 类开发一个支持多种语言的网站,您将如何对 POCO 类进行建模来支持这种情况?
通过多语言支持,我的意思不仅是在您的 UI 的某些资源文件中进行标签翻译,而且您还需要在数据库中存储给定域实体的名称、描述等。
例如:假设您的领域模型有一个实体产品。在您的数据库中,您需要存储产品的名称和名称。 2 种语言的描述(为简单起见,有 2 个字段)(但将来可以添加更多语言)。 根据当前的文化,UI需要从资源文件中获取标签的文本(这很容易实现),但您还需要显示产品的名称和名称。来自数据库的描述。
Assuming you have to develop a website supporting multiple languages with EF Code First and POCO classes how would you model your POCO classes to support this scenario?
By multi-language support I meant not only having e.g. labels translation in some resource files for your UI, but also you'd need to store Name, Description, etc for a given domain entity in your database.
E.g.: Let's say your Domain model has an entity Product. In your database you need to store the Product' Name & Description (2 fields for simplicity) for 2 languages(but more languages can be added in the future).
Based on the current culture, the UI needs to pick up the labels' text from the resource files(this is easy to implement) but you also need to display the Product' Name & Description that comes from the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我创建了自己的类来支持数据库数据中的多语言。
我是西班牙人,我已经在这里翻译了我的代码,也许它有简单的语法错误,
这里的代码:
你可以构建 DbContext 类并进行一些测试,如下所示:
我有只是为了结束它,我希望这一切顺利;)
最诚挚的问候!
更新
还有一件事,当您删除产品时,不要删除多语言字符串条目。您可以将业务添加到您的服务中,并在删除产品时删除多语言条目。
或不!。如果您不删除多语言条目,您将拥有越来越多的 MultilingualStrings 条目,并且您可以利用此存储库为未来的产品提供翻译后的产品名称。
您可以将 MultilingualStrings 设置添加到上下文:
然后您可以在 ProductNameTextBoxOnChange 事件中搜索 MultilingualStrings,例如:
您可以使用此存储库来获取具有多语言字符串的其他实体的其他属性。
Finally I have made my own classes to support multilingual in my database data.
I'm Spanish and I have translated my code for post here, Maybe it have simple syntax errors,
Here the code:
You can build the DbContext class and make some test like this:
I have just to end it, I hope this works fine ;)
Best regards!
UPDATE
One more thing, when you remove the product don't remove the multilingual string entry. You can add bussines logical to your services and remove the multilingual entry when you remove the product.
Or not!. if you don't remove the multilingual entry you will have more and more MultilingualStrings entries and you could take advantage of this repository to offer translated products names for the future products.
You can add MultilingualStrings set to the context:
Then you can search MultilingualStrings in the ProductNameTextBoxOnChange event for example:
And you can using this repository for other properties of other entities that have multilingual strings.