简体   繁体   中英

How to force the language of a .NET application?

I have a .NET application (VB.NET) that was working fine until someone from Germany tried it.

I can see that the problem is im catching exceptions with "try catch exemption" and parsing the exemption string.

This works fine when the application is in English but is failing in any other language (obvious, but I never thought it would have such a global use).

So rather than rewritting all my error handling (where to start!) is there a way of forcing the .net application to use en-us?

Also what are the bad implications of doing this?

To expand further as this got marked as duplicate. I am starting a thread that begins with

Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-us")

This works except for when it reaches this part of the thread

Dim client As WebClient = New WebClient()
Try
client.DownloadFile(url, tempName)
Catch ex As Exception
!!Ex is still giving the string in German rather than en-us
End Try

also, under Assembly Information it shows "English (United States)" as Neutral Language.

Seems that you are trying to parse in some way the exception message, or you are trying to show it in English for the end-user, in both cases you could try this general solution to (try)convert the message exception to English culture (taken from here ):

public module ExceptionExtensions

<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Function ToEnglish(Of T As System.Exception)(ByVal ex As T) As String

    Dim oldCI As CultureInfo = Thread.CurrentThread.CurrentUICulture
    Dim exEng As System.Exception = Nothing

    Task.Factory.StartNew(Sub()
                              Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US")
                              exEng = DirectCast(Activator.CreateInstance(ex.[GetType]), System.Exception)
                              Thread.CurrentThread.CurrentUICulture = oldCI
                          End Sub,
                          TaskCreationOptions.LongRunning).Wait()

    Return exEng.Message

End Function

end module

Usage Example:

 Try
     Throw New FileNotFoundException

 Catch ex As FileNotFoundException
     Dim message As String = ex.ToEnglish()

 End Try

Also, see my question and/or the comments below, they could be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM