Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 0c3e7b5

Browse files
committed
Improve usability of IHtmlLocalizer & associated API:
- IHtmlLocalizer no longer derives from IStringLocalizer - IHtmlLocalizer indexer now returns LocalizedHtmlString - IHtmlLocalizer has GetString methods now that act the same as IStringLocalizer.GetString - Made LocalizedHtmlString a struct to match LocalizedString - Updated samples in response to aspnet/Localization#167 - Rename "ancestor" to "parent" for loc API - Fixes some doc comments - Fixed tests - #3716
1 parent 0720d23 commit 0c3e7b5

16 files changed

+263
-250
lines changed

samples/LocalizationSample.Web/Views/Home/Locpage.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
}
55
@LocString["Hello there!!"]
66
@ViewBag.Message
7-
@LocString.Html("Hi", "John", @date , @date.DayOfWeek)
7+
@LocString.GetHtml("Hi", "John", @date , @date.DayOfWeek)

src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs

Lines changed: 25 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
namespace Microsoft.AspNet.Mvc.Localization
1010
{
1111
/// <summary>
12-
/// An <see cref="IHtmlLocalizer"/> that uses the <see cref="IStringLocalizer"/> to provide localized HTML content.
13-
/// This service just encodes the arguments but not the resource string.
12+
/// An <see cref="IHtmlLocalizer"/> that uses the provided <see cref="IStringLocalizer"/> to do HTML-aware
13+
/// localization of content.
1414
/// </summary>
1515
public class HtmlLocalizer : IHtmlLocalizer
1616
{
17-
private IStringLocalizer _localizer;
17+
private readonly IStringLocalizer _localizer;
1818

1919
/// <summary>
2020
/// Creates a new <see cref="HtmlLocalizer"/>.
@@ -31,109 +31,68 @@ public HtmlLocalizer(IStringLocalizer localizer)
3131
}
3232

3333
/// <inheritdoc />
34-
public virtual LocalizedString this[string key]
34+
public virtual LocalizedHtmlString this[string name]
3535
{
3636
get
3737
{
38-
if (key == null)
38+
if (name == null)
3939
{
40-
throw new ArgumentNullException(nameof(key));
40+
throw new ArgumentNullException(nameof(name));
4141
}
4242

43-
return _localizer[key];
43+
return ToHtmlString(_localizer[name]);
4444
}
4545
}
4646

4747
/// <inheritdoc />
48-
public virtual LocalizedString this[string key, params object[] arguments]
48+
public virtual LocalizedHtmlString this[string name, params object[] arguments]
4949
{
5050
get
5151
{
52-
if (key == null)
52+
if (name == null)
5353
{
54-
throw new ArgumentNullException(nameof(key));
54+
throw new ArgumentNullException(nameof(name));
5555
}
5656

57-
return _localizer[key, arguments];
57+
return ToHtmlString(_localizer[name], arguments);
5858
}
5959
}
6060

61-
/// <summary>
62-
/// Creates a new <see cref="IHtmlLocalizer"/> for a specific <see cref="CultureInfo"/>.
63-
/// </summary>
64-
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
65-
/// <returns>A culture-specific <see cref="IHtmlLocalizer"/>.</returns>
66-
public virtual IHtmlLocalizer WithCulture(CultureInfo culture)
67-
{
68-
if (culture == null)
69-
{
70-
throw new ArgumentNullException(nameof(culture));
71-
}
72-
73-
return new HtmlLocalizer(_localizer.WithCulture(culture));
74-
}
75-
76-
/// <summary>
77-
/// Creates a new <see cref="IStringLocalizer"/> for a specific <see cref="CultureInfo"/>.
78-
/// </summary>
79-
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
80-
/// <returns>A culture-specific <see cref="IStringLocalizer"/>.</returns>
81-
IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture)
82-
{
83-
if (culture == null)
84-
{
85-
throw new ArgumentNullException(nameof(culture));
86-
}
87-
88-
return new HtmlLocalizer(_localizer.WithCulture(culture));
89-
}
90-
9161
/// <inheritdoc />
92-
public virtual LocalizedString GetString(string key)
62+
public virtual LocalizedString GetString(string name)
9363
{
94-
if (key == null)
64+
if (name == null)
9565
{
96-
throw new ArgumentNullException(nameof(key));
66+
throw new ArgumentNullException(nameof(name));
9767
}
9868

99-
return _localizer.GetString(key);
69+
return _localizer[name];
10070
}
10171

10272
/// <inheritdoc />
103-
public virtual LocalizedString GetString(string key, params object[] arguments)
73+
public virtual LocalizedString GetString(string name, params object[] arguments)
10474
{
105-
if (key == null)
75+
if (name == null)
10676
{
107-
throw new ArgumentNullException(nameof(key));
77+
throw new ArgumentNullException(nameof(name));
10878
}
10979

110-
return _localizer.GetString(key, arguments);
80+
return _localizer[name, arguments];
11181
}
11282

11383
/// <inheritdoc />
114-
public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
115-
_localizer.GetAllStrings(includeAncestorCultures);
84+
public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) =>
85+
_localizer.GetAllStrings(includeParentCultures);
11686

11787
/// <inheritdoc />
118-
public virtual LocalizedHtmlString Html(string key)
119-
{
120-
if (key == null)
121-
{
122-
throw new ArgumentNullException(nameof(key));
123-
}
124-
125-
return ToHtmlString(_localizer.GetString(key));
126-
}
127-
128-
/// <inheritdoc />
129-
public virtual LocalizedHtmlString Html(string key, params object[] arguments)
88+
public virtual IHtmlLocalizer WithCulture(CultureInfo culture)
13089
{
131-
if (key == null)
90+
if (culture == null)
13291
{
133-
throw new ArgumentNullException(nameof(key));
92+
throw new ArgumentNullException(nameof(culture));
13493
}
13594

136-
return ToHtmlString(_localizer.GetString(key), arguments);
95+
return new HtmlLocalizer(_localizer.WithCulture(culture));
13796
}
13897

13998
/// <summary>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Microsoft.Extensions.Localization;
7+
8+
namespace Microsoft.AspNet.Mvc.Localization
9+
{
10+
/// <summary>
11+
/// Extension methods for <see cref="IHtmlLocalizer"/>.
12+
/// </summary>
13+
public static class HtmlLocalizerExtensions
14+
{
15+
/// <summary>
16+
/// Gets the <see cref="LocalizedHtmlString"/> resource for a specific name.
17+
/// </summary>
18+
/// <param name="key">The key to use.</param>
19+
/// <returns>The <see cref="LocalizedHtmlString"/> resource.</returns>
20+
public static LocalizedHtmlString GetHtml(this IHtmlLocalizer htmlLocalizer, string name)
21+
{
22+
if (htmlLocalizer == null)
23+
{
24+
throw new ArgumentNullException(nameof(htmlLocalizer));
25+
}
26+
27+
if (name == null)
28+
{
29+
throw new ArgumentNullException(nameof(name));
30+
}
31+
32+
return htmlLocalizer[name];
33+
}
34+
35+
/// <summary>
36+
/// Gets the <see cref="LocalizedHtmlString"/> resource for a specific name.
37+
/// </summary>
38+
/// <param name="key">The key to use.</param>
39+
/// <param name="arguments">The values to format the string with.</param>
40+
/// <returns>The <see cref="LocalizedHtmlString"/> resource.</returns>
41+
public static LocalizedHtmlString GetHtml(this IHtmlLocalizer htmlLocalizer, string name, params object[] arguments)
42+
{
43+
if (htmlLocalizer == null)
44+
{
45+
throw new ArgumentNullException(nameof(htmlLocalizer));
46+
}
47+
48+
if (name == null)
49+
{
50+
throw new ArgumentNullException(nameof(name));
51+
}
52+
53+
return htmlLocalizer[name, arguments];
54+
}
55+
56+
/// <summary>
57+
/// Gets all string resources including those for parent cultures.
58+
/// </summary>
59+
/// <param name="htmlLocalizer">The <see cref="IHtmlLocalizer"/>.</param>
60+
/// <returns>The string resources.</returns>
61+
public static IEnumerable<LocalizedString> GetAllStrings(this IHtmlLocalizer htmlLocalizer)
62+
{
63+
if (htmlLocalizer == null)
64+
{
65+
throw new ArgumentNullException(nameof(htmlLocalizer));
66+
}
67+
68+
return htmlLocalizer.GetAllStrings(includeParentCultures: true);
69+
}
70+
}
71+
}

src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
namespace Microsoft.AspNet.Mvc.Localization
88
{
99
/// <summary>
10-
/// An <see cref="IHtmlLocalizerFactory"/> that creates instances of <see cref="HtmlLocalizer"/>.
10+
/// An <see cref="IHtmlLocalizerFactory"/> that creates instances of <see cref="HtmlLocalizer"/> using the
11+
/// registered <see cref="IStringLocalizerFactory"/>.
1112
/// </summary>
1213
public class HtmlLocalizerFactory : IHtmlLocalizerFactory
1314
{
1415
private readonly IStringLocalizerFactory _factory;
1516

1617
/// <summary>
17-
/// Creates a new <see cref="HtmlLocalizer"/>.
18+
/// Creates a new <see cref="HtmlLocalizerFactory"/>.
1819
/// </summary>
1920
/// <param name="localizerFactory">The <see cref="IStringLocalizerFactory"/>.</param>
2021
public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory)
@@ -28,10 +29,9 @@ public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory)
2829
}
2930

3031
/// <summary>
31-
/// Creates an <see cref="HtmlLocalizer"/> using the <see cref="System.Reflection.Assembly"/> and
32-
/// <see cref="Type.FullName"/> of the specified <see cref="Type"/>.
32+
/// Creates an <see cref="HtmlLocalizer"/> using the specified <see cref="Type"/>.
3333
/// </summary>
34-
/// <param name="resourceSource">The <see cref="Type"/>.</param>
34+
/// <param name="resourceSource">The <see cref="Type"/> to load resources for.</param>
3535
/// <returns>The <see cref="HtmlLocalizer"/>.</returns>
3636
public virtual IHtmlLocalizer Create(Type resourceSource)
3737
{
@@ -44,7 +44,7 @@ public virtual IHtmlLocalizer Create(Type resourceSource)
4444
}
4545

4646
/// <summary>
47-
/// Creates an <see cref="HtmlLocalizer"/>.
47+
/// Creates an <see cref="HtmlLocalizer"/> using the specified base name and location.
4848
/// </summary>
4949
/// <param name="baseName">The base name of the resource to load strings from.</param>
5050
/// <param name="location">The location to load resources from.</param>

0 commit comments

Comments
 (0)