Files
dotnet-bot adf899c629 Adding System.Data..., System.ServiceModel..., and System.Web...
Adding reference source for
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Entity.Design
System.Data.Linq
System.Data.SqlXml
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Activities
System.ServiceModel.Channels
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.WasHosting
System.ServiceModel.Web
System.Web
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Entity
System.Web.Entity.Design
System.Web.Extensions
System.Web.Mobile
System.Web.Routing
System.Web.Services
System.Xml
System.Xml.Linq
2016-05-03 00:07:37 +02:00

68 lines
2.9 KiB
C#

//------------------------------------------------------------------------------
// <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;
}
}
}