Imported Upstream version 4.6.0.125

Former-commit-id: a2155e9bd80020e49e72e86c44da02a8ac0e57a4
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2016-08-03 10:59:49 +00:00
parent a569aebcfd
commit e79aa3c0ed
17047 changed files with 3137615 additions and 392334 deletions

View File

@@ -0,0 +1,67 @@
//------------------------------------------------------------------------------
// <copyright file="CultureUtil.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Util {
using System;
using System.Globalization;
// This class contains helper methods for obtaining a CultureInfo instance from a list of candidates.
internal static class CultureUtil {
// Given a single culture name, attempts to turn it into a CultureInfo.
// If 'requireSpecific' is set, this method attempts to return an object where IsNeutral = false.
public static CultureInfo CreateReadOnlyCulture(string cultureName, bool requireSpecific) {
if (requireSpecific) {
return HttpServerUtility.CreateReadOnlySpecificCultureInfo(cultureName);
}
else {
return HttpServerUtility.CreateReadOnlyCultureInfo(cultureName);
}
}
// Given a list of culture names, loop through them until we find one we understand.
// We expect 'cultureNames' to be the raw Accept-Languages header value, as we'll strip q-values.
// Otherwise equivalent to the single-element overload.
public static CultureInfo CreateReadOnlyCulture(string[] cultureNames, bool requireSpecific) {
return ExtractCultureImpl(cultureNames, requireSpecific, AppSettings.MaxAcceptLanguageFallbackCount);
}
// for unit testing, uses 'maxCount' instead of the <appSettings> switch
internal static CultureInfo ExtractCultureImpl(string[] cultureNames, bool requireSpecific, int maxCount) {
int lastIndex = Math.Min(cultureNames.Length, maxCount) - 1;
for (int i = 0; i < cultureNames.Length; i++) {
string candidate = StripQValue(cultureNames[i]);
try {
return CreateReadOnlyCulture(candidate, requireSpecific);
}
catch (CultureNotFoundException) {
// If this is the last iteration before giving up, let the exception propagate upward.
// Otherwise just ---- and move on to the next candidate.
if (i == lastIndex) {
throw;
}
}
}
return null;
}
// Given an input "foo;q=xx", returns "foo".
private static string StripQValue(string input) {
if (input != null) {
int indexOfSemicolon = input.IndexOf(';');
if (indexOfSemicolon >= 0) {
return input.Substring(0, indexOfSemicolon);
}
}
return input;
}
}
}