Skip to content

Commit 0f8b90f

Browse files
committed
Expand Locale parser to support three-character language codes
1 parent fad3153 commit 0f8b90f

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/NuGet.Core/NuGet.Packaging/ContentModel/ManagedCodeConventions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ private static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable tabl
171171
}
172172
}
173173

174+
// We use a heuristic here for common locale codes. Locale codes are often
175+
// * two characters for the language: en, es, fr, de
174176
if (name.Length == 2)
175177
{
176178
if (matchOnly)
@@ -179,14 +181,25 @@ private static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable tabl
179181
}
180182
return name.ToString();
181183
}
182-
else if (name.Length >= 4 && name.Span[2] == '-')
184+
185+
// * a language portion that is two or three characters followed by a '-' and a country code
186+
else if (name.Length >= 4 && name.Span[2] == '-') // e.g. en-US
183187
{
184188
if (matchOnly)
185189
{
186190
return string.Empty;
187191
}
188192
return name.ToString();
189193
}
194+
else if (name.Length >= 5 && name.Span[3] == '-') // e.g agq-CM
195+
{
196+
return name;
197+
}
198+
199+
// there are other variations, but this heuristic doesn't cover them all. A future-proof implementation would make
200+
// use of the .NET CultureInfo APIs to compare the locale against the underlying system ICU database. This would
201+
// be correct, but potentially more expensive because the CultureInfo APIs are lazily-loaded and throw if an
202+
// invalid/unknown locale is used.
190203

191204
return null;
192205
}

0 commit comments

Comments
 (0)