提问人:PeterCo 提问时间:1/4/2021 更新时间:1/4/2021 访问量:1273
在不了解文化的情况下将月份名称从一种语言转换为另一种语言?
Convert month name from one language to another without knowing the Culture?
问:
如何在不知道“agosto”是西班牙语还是意大利语的情况下将月份名称为“agosto”的字符串转换为英文翻译“August”?
我知道我得到了西班牙语月份数字 8 的月份名称
Dim SpanishMonthName as String = Globalization.CultureInfo.GetCultureInfo("ES").DateTimeFormat.GetMonthName(8)
但是我怎样才能得到字符串“八月”(英文的第 8 个月名称)作为西班牙语或意大利语月份名称“agosto”的翻译?
答:
在各种内部有月份名称(请参阅),因此您可以:CultureInfo
someCulture.DateTimeFormat.MonthNames
var italian = CultureInfo.GetCultureInfo("it-IT");
var spanish = CultureInfo.GetCultureInfo("es-ES");
var english = CultureInfo.GetCultureInfo("en-US");
string month = "agosto";
var italianMonthNames = italian.DateTimeFormat.MonthNames;
var spanishMonthNames = spanish.DateTimeFormat.MonthNames;
int ix = Array.FindIndex(italianMonthNames, x => StringComparer.OrdinalIgnoreCase.Equals(x, month));
if (ix == -1)
{
ix = Array.FindIndex(spanishMonthNames, x => StringComparer.OrdinalIgnoreCase.Equals(x, month));
}
// ix is 0 based, while months are 1 based
string englishMonth = ix != -1 ? english.DateTimeFormat.GetMonthName(ix + 1) : null;
您甚至可以尝试将一些委托给 .NET :DateTime.ParseExact
var italian = CultureInfo.GetCultureInfo("it-IT");
var spanish = CultureInfo.GetCultureInfo("es-ES");
var english = CultureInfo.GetCultureInfo("en-US");
string month = "agosto";
string englishMonth = null;
DateTime dt;
if (DateTime.TryParseExact(month, "MMMM", italian, 0, out dt) || DateTime.TryParseExact(month, "MMMM", spanish, 0, out dt))
{
englishMonth = dt.ToString("MMMM", english);
}
一般来说,至少有一个月份在两种语言中具有不同的含义:listopad(10月或11月,见这里)。完整名单是 Hlakola、listopad、Mopitlo、Nhlangula、Nyakanga、Phupu
使用 a 收集月份名称的第一个版本:Dictionary<>
public class MonthNameFinder
{
private readonly IReadOnlyDictionary<string, int> MonthToNumber;
public MonthNameFinder(params string[] cultures)
{
MonthToNumber = BuildDictionary(cultures.Select(x => CultureInfo.GetCultureInfo(x)));
}
public MonthNameFinder(params CultureInfo[] cultureInfos)
{
MonthToNumber = BuildDictionary(cultureInfos);
}
public MonthNameFinder(CultureTypes cultureTypes = CultureTypes.AllCultures)
{
MonthToNumber = BuildDictionary(CultureInfo.GetCultures(cultureTypes));
}
private static IReadOnlyDictionary<string, int> BuildDictionary(IEnumerable<CultureInfo> cultureInfos)
{
// Note that the comparer will always be wrong, sadly. Each culture has its comparer
var dict = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
foreach (var culture in cultureInfos)
{
var monthNames = culture.DateTimeFormat.MonthNames;
for (int i = 0; i < monthNames.Length; i++)
{
string monthName = monthNames[i];
int other;
if (!dict.TryGetValue(monthName, out other))
{
dict[monthName] = i + 1;
}
else if (other != i + 1)
{
Debug.WriteLine($"Repeated month {monthName}: {i + 1} in {culture.Name} ({culture.DisplayName})");
}
}
}
return dict;
}
public int? GetMonthNumber(string monthName)
{
int monthNumber;
if (MonthToNumber.TryGetValue(monthName, out monthNumber))
{
return monthNumber;
}
return null;
}
}
像这样使用它:
var mnf = new MonthNameFinder();
int? n = mnf.GetMonthNumber("agosto");
if (n != null)
{
string name = new DateTime(1, n.Value, 1).ToString("MMMM", CultureInfo.GetCultureInfo("en-US"));
}
(请注意,您应该缓存...建造起来可能相当昂贵)mnf
嗯......我不喜欢。。。我有点OC...我知道以月份的名义存在一些碰撞的简单事实困扰着我。
这是第二个版本,使用 a 甚至保存 ,以便可以发现月份名称的语言。现在返回一个 ,一个匿名值类型的数组。你可以明明拿第一个,过上幸福的生活,也可以查一下,看看有没有多个不同的s。ILookup<>
CultureName
GetMonthNumbers(monthName)
(int MonthNumber, string CultureName)[]
MonthNumber
public class MonthNameFinder
{
private readonly ILookup<string, (int MonthNumber, string CultureName)> MonthToNumber;
public MonthNameFinder(params string[] cultures)
{
MonthToNumber = BuildLookup(cultures.Select(x => CultureInfo.GetCultureInfo(x)));
}
public MonthNameFinder(params CultureInfo[] cultureInfos)
{
MonthToNumber = BuildLookup(cultureInfos);
}
public MonthNameFinder(CultureTypes cultureTypes = CultureTypes.AllCultures)
{
MonthToNumber = BuildLookup(CultureInfo.GetCultures(cultureTypes));
}
private static ILookup<string, (int MonthNumber, string CultureName)> BuildLookup(IEnumerable<CultureInfo> cultureInfos)
{
// Note that the comparer will always be wrong, sadly. Each culture has its comparer
var lst = new List<(string Name, int MonthNumber, string CultureName)>();
foreach (var culture in cultureInfos)
{
var monthNames = culture.DateTimeFormat.MonthNames;
for (int i = 0; i < monthNames.Length; i++)
{
string monthName = monthNames[i];
lst.Add((monthName, i + 1, culture.Name));
}
}
return lst.OrderBy(x => x.Name)
.ThenBy(x => x.MonthNumber)
.ToLookup(x => x.Name, x => (x.MonthNumber, x.CultureName), StringComparer.InvariantCultureIgnoreCase);
}
public (int MonthNumber, string CultureName)[] GetMonthNumbers(string monthName)
{
return MonthToNumber[monthName].ToArray();
}
}
像这样使用它:
// This is an array of (MonthNumber, CultureName)
var mnf = new MonthNameFinder();
var numbers = mnf.GetMonthNumbers("agosto");
if (numbers.Length != 0)
{
string monthName = new DateTime(1, numbers[0].MonthNumber, 1).ToString("MMMM", CultureInfo.GetCultureInfo("en-US"));
}
(即使在这里,你也应该缓存......建造起来可能相当昂贵)mnf
请注意,有许多相似的文化,所以会相当大(例如,仅对于意大利语,就有 5 种文化,搜索 agosto 返回 52 种不同的文化,其中包含 agosto 月份。numbers
评论
Array.FindIndex
Array.Find()
Array.IndexOf
Array.Find
Array.IndexOf
Array.FindIndex
评论
var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);