如何仅从 .NET 中的语言获取 DayNames

发布于 2024-08-26 05:36:49 字数 375 浏览 6 评论 0原文

假设我只有一个国家/地区代码(en、de、fr),并且我需要用这种语言显示工作日。 我了解 RegionInfo 和 CultureInfo - 但我找不到解决方案。 如果我从(例如)“en”创建国家/地区信息,则其中没有日期时间信息。 只取第一个匹配的区域也可以。例如,en-US 代表 en,de-DE 代表 de。

我不知道日名是否有差异,但我知道月份有一些差异。 de-DE Februar - de-AT Feber —— 无论如何我不在乎。 如果事件可能“有点不同”(看到 Februar 而不是 Feber) - 它仍然是德语。

这就是我想要实现的目标 - 获取 en 并在星期一写入 - 获取 de 并写入 Montag...

有没有一种方法可以仅从语言代码创建一个区域?

Assume that I only have a country code (en, de, fr) and I need to display the weekdays in this language.
I know about RegionInfo and CultureInfo - but I can't find a solution.
If I create a country info from (for an example) "en" I have no DateTime info in it.
It would also be OK to just take the first matching Region. For an example en-US for en or de-DE for de.

I don't know if there are differences in DayNames but I know there are some for the months.
de-DE Februar - de-AT Feber -- anyhow I don't care.
Event if it may be "a bit different" (to see Februar instead of Feber) - it is still German.

And that's what I want to achive - get en an write Monday - get de and write Montag...

Is there a way to create a region just from a language code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心奴独伤 2024-09-02 05:36:49

这篇文章可能会有所帮助:要从“en”转到 CultureInfo,更快的方法是

CultureInfo ci = CultureInfo.CreateSpecificCulture("en")

对于第二部分,我相信您正在询问日期名称,因此您会写

string[] names = ci.DateTimeFormat.DayNames 

This piece may be helpful: to go from "en" to the CultureInfo, a quicker way is

CultureInfo ci = CultureInfo.CreateSpecificCulture("en")

For the second part, I believe you are asking for day names, so you would write

string[] names = ci.DateTimeFormat.DayNames 
与酒说心事 2024-09-02 05:36:49

通常当你说“我不在乎”时,你就开始与 API 作斗争。

无论如何,这应该有效:

var list = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var ci = list.FirstOrDefault(c => c.IetfLanguageTag.StartsWith("de"));

var ri = new RegionInfo(ci.Name);
Console.WriteLine("Today = {0:dddd MMMM yyyy}", DateTime.Today);

It's usually when you say "I don't care" that you start fighting the APIs.

Anyway, this should work:

var list = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var ci = list.FirstOrDefault(c => c.IetfLanguageTag.StartsWith("de"));

var ri = new RegionInfo(ci.Name);
Console.WriteLine("Today = {0:dddd MMMM yyyy}", DateTime.Today);
带刺的爱情 2024-09-02 05:36:49

这将为您提供文化列表:

    Dim ci As CultureInfo
    For Each ci In CultureInfo.GetCultures(CultureTypes.SpecificCultures)
        Console.WriteLine(ci.Name)
    Next

输出:
ar-SA
BG-BG
钙ES
zh-TW
铯-CZ
达DK
去DE
埃尔-GR
en-US
FI-FI
fr-FR
he-IL
胡-胡
是-IS
它-IT
ja-JP
ko-KR
NL-NL
铌一NO
PL-PL
PT-BR
滚装滚装
茹-茹
小时-HR
SK-SK
方铝
sv-SE
th-TH
TR-TR
尿素PK
id-ID
英国-UA
贝-BY
sl-SI
乙醚
左室-左室
LT-LT
公平的
vi-VN
hy-AM
氮-拉丁-AZ
欧盟ES
MK-MK
af-ZA
卡GE
FO-FO
嗨输入
女士-MY
kk-KZ
ky-KG
sw-KE
乌兹-拉丁-乌兹
tt-RU
疼痛
gu-IN
ta-IN
TE-IN
kn-IN
先生-IN
萨-IN
锰-MN
糖苷酶
角印
syr-SY
DV-MV
智商
zh-CN
脱CH
en-GB
es-MX
fr-BE

氮肥
硝基一氧化氮
PT-PT
sr-Latn-CS
sv-FI
氮基-Cyr-AZ
MS-BN
乌兹-塞尔-乌兹
精氨酸EG
zh-HK
去AT
en-AU
ES-ES
fr-CA
sr-Cyr1-CS
ar-LY
zh-SG
脱鲁
en-CA
es-GT
fr-CH
ar-DZ
zh-MO
熟食店
en-NZ
es-CR
fr-LU
ar-MA
en-IE
es-PA
fr-MC
ar-TN
en-ZA
溶解氧
ar-OM
en-JM
ES-VE
ar-YE
zh-029
二氧化碳一氧化碳
ar-SY
en-BZ
es-PE
ar-JO
en-TT
增强现实
ar-LB
en-ZW
内皮细胞
ar-KW
en-PH
es-CL
ar-AE
es-UY
ar-BH
es-PY
ar-QA
es-BO
SV
es-HN
es-NI
es-PR
sma一NO
BN-BD
双半胱氨酸-BA
tg-Cyr-TJ
en-SG
en-MY
mn-Mong-CN
prs-AF
wo-SN
RW-RW
qut-GT
萨赫-RU
gsw-FR
共FR
rm-CH
米-NZ
ug-CN
br-FR
莫氏CA
氨-CL
GA-IE
ii-CN
sma-SE
sr-拉丁-BA
quz-PE
免疫球蛋白NG
GL-GL
LB-LU
巴鲁
纳索ZA
quz-BO
哟-NG
短信FI
哈-拉丁-NG
小时BA
PH值
聚苯乙烯AF
fy-NL
新NP
上午-ET
iu-Cans-CA
quz-EC
丝绸
sj-SE
洛-LA
公里-KH
cy-GB
bo-CN
硒-SE
sj-NO
如-IN
毫升-IN
硒FI
或-IN
sr-Cyr-BA
BN-IN
恩-IN
DSB-DE
tk-TM
smn-FI
α-FR
es-美国
硒一氧化氮
MT-MT
bs-拉丁语-BA
祖ZA
xh-ZA
氮酸ZA
tzm-拉丁-DZ
iu-拉丁-CA
hsb-DE

This will give you a list of cultures:

    Dim ci As CultureInfo
    For Each ci In CultureInfo.GetCultures(CultureTypes.SpecificCultures)
        Console.WriteLine(ci.Name)
    Next

OUTPUT:
ar-SA
bg-BG
ca-ES
zh-TW
cs-CZ
da-DK
de-DE
el-GR
en-US
fi-FI
fr-FR
he-IL
hu-HU
is-IS
it-IT
ja-JP
ko-KR
nl-NL
nb-NO
pl-PL
pt-BR
ro-RO
ru-RU
hr-HR
sk-SK
sq-AL
sv-SE
th-TH
tr-TR
ur-PK
id-ID
uk-UA
be-BY
sl-SI
et-EE
lv-LV
lt-LT
fa-IR
vi-VN
hy-AM
az-Latn-AZ
eu-ES
mk-MK
af-ZA
ka-GE
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.ToString("dddd", new CultureInfo("zh-TW"))

以上代码会将星期一打印为星期一(中文语言环境)。

DateTime.Today.ToString("dddd", new CultureInfo("zh-TW"))

Above code will print Monday as 星期一 (Chinise locale).

折戟 2024-09-02 05:36:49

昏暗的日子 As String()
昏暗的月份 As String()
Dim cul As New System.Globalization.CultureInfo("de-DE")
月份 = cul.DateTimeFormat.MonthNames
天= cul.DateTimeFormat.DayNames

Dim days As String()
Dim months As String()
Dim cul As New System.Globalization.CultureInfo("de-DE")
months = cul.DateTimeFormat.MonthNames
days = cul.DateTimeFormat.DayNames

勿忘心安 2024-09-02 05:36:49

试试这个:-

string Culture = "your Culture"

CultureInfo myCulture = new CultureInfo(culture);

DateTimeFormatInfo dtfi = myCulture .DateTimeFormat;

dayString = dtfi.GetDayName( 这里是您的日期.. );

查理·G

Try this:-

string culture = "your culture"

CultureInfo myCulture = new CultureInfo(culture);

DateTimeFormatInfo dtfi = myCulture .DateTimeFormat;

dayString = dtfi.GetDayName( your date here.. );

Charlie G

一腔孤↑勇 2024-09-02 05:36:49

由于“de”是 NeutralCulture,因此如果您尝试 CultureInfo.GetCultureInfo(“de”),则无法从 DateTimeFormat 中获取 DayNames。

我建议您在这种情况下运行 CultureInfo.CreateSpecificCulture("de"),因为它将创建一种非中性文化...在“de”的情况下,我从.NET 诸神:D

    CultureInfo tmp = CultureInfo.CreateSpecificCulture("de");

    string[] names = tmp.DateTimeFormat.DayNames;
    foreach(string name in names)
    {
        Console.WriteLine(name);
    }

As "de" is a NeutralCulture it won't be possible to get the DayNames out of the DateTimeFormat if you try CultureInfo.GetCultureInfo("de").

I would suggest that you run the CultureInfo.CreateSpecificCulture("de") in this case since it'll create a non-neutral culture...in case of "de" I got a culture of "de-DE" back from the .NET gods :D

    CultureInfo tmp = CultureInfo.CreateSpecificCulture("de");

    string[] names = tmp.DateTimeFormat.DayNames;
    foreach(string name in names)
    {
        Console.WriteLine(name);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文