Prevent exception messages from being translated into the user's language?

帅比萌擦擦* 提交于 2019-11-28 20:14:05

You can try setting Thread.CurrentThread.CurrentUICulture and/or .CurrentCulture to CultureInfo("en-US").

All the more reason why the exceptions shouldn't be translated (badly). When logging exceptions, it makes muc more sense to do it in a single language. I can't believe Microsoft didn't think of a way to do this other than UICulture, which is basically a non-option :(

If it's an ASP.NET application, you can set the UI language in web.config (*):

<system.web>
    <globalization ... uiCulture="en-US" ... />
</system.web>

For other applications, the current user's regional settings are used by default, and you have to explicitly override it - e.g. Thread.CurrentUICulture = new CultureInfo("en-US").

(*) caveat - if an error in the config file results in an exception being thrown before the element is processed, you'll get the default uiCulture.

Forcing exceptions to display in a different language seems a bit harsh on the user... can you display an error code along with the message? Then the user will get something they can understand, and you can look up the error code for the translated version.

I'm not a .net guy so I don't know if this is possible, just an idea.

How do I make my application always use English when displaying win32/.net exceptions messages?

First of all, don't show win32/.net exception messages to users. You should handle exceptions rather than showing them to user.

By default exception messages will be shown in current's UI language (if appropriate language pack is installed, otherwise they fallback to English). You can change exception messages changing Thread.CurrentThread.CurrentUICulture property, however it will affect the whole GUI of your app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!