forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCultureUtil.cs
More file actions
76 lines (69 loc) · 2.73 KB
/
Copy pathCultureUtil.cs
File metadata and controls
76 lines (69 loc) · 2.73 KB
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
68
69
70
71
72
73
74
75
76
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
namespace System.Web.WebPages
{
internal static class CultureUtil
{
internal static void SetCulture(Thread thread, HttpContextBase context, string cultureName)
{
Debug.Assert(!String.IsNullOrEmpty(cultureName));
CultureInfo cultureInfo = GetCulture(context, cultureName);
if (cultureInfo != null)
{
thread.CurrentCulture = cultureInfo;
}
}
internal static void SetUICulture(Thread thread, HttpContextBase context, string cultureName)
{
Debug.Assert(!String.IsNullOrEmpty(cultureName));
CultureInfo cultureInfo = GetCulture(context, cultureName);
if (cultureInfo != null)
{
thread.CurrentUICulture = cultureInfo;
}
}
private static CultureInfo GetCulture(HttpContextBase context, string cultureName)
{
if (cultureName.Equals("auto", StringComparison.OrdinalIgnoreCase))
{
return DetermineAutoCulture(context);
}
else
{
return CultureInfo.GetCultureInfo(cultureName);
}
}
private static CultureInfo DetermineAutoCulture(HttpContextBase context)
{
HttpRequestBase request = context.Request;
Debug.Assert(request != null); //This call is made from a WebPageExecutingBase. Request can never be null when inside a page.
CultureInfo culture = null;
if (request.UserLanguages != null)
{
string userLanguageEntry = request.UserLanguages.FirstOrDefault();
if (!String.IsNullOrWhiteSpace(userLanguageEntry))
{
// Check if user language has q parameter. E.g. something like this: "as-IN;q=0.3"
int index = userLanguageEntry.IndexOf(';');
if (index != -1)
{
userLanguageEntry = userLanguageEntry.Substring(0, index);
}
try
{
culture = new CultureInfo(userLanguageEntry);
}
catch (CultureNotFoundException)
{
// There is no easy way to ask if a given culture is invalid so we have to handle exception.
}
}
}
return culture;
}
}
}