我正在寻找一个.Net类/库与货币缩写词一起使用,就像TimeZoneInfo类与时区一样.
我需要使用这些首字母缩写填充下拉列表并将结果存储在数据库中.此值将用于在稍后阶段从Web检索最新的汇率.
有任何想法吗?
我想将我的应用文化设置为我想要的任何东西,无论操作系统文化是什么.为了获得这个,我使用了"fa-IR"作为文化的CultureInfo类,但它使用"GregorianCalendar"作为默认日历而不是.NET PersianCalendar类.所以我试图从CultureInfo派生一个新类来实现我的客户文化:
/// <summary>
/// Represents culture specific information, Persian calendar and number format info for Iran.
/// </summary>
public class PersianCultureInfo : CultureInfo
{
private Calendar _calendar = new PersianCalendar();
public PersianCultureInfo()
: base("fa-IR", true)
{
}
/// <summary>
/// Persian calendar with solar system algorithm for Iran.
/// </summary>
public override Calendar Calendar
{
get
{
return this._calendar;
}
}
public static PersianCultureInfo Create()
{
PersianCultureInfo culture = new PersianCultureInfo();
culture.PerpareDateTimeFormatInfo();
return culture;
}
private void PerpareDateTimeFormatInfo()
{ …Run Code Online (Sandbox Code Playgroud) 我首先在堆栈溢出时遇到了类似于我的问题:循环遍历ResourceManager中的所有资源 - C#.它只解决了我需要做的部分工作.当您在特定文化的资源文件中请求条目时,如果没有存在,它将默认返回中性文化资源文件.
我需要遍历给定资源文件的每个条目,而GetResourceSet需要一个文化.例如,我有一个中性资源文件,其中包含3个条目,以及一个文化特定资源文件,其中包含1个条目的中性文件.
我的中性资源示例文件是MyResource.resx,我的特定于文化的资源示例文件是MyResource.en-gb.resx.以下代码显示了我当前正在尝试遍历并访问所有资源条目的方式.
Dim cultInfo as New CultureInfo(culture)
For Each entry As System.Collections.DictionaryEntry In myResourceManager.GetResourceSet(cultInfo, True, True)
Next
Run Code Online (Sandbox Code Playgroud)
中性资源文件条目
文化特定资源文件输入
当我为特定文化调用GetResourceSet时,我只返回1个条目.我期待(并希望)在覆盖一个特定于文化的条目时取回所有3个条目.这是我想要的回报:
无论如何我能做到这一点吗?谢谢.
如果我有国家/语言组合的文化代码,是否可以从中找到文本方向.
例如,对于en-US,我如何从中得到文本方向从左到右?从ar-EG开始,我怎么知道文本方向是从右到左?
根据这个问题 的答案,async/await调用应该保留CurrentCulture.就我而言,当我调用自己的异步方法时,将保留CurrentCulture.但是,如果我调用某种WCF服务方法,则不会保留CurrentCulture,它会被更改为看起来像服务器默认线程文化的东西.
我已经了解了调用托管线程的内容.它发生在每个代码行都在一个托管线程上执行(ManagedThreadId保持不变).在调用我自己的异步方法后,CultureInfo保持不变.但是当我调用WCF的方法时,ManagedTrheadId保持不变,但CurrentCulture已更改.
简化的代码如下所示::
private async Task Test()
{
// Befoe this code Thread.CurrentThread.CurrentCulture is "ru-RU", i.e. the default server's culture
// Here we change current thread's culture to some desired culture, e.g. hu-HU
Thread.CurrentThread.CurrentCulture = new CultureInfo("hu-HU");
var intres = await GetIntAsync();
// after with await Thread.CurrentThread.CurrentCulture is still "hu-HU"
var wcfres = await wcfClient.GetResultAsync();
// And here Thread.CurrentThread.CurrentCulture is "ru-RU", i.e. the default server's culture
}
private async Task<int> GetIntAsync()
{
return await Task.FromResult(1);
} …Run Code Online (Sandbox Code Playgroud) 我有一个使用Tasks的应用程序.我们还修改了cultureInfo(我们使用EN-US语言,但保留日期/数字格式),我们使用.Net 4.0.
应用程序有很多线程和任务,我们有一个工厂来创建任务/线程.
对于该线程,我们有以下代码,以确保使用正确的CurrentCulture启动每个线程:
//This is basically only the constructor, but it describe well how we create the Thread:
public MonitoredThread(ThreadStart threadStart, string name, bool isBackground = false)
{
m_threadStart = threadStart;
m_name = name;
m_isBackground = isBackground;
Thread = new Thread(ThreadWorker)
{
Name = name,
IsBackground = isBackground,
CurrentCulture = CustomCultureInfo.CurrentCulture,
CurrentUICulture = CustomCultureInfo.CurrentCulture
};
}
Run Code Online (Sandbox Code Playgroud)
但是对于Tasks,我不知道如何实现这种机制:
public static Task ExecuteTask(Action action, string name)
{
MonitoredTask task = new MonitoredTask(action, name);
return Task.Factory.StartNew(task.TaskWorker);
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我在Umm Al-Qura日历中有以下阿拉伯语日期,我要解析为.NET DateTime对象:
الأربعاء,17ذوالحجة,1436
此日期相当于公历中的2015年9月30日.
我一直在尝试使用以下"标准"C#代码来解析这个日期,但没有成功:
var cultureInfo = new CultureInfo("ar-SA");
cultureInfo.DateTimeFormat.Calendar = new UmAlQuraCalendar(); // the default one anyway
var dateFormat = "dddd? dd MMMM? yyyy"; //note the ? instead of ,
var dateString = "??????????? 17? ?? ??????? 1436";
DateTime date;
DateTime.TryParseExact(dateString, dateFormat, cultureInfo.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces, out date);
Run Code Online (Sandbox Code Playgroud)
无论我做什么,结果TryParseExact总是如此false.如何在.NET中正确解析此字符串?
顺便说一句,如果我从一个DateTime对象开始,我可以ToString()在DateTime没有问题的情况下使用's overloads 创建上面的确切日期字符串.我显然不能以相反的方式做到这一点.
我正在尝试本地化我的网络API的响应.
我在项目中创建了一个包含所有本地化字符串的资源文件(Messges.resx).并使用Accept-Language标头来识别用户的语言.
在响应中,我可以将字符串设置为:
response = Messages.KEY_GOOD_MORNING
Run Code Online (Sandbox Code Playgroud)
它将以当前线程文化的语言获取该名称的字符串.我将不得不将线程的当前文化更改为从Accept-Language标题中找到的文化.但我不想改变线程的文化,因为这也会改变数字/日期格式,这对我来说是个问题.
我看到的另一种选择是 - 使用ResourceManager并将文化传递给 -
Messages.ResourceManager.GetString("KEY_GOOD_MORNING", CultureInfo.CreateSpecificCulture(lang.value))
Run Code Online (Sandbox Code Playgroud)
这样我就不必改变线程的文化.但这种方法的问题是 - 字符串名称不再是类型安全的.传递给GetString()的字符串名称中的拼写错误可能导致返回空值.
我可以采取其他方法来避免上述问题吗?
是否可以让 .NET 创建以下输出?
DateTime.UtcNow.ToString() --> "2017-11-07T00:40:00.123456Z"
Run Code Online (Sandbox Code Playgroud)
当然,总有可能使用 ToString("s") 或 ToString("yyyy-MM-ddTHH:mm:ss.fffffffK")。但是有没有办法将无参数 ToString-Method 的默认行为调整为所需的输出?
我尝试更改 CurrentCulture。但我得到的最好的是“2017-11-07 00:40:00.123456Z”。我没有找到将日期和时间之间的分隔符从空格更改为“T”的方法。
执行语句时如何比较字符串switch?线程/计算机的当前文化是否会影响switch评估?我养成了在比较字符串时总是指定比较器的习惯,因此如果能够确认这一点那就太好了。
我怀疑是这样StringComparer.Ordinal,但我找不到任何相关文档。
cultureinfo ×10
c# ×9
.net ×2
datetime ×2
asp.net ×1
asp.net-mvc ×1
async-await ×1
culture ×1
date ×1
hebrew ×1
localization ×1
persian ×1
resources ×1
resx ×1
string ×1
wcf ×1