如何国际化来自数据库的数据?

时间:2022-11-11 20:21:18

I have a ASP.NET MVC Website that relys on resource files for UI internationalization. Some data in the database will be generated by useres (in their language), but some data is generated by me and users shouldn't edit it. For that system data, I would need to store just a "token" in database and translate it at the moment of diplay it to the user in his language.

我有一个ASP.NET MVC网站,它依赖于资源文件进行UI国际化。数据库中的某些数据将由useres(使用其语言)生成,但某些数据由我生成,用户不应编辑它。对于那个系统数据,我需要在数据库中只存储一个“令牌”,并在用他的语言将其显示给用户时将其翻译。

I really don't like the idea of having the translation in the database, and join every text column with the translation tables.

我真的不喜欢在数据库中进行翻译,并将每个文本列与转换表连接起来。

What are my alternatives to accomplish this? May be an hybrid of DB and resources?

我有什么办法来实现这个目标?可能是数据库和资源的混合体?

EDIT: The number of languages is indeterminated, for now we are working with 3 languages, but I hope we could need many other.

编辑:语言的数量是不确定的,现在我们正在使用3种语言,但我希望我们可能需要许多其他语言。

EDIT 2: This is mainly for Catalog data... some catalogs have system and user data in the same table. System data needs translation as is not editable by the user.

编辑2:这主要是针对目录数据...某些目录在同一个表中有系统和用户数据。系统数据需要转换,因为用户无法编辑。

2 个解决方案

#1


1  

You could actually store just the identifiers in the database and resolve them to language content from Resource Files (regular *.resx) at runtime.
Although in such case it quite does not make sense to store this in the database at all (just using resource files would suffice). And of course maintaining this would be a nightmare (you would need to actually synchronize contents of the database with resource files).

实际上,您可以在数据库中存储标识符,并在运行时将它们从资源文件(常规* .resx)解析为语言内容。虽然在这种情况下根本没有意义将它存储在数据库中(仅使用资源文件就足够了)。当然,维护这将是一场噩梦(你需要实际上将数据库的内容与资源文件同步)。

Other than that, you could actually create your own Resource Manager to read the contents from the database, and put translations along with identifiers - in such case, your primary key would need to be defined on both resource identifier and language (I think it is called compound key, isn't it?):

除此之外,您实际上可以创建自己的资源管理器来读取数据库中的内容,并将翻译与标识符一起放置 - 在这种情况下,您的主键需要在资源标识符和语言上定义(我认为它是叫复合键,不是吗?):

---------------------------------------------------------
| Id | Locale | Translation                             |
---------------------------------------------------------
| 1  | en-US  | Something happened on the way to heaven |
| 1  | pl     | Coś się stało na drodze do nieba        |
---------------------------------------------------------

But it would require modifying Database Schema...

但它需要修改数据库架构......

#2


0  

I'm not clear reading your post whether you are maintaining user-generated content together with your own localizable content in the same DB table. If that's the case, it sounds like you should consider separating them.

我不清楚您是否在同一个数据库表中维护用户生成的内容以及您自己的可本地化内容。如果是这种情况,听起来你应该考虑将它们分开。

If your own content is stable (i.e. you won't be adding new content or changing it between deployments of updates of your app), then it should live in your localizable resources rather than in the database and you should treat it just like your localized UI.

如果您自己的内容是稳定的(即您不会添加新内容或在应用程序更新的部署之间更改它),那么它应该存在于可本地化的资源中而不是数据库中,您应该像处理本地化一样对待它UI。

If on the other hand it is dynamic content, then there seems to be no alternative to storing it in the DB. You would need to store a content ID as well as a language ID for each (the compound key Paweł Dyda refers to) in a separate localization table that provides the translated versions of each content. Or perhaps you could rely on a multilingual CMS to manage this multilingual content (see for example the WPML plug-in for WordPress)

另一方面,如果它是动态内容,那么似乎没有其他方法可以将其存储在数据库中。您需要在单独的本地化表中存储内容ID以及每个(复合键PawełDyda引用的)的语言ID,该表提供每个内容的翻译版本。或者您可以依靠多语言CMS来管理这种多语言内容(例如,参见WordPress的WPML插件)

#1


1  

You could actually store just the identifiers in the database and resolve them to language content from Resource Files (regular *.resx) at runtime.
Although in such case it quite does not make sense to store this in the database at all (just using resource files would suffice). And of course maintaining this would be a nightmare (you would need to actually synchronize contents of the database with resource files).

实际上,您可以在数据库中存储标识符,并在运行时将它们从资源文件(常规* .resx)解析为语言内容。虽然在这种情况下根本没有意义将它存储在数据库中(仅使用资源文件就足够了)。当然,维护这将是一场噩梦(你需要实际上将数据库的内容与资源文件同步)。

Other than that, you could actually create your own Resource Manager to read the contents from the database, and put translations along with identifiers - in such case, your primary key would need to be defined on both resource identifier and language (I think it is called compound key, isn't it?):

除此之外,您实际上可以创建自己的资源管理器来读取数据库中的内容,并将翻译与标识符一起放置 - 在这种情况下,您的主键需要在资源标识符和语言上定义(我认为它是叫复合键,不是吗?):

---------------------------------------------------------
| Id | Locale | Translation                             |
---------------------------------------------------------
| 1  | en-US  | Something happened on the way to heaven |
| 1  | pl     | Coś się stało na drodze do nieba        |
---------------------------------------------------------

But it would require modifying Database Schema...

但它需要修改数据库架构......

#2


0  

I'm not clear reading your post whether you are maintaining user-generated content together with your own localizable content in the same DB table. If that's the case, it sounds like you should consider separating them.

我不清楚您是否在同一个数据库表中维护用户生成的内容以及您自己的可本地化内容。如果是这种情况,听起来你应该考虑将它们分开。

If your own content is stable (i.e. you won't be adding new content or changing it between deployments of updates of your app), then it should live in your localizable resources rather than in the database and you should treat it just like your localized UI.

如果您自己的内容是稳定的(即您不会添加新内容或在应用程序更新的部署之间更改它),那么它应该存在于可本地化的资源中而不是数据库中,您应该像处理本地化一样对待它UI。

If on the other hand it is dynamic content, then there seems to be no alternative to storing it in the DB. You would need to store a content ID as well as a language ID for each (the compound key Paweł Dyda refers to) in a separate localization table that provides the translated versions of each content. Or perhaps you could rely on a multilingual CMS to manage this multilingual content (see for example the WPML plug-in for WordPress)

另一方面,如果它是动态内容,那么似乎没有其他方法可以将其存储在数据库中。您需要在单独的本地化表中存储内容ID以及每个(复合键PawełDyda引用的)的语言ID,该表提供每个内容的翻译版本。或者您可以依靠多语言CMS来管理这种多语言内容(例如,参见WordPress的WPML插件)