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
/
LocalizedHtmlString.cs
101 lines (87 loc) · 3.44 KB
/
LocalizedHtmlString.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
// 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.IO;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Html;
namespace Microsoft.AspNetCore.Mvc.Localization
{
/// <summary>
/// An <see cref="IHtmlContent"/> with localized content.
/// </summary>
public class LocalizedHtmlString : IHtmlContent
{
private readonly object[] _arguments;
/// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <param name="value">The string resource.</param>
public LocalizedHtmlString(string name, string value)
: this(name, value, isResourceNotFound: false, arguments: Array.Empty<object>())
{
}
/// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <param name="value">The string resource.</param>
/// <param name="isResourceNotFound">A flag that indicates if the resource is not found.</param>
public LocalizedHtmlString(string name, string value, bool isResourceNotFound)
: this(name, value, isResourceNotFound, arguments: Array.Empty<object>())
{
}
/// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <param name="value">The string resource.</param>
/// <param name="isResourceNotFound">A flag that indicates if the resource is not found.</param>
/// <param name="arguments">The values to format the <paramref name="value"/> with.</param>
public LocalizedHtmlString(string name, string value, bool isResourceNotFound, params object[] arguments)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (arguments == null)
{
throw new ArgumentNullException(nameof(arguments));
}
Name = name;
Value = value;
IsResourceNotFound = isResourceNotFound;
_arguments = arguments;
}
/// <summary>
/// The name of the string resource.
/// </summary>
public string Name { get; }
/// <summary>
/// The string resource.
/// </summary>
public string Value { get; }
/// <summary>
/// Gets a flag that indicates if the resource is not found.
/// </summary>
public bool IsResourceNotFound { get; }
/// <inheritdoc />
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}
var formattableString = new HtmlFormattableString(Value, _arguments);
formattableString.WriteTo(writer, encoder);
}
}
}