Skip to content

Commit cd7daf6

Browse files
author
Nate McMaster
committed
Merge source code from aspnet/Localization
\n\nCommit migrated from dotnet/extensions@9c7e86f
2 parents d3a479c + e5fee28 commit cd7daf6

30 files changed

+3385
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.Collections.Generic;
5+
using System.Globalization;
6+
7+
namespace Microsoft.Extensions.Localization
8+
{
9+
/// <summary>
10+
/// Represents a service that provides localized strings.
11+
/// </summary>
12+
public interface IStringLocalizer
13+
{
14+
/// <summary>
15+
/// Gets the string resource with the given name.
16+
/// </summary>
17+
/// <param name="name">The name of the string resource.</param>
18+
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
19+
LocalizedString this[string name] { get; }
20+
21+
/// <summary>
22+
/// Gets the string resource with the given name and formatted with the supplied arguments.
23+
/// </summary>
24+
/// <param name="name">The name of the string resource.</param>
25+
/// <param name="arguments">The values to format the string with.</param>
26+
/// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns>
27+
LocalizedString this[string name, params object[] arguments] { get; }
28+
29+
/// <summary>
30+
/// Gets all string resources.
31+
/// </summary>
32+
/// <param name="includeParentCultures">
33+
/// A <see cref="System.Boolean"/> indicating whether to include strings from parent cultures.
34+
/// </param>
35+
/// <returns>The strings.</returns>
36+
IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures);
37+
38+
/// <summary>
39+
/// Creates a new <see cref="IStringLocalizer"/> for a specific <see cref="CultureInfo"/>.
40+
/// </summary>
41+
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
42+
/// <returns>A culture-specific <see cref="IStringLocalizer"/>.</returns>
43+
IStringLocalizer WithCulture(CultureInfo culture);
44+
}
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
6+
namespace Microsoft.Extensions.Localization
7+
{
8+
/// <summary>
9+
/// Represents a factory that creates <see cref="IStringLocalizer"/> instances.
10+
/// </summary>
11+
public interface IStringLocalizerFactory
12+
{
13+
/// <summary>
14+
/// Creates an <see cref="IStringLocalizer"/> using the <see cref="System.Reflection.Assembly"/> and
15+
/// <see cref="Type.FullName"/> of the specified <see cref="Type"/>.
16+
/// </summary>
17+
/// <param name="resourceSource">The <see cref="Type"/>.</param>
18+
/// <returns>The <see cref="IStringLocalizer"/>.</returns>
19+
IStringLocalizer Create(Type resourceSource);
20+
21+
/// <summary>
22+
/// Creates an <see cref="IStringLocalizer"/>.
23+
/// </summary>
24+
/// <param name="baseName">The base name of the resource to load strings from.</param>
25+
/// <param name="location">The location to load resources from.</param>
26+
/// <returns>The <see cref="IStringLocalizer"/>.</returns>
27+
IStringLocalizer Create(string baseName, string location);
28+
}
29+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
namespace Microsoft.Extensions.Localization
5+
{
6+
/// <summary>
7+
/// Represents an <see cref="IStringLocalizer"/> that provides strings for <typeparamref name="T"/>.
8+
/// </summary>
9+
/// <typeparam name="T">The <see cref="System.Type"/> to provide strings for.</typeparam>
10+
public interface IStringLocalizer<T> : IStringLocalizer
11+
{
12+
13+
}
14+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
6+
namespace Microsoft.Extensions.Localization
7+
{
8+
/// <summary>
9+
/// A locale specific string.
10+
/// </summary>
11+
public class LocalizedString
12+
{
13+
/// <summary>
14+
/// Creates a new <see cref="LocalizedString"/>.
15+
/// </summary>
16+
/// <param name="name">The name of the string in the resource it was loaded from.</param>
17+
/// <param name="value">The actual string.</param>
18+
public LocalizedString(string name, string value)
19+
: this(name, value, resourceNotFound: false)
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Creates a new <see cref="LocalizedString"/>.
25+
/// </summary>
26+
/// <param name="name">The name of the string in the resource it was loaded from.</param>
27+
/// <param name="value">The actual string.</param>
28+
/// <param name="resourceNotFound">Whether the string was not found in a resource. Set this to <c>true</c> to indicate an alternate string value was used.</param>
29+
public LocalizedString(string name, string value, bool resourceNotFound)
30+
: this(name, value, resourceNotFound, searchedLocation: null)
31+
{
32+
}
33+
34+
/// <summary>
35+
/// Creates a new <see cref="LocalizedString"/>.
36+
/// </summary>
37+
/// <param name="name">The name of the string in the resource it was loaded from.</param>
38+
/// <param name="value">The actual string.</param>
39+
/// <param name="resourceNotFound">Whether the string was not found in a resource. Set this to <c>true</c> to indicate an alternate string value was used.</param>
40+
/// <param name="searchedLocation">The location which was searched for a localization value.</param>
41+
public LocalizedString(string name, string value, bool resourceNotFound, string searchedLocation)
42+
{
43+
if (name == null)
44+
{
45+
throw new ArgumentNullException(nameof(name));
46+
}
47+
48+
if (value == null)
49+
{
50+
throw new ArgumentNullException(nameof(value));
51+
}
52+
53+
Name = name;
54+
Value = value;
55+
ResourceNotFound = resourceNotFound;
56+
SearchedLocation = searchedLocation;
57+
}
58+
59+
public static implicit operator string(LocalizedString localizedString)
60+
{
61+
return localizedString?.Value;
62+
}
63+
64+
/// <summary>
65+
/// The name of the string in the resource it was loaded from.
66+
/// </summary>
67+
public string Name { get; }
68+
69+
/// <summary>
70+
/// The actual string.
71+
/// </summary>
72+
public string Value { get; }
73+
74+
/// <summary>
75+
/// Whether the string was not found in a resource. If <c>true</c>, an alternate string value was used.
76+
/// </summary>
77+
public bool ResourceNotFound { get; }
78+
79+
/// <summary>
80+
/// The location which was searched for a localization value.
81+
/// </summary>
82+
public string SearchedLocation { get; }
83+
84+
/// <summary>
85+
/// Returns the actual string.
86+
/// </summary>
87+
/// <returns>The actual string.</returns>
88+
public override string ToString() => Value;
89+
}
90+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Product>Microsoft .NET Extensions</Product>
5+
<Description>Abstractions of application localization services.
6+
Commonly used types:
7+
Microsoft.Extensions.Localization.IStringLocalizer
8+
Microsoft.Extensions.Localization.IStringLocalizer&lt;T&gt;</Description>
9+
<TargetFramework>netstandard2.0</TargetFramework>
10+
<NoWarn>$(NoWarn);CS1591</NoWarn>
11+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
12+
<PackageTags>localization</PackageTags>
13+
</PropertyGroup>
14+
15+
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
7+
namespace Microsoft.Extensions.Localization
8+
{
9+
public static class StringLocalizerExtensions
10+
{
11+
/// <summary>
12+
/// Gets the string resource with the given name.
13+
/// </summary>
14+
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
15+
/// <param name="name">The name of the string resource.</param>
16+
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
17+
public static LocalizedString GetString(
18+
this IStringLocalizer stringLocalizer,
19+
string name)
20+
{
21+
if (stringLocalizer == null)
22+
{
23+
throw new ArgumentNullException(nameof(stringLocalizer));
24+
}
25+
26+
if (name == null)
27+
{
28+
throw new ArgumentNullException(nameof(name));
29+
}
30+
31+
return stringLocalizer[name];
32+
}
33+
34+
/// <summary>
35+
/// Gets the string resource with the given name and formatted with the supplied arguments.
36+
/// </summary>
37+
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
38+
/// <param name="name">The name of the string resource.</param>
39+
/// <param name="arguments">The values to format the string with.</param>
40+
/// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns>
41+
public static LocalizedString GetString(
42+
this IStringLocalizer stringLocalizer,
43+
string name,
44+
params object[] arguments)
45+
{
46+
if (stringLocalizer == null)
47+
{
48+
throw new ArgumentNullException(nameof(stringLocalizer));
49+
}
50+
51+
if (name == null)
52+
{
53+
throw new ArgumentNullException(nameof(name));
54+
}
55+
56+
return stringLocalizer[name, arguments];
57+
}
58+
59+
/// <summary>
60+
/// Gets all string resources including those for parent cultures.
61+
/// </summary>
62+
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
63+
/// <returns>The string resources.</returns>
64+
public static IEnumerable<LocalizedString> GetAllStrings(this IStringLocalizer stringLocalizer)
65+
{
66+
if (stringLocalizer == null)
67+
{
68+
throw new ArgumentNullException(nameof(stringLocalizer));
69+
}
70+
71+
return stringLocalizer.GetAllStrings(includeParentCultures: true);
72+
}
73+
}
74+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 System.Globalization;
7+
8+
namespace Microsoft.Extensions.Localization
9+
{
10+
/// <summary>
11+
/// Provides strings for <typeparamref name="TResourceSource"/>.
12+
/// </summary>
13+
/// <typeparam name="TResourceSource">The <see cref="Type"/> to provide strings for.</typeparam>
14+
public class StringLocalizer<TResourceSource> : IStringLocalizer<TResourceSource>
15+
{
16+
private IStringLocalizer _localizer;
17+
18+
/// <summary>
19+
/// Creates a new <see cref="StringLocalizer{TResourceSource}"/>.
20+
/// </summary>
21+
/// <param name="factory">The <see cref="IStringLocalizerFactory"/> to use.</param>
22+
public StringLocalizer(IStringLocalizerFactory factory)
23+
{
24+
if (factory == null)
25+
{
26+
throw new ArgumentNullException(nameof(factory));
27+
}
28+
29+
_localizer = factory.Create(typeof(TResourceSource));
30+
}
31+
32+
/// <inheritdoc />
33+
public virtual IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
34+
35+
/// <inheritdoc />
36+
public virtual LocalizedString this[string name]
37+
{
38+
get
39+
{
40+
if (name == null)
41+
{
42+
throw new ArgumentNullException(nameof(name));
43+
}
44+
45+
return _localizer[name];
46+
}
47+
}
48+
49+
/// <inheritdoc />
50+
public virtual LocalizedString this[string name, params object[] arguments]
51+
{
52+
get
53+
{
54+
if (name == null)
55+
{
56+
throw new ArgumentNullException(nameof(name));
57+
}
58+
59+
return _localizer[name, arguments];
60+
}
61+
}
62+
63+
/// <inheritdoc />
64+
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) =>
65+
_localizer.GetAllStrings(includeParentCultures);
66+
}
67+
}

0 commit comments

Comments
 (0)