您如何处理本地化/CultureInfo [英] How do you handle localization / CultureInfo
问题描述
某些方法,例如string.Format()或.Parse()需要IFormatProvider.您如何提供它?
Some methods like string.Format() or .Parse() require an IFormatProvider. How do you provide it?
-
在封闭环境的应用程序中(您知道永远不需要本地化),您是否只是跳过它并在没有IFormatProvider的情况下调用方法?
In closed environment application (where you know that localization will never be required), do you just skip it and call the methods without the IFormatProvider?
在可能已本地化的应用程序中,您是否为每个方法调用都考虑了正确的值并将其设置在正确的位置?可能是"CultureInfo.CurrentCulture"或"CultureInfo.CurrentUiCulture".
In applications that might get localized, do you think about the correct value for every method call and just set it right there? That would be probably 'CultureInfo.CurrentCulture' or 'CultureInfo.CurrentUiCulture'.
还是您使用诸如"MyUiCultureInfo"和"MyCultureInfo"之类的全局变量来通过更改其值来切换本地化?您如何以及在何处存储这些变量?
Or do you use global variables like 'MyUiCultureInfo' and 'MyCultureInfo' to be able to switch the localization by changing their values? How and where do you store these variables?
在开发库或框架时有什么要考虑的问题-在这种情况下如何处理本地化?
Is there anything to consider when I develop libraries or frameworks - how to handle localization in this case?
推荐答案
在我的(ASP.NET)应用程序中,我总是将CurrentThread.Current(Ui)Culture设置为正确的值.通常会在每次请求开始时根据用户的首选项或配置文件中存储的默认值(如果用户尚未定义首选项)完成此操作.
I'm always setting CurrentThread.Current(Ui)Culture to the correct value in our (ASP.NET) applications. This is usually done at the begin of each request, based on the user's preferences or a default value stored in a config file (if the user has not defined a preference).
设置当前线程的这些属性后,您可以停止考虑它-数字,日期等将被正确格式化/解析,即使没有为这些方法提供IFormatProvider.否则,您必须确保将正确的IFormatProvider传递到任何地方.
After setting these properties of the current thread, you can stop thinking about it - numbers, dates, etc. will be correctly formatted/parsed, even when no IFormatProvider is provided to these methods. Otherwise, you have to ensure that the correct IFormatProvider is passed everywhere.
就图书馆而言,我认为它应该仅依赖于应用程序来处理这些事情,而不必担心这些事情.
I the case of a library, I think it should just rely on the application for these things and should not have to worry about these things.
这篇关于您如何处理本地化/CultureInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!