-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
LanguageViewLocationExpander.cs
115 lines (99 loc) · 3.92 KB
/
LanguageViewLocationExpander.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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;
using System.Collections.Generic;
using System.Globalization;
namespace Microsoft.AspNetCore.Mvc.Razor
{
/// <summary>
/// A <see cref="IViewLocationExpander"/> that adds the language as an extension prefix to view names. Language
/// that is getting added as extension prefix comes from <see cref="Microsoft.AspNetCore.Http.HttpContext"/>.
/// </summary>
/// <example>
/// For the default case with no areas, views are generated with the following patterns (assuming controller is
/// "Home", action is "Index" and language is "en")
/// Views/Home/en/Action
/// Views/Home/Action
/// Views/Shared/en/Action
/// Views/Shared/Action
/// </example>
public class LanguageViewLocationExpander : IViewLocationExpander
{
private const string ValueKey = "language";
private readonly LanguageViewLocationExpanderFormat _format;
/// <summary>
/// Instantiates a new <see cref="LanguageViewLocationExpander"/> instance.
/// </summary>
public LanguageViewLocationExpander()
: this(LanguageViewLocationExpanderFormat.Suffix)
{
}
/// <summary>
/// Instantiates a new <see cref="LanguageViewLocationExpander"/> instance.
/// </summary>
/// <param name="format">The <see cref="LanguageViewLocationExpanderFormat"/>.</param>
public LanguageViewLocationExpander(LanguageViewLocationExpanderFormat format)
{
_format = format;
}
/// <inheritdoc />
public void PopulateValues(ViewLocationExpanderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
// Using CurrentUICulture so it loads the locale specific resources for the views.
context.Values[ValueKey] = CultureInfo.CurrentUICulture.Name;
}
/// <inheritdoc />
public virtual IEnumerable<string> ExpandViewLocations(
ViewLocationExpanderContext context,
IEnumerable<string> viewLocations)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (viewLocations == null)
{
throw new ArgumentNullException(nameof(viewLocations));
}
context.Values.TryGetValue(ValueKey, out var value);
if (!string.IsNullOrEmpty(value))
{
CultureInfo culture;
try
{
culture = new CultureInfo(value);
}
catch (CultureNotFoundException)
{
return viewLocations;
}
return ExpandViewLocationsCore(viewLocations, culture);
}
return viewLocations;
}
private IEnumerable<string> ExpandViewLocationsCore(IEnumerable<string> viewLocations, CultureInfo cultureInfo)
{
foreach (var location in viewLocations)
{
var temporaryCultureInfo = cultureInfo;
while (temporaryCultureInfo != temporaryCultureInfo.Parent)
{
if (_format == LanguageViewLocationExpanderFormat.SubFolder)
{
yield return location.Replace("{0}", temporaryCultureInfo.Name + "/{0}");
}
else
{
yield return location.Replace("{0}", "{0}." + temporaryCultureInfo.Name);
}
temporaryCultureInfo = temporaryCultureInfo.Parent;
}
yield return location;
}
}
}
}