好办法,按要求动态变化的语言资源好办法、语言、动态、资源

2023-09-03 17:10:56 作者:装逼是挨揍的前兆╮

我有一个ASP.NET Web API的应用程序,它应该响应用户的Accept-Language头适当。

I have an ASP.NET Web API application which should react to user's Accept-Language header appropriately.

目前,字符串被存储在RESX并通过Visual Studio的生成的类在编译安全的方式访问。我想这样做是为了保持目前的做法,并创建附属程序集的RESX每个翻译版本。然后分析用户的Accept-Language头,看用户是接受语言和加载资源的附属程序集所请求的语言。

Currently, the strings are stored in the resx and accessed in compile-safe manner through Visual Studio's generated class. What I would like to do is to keep the current approach and create satellite assemblies for each translated version of resx. Then to analyze the user's Accept-Language header to see what languages a user accepts and load the resources for the requested language from the satellite assembly.

我想我可以实现通过创建一组特定语言的的ResourceManager 的帮助下对象的的ResourceSet 但它不可能保持编译时的安全性,由于Visual Studio利用自动更新类RESX文件的照顾。

I suppose I could implement all this behavior myself by creating a set of language-specific ResourceManager objects with the help of the ResourceSet but then it would not be possible to keep the compile-time safety, since Visual Studio takes care of automatically updating the class for resx file.

什么是动态挑本地化语言资源的最佳方法是什么?

What would be the best way to pick the localized language resource dynamically?

推荐答案

从阅读你的问题,我没有看到任何ASP.NET不会自动提供。您可以配置ASP.NET(的WebForms或MVC是否)使用接受语言请求头,并设置适当的UICulture(其中卫星组件通过ResourceManager加载,这将影响)和文化(这将影响区域设置相关的格式化和解析,如日期和数字),适当的。

From reading your question, I don't see anything that ASP.NET doesn't offer automatically. You can configure your ASP.NET (whether WebForms or MVC) to use the accept-language request header and set the appropriate UICulture (which will impact which satellite assembly is loaded by ResourceManager) and Culture (which will impact locale-dependent formatting and parsing such as dates and numbers) appropriately.

要配置您的应用程序使用接受语言列表中同时设置的UICulture和文化为每个请求,(按的这个MSDN页面),配置你的web.config是这样的:

To configure your app to use the accept-language list to set both UICulture and Culture for each request, (as per this MSDN page), configure your web.config like this:

<globalization uiCulture="auto" culture="auto" />

还有每页同等配置设置。

There is also an equivalent configuration setting per page.

然后,按照资源后备的过程中,如果您的应用程序包括卫星组件匹配的文化(或者,如果做不到这一点,它的父区域性),它将被使用的资源管理器。如果不是,那么默认的资金将用于(英语,如果这是你的基本语言)。

Then, as per the Resource Fallback process, if your app includes a satellite assembly for the matching culture (or, failing that, its parent neutral culture), it will be used by the Resource Manager. If not, then your default resources will be used (English if that's your base language).