我有一个 .NET 应用程序 (VB.NET),它运行良好,直到德国人尝试了它。
我可以看到问题是我使用“try catch exception”捕获异常并解析豁免字符串。
当应用程序是英文的但在任何其他语言中都失败时,这工作正常(很明显,但我从没想过它会在全局范围内使用)。
因此,与其重写我所有的错误处理(从哪里开始!),有没有办法强制 .net 应用程序使用 en-us?
这样做的坏处是什么?
进一步扩展,因为它被标记为重复。 我正在开始一个以
开头的线程Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-us")
除了当它到达线程的这一部分时,这是有效的
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
此外,在装配信息下,它显示“英语(美国)”作为中性语言。
最佳答案
似乎您正在尝试以某种方式解析异常消息,或者您正在尝试以英语向最终用户显示它,在这两种情况下,您都可以尝试这种通用解决方案来(尝试)将消息异常转换为英国文化(取自 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
使用示例:
Try
Throw New FileNotFoundException
Catch ex As FileNotFoundException
Dim message As String = ex.ToEnglish()
End Try
另请参见 my question 和/或下面的评论,它们可能会有用。
关于c# - 如何强制 .NET 应用程序的语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270053/