File: CultureUtil.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (67 lines) | stat: -rw-r--r-- 2,941 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
        }

    }
}