This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ViewLocalizer.cs
130 lines (108 loc) · 4.43 KB
/
ViewLocalizer.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Localization;
namespace Microsoft.AspNetCore.Mvc.Localization
{
/// <summary>
/// An <see cref="IViewLocalizer"/> implementation that derives the resource location from the executing view's
/// file path.
/// </summary>
public class ViewLocalizer : IViewLocalizer, IViewContextAware
{
private readonly IHtmlLocalizerFactory _localizerFactory;
private readonly string _applicationName;
private IHtmlLocalizer _localizer;
/// <summary>
/// Creates a new <see cref="ViewLocalizer"/>.
/// </summary>
/// <param name="localizerFactory">The <see cref="IHtmlLocalizerFactory"/>.</param>
/// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param>
public ViewLocalizer(IHtmlLocalizerFactory localizerFactory, IHostingEnvironment hostingEnvironment)
{
if (localizerFactory == null)
{
throw new ArgumentNullException(nameof(localizerFactory));
}
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
_applicationName = hostingEnvironment.ApplicationName;
_localizerFactory = localizerFactory;
}
/// <inheritdoc />
public virtual LocalizedHtmlString this[string key]
{
get
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer[key];
}
}
/// <inheritdoc />
public virtual LocalizedHtmlString this[string key, params object[] arguments]
{
get
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer[key, arguments];
}
}
/// <inheritdoc />
public LocalizedString GetString(string name) => _localizer.GetString(name);
/// <inheritdoc />
public LocalizedString GetString(string name, params object[] values) => _localizer.GetString(name, values);
/// <inheritdoc />
public IHtmlLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
/// <inheritdoc />
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) =>
_localizer.GetAllStrings(includeParentCultures);
/// <summary>
/// Apply the specified <see cref="ViewContext"/>.
/// </summary>
/// <param name="viewContext">The <see cref="ViewContext"/>.</param>
public void Contextualize(ViewContext viewContext)
{
if (viewContext == null)
{
throw new ArgumentNullException(nameof(viewContext));
}
// Given a view path "/Views/Home/Index.cshtml" we want a baseName like "MyApplication.Views.Home.Index"
var path = viewContext.ExecutingFilePath;
if (string.IsNullOrEmpty(path))
{
path = viewContext.View.Path;
}
Debug.Assert(!string.IsNullOrEmpty(path), "Couldn't determine a path for the view");
_localizer = _localizerFactory.Create(BuildBaseName(path), _applicationName);
}
private string BuildBaseName(string path)
{
var extension = Path.GetExtension(path);
var startIndex = path[0] == '/' || path[0] == '\\' ? 1 : 0;
var length = path.Length - startIndex - extension.Length;
var capacity = length + _applicationName.Length + 1;
var builder = new StringBuilder(path, startIndex, length, capacity);
builder.Replace('/', '.').Replace('\\', '.');
// Prepend the application name
builder.Insert(0, '.');
builder.Insert(0, _applicationName);
return builder.ToString();
}
}
}