Skip to content

Commit 180ea48

Browse files
committed
Fix legacy code
1 parent 2451412 commit 180ea48

25 files changed

+1089
-126
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<PropertyGroup>
99
<Version>2.0.0</Version>
10-
<PackageVersion>2.0.0-rc.1</PackageVersion>
10+
<PackageVersion>2.0.0-rc.2</PackageVersion>
1111
</PropertyGroup>
1212

1313
<PropertyGroup>

Directory.Packages.props

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageVersion Include="NSubstitute" Version="5.1.0" />
1313
<PackageVersion Include="System.Text.Json" Version="6.0.0" />
1414
<PackageVersion Include="PolySharp" Version="1.13.1" />
15+
<PackageVersion Include="xunit.extensibility.core" Version="2.6.2" />
1516
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
1617
<PackageVersion Include="xunit" Version="2.6.2" />
1718
</ItemGroup>

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ https://www.nuget.org/packages/Lepo.i18n.Json
2020
You can add it to your project using .NET CLI:
2121

2222
```powershell
23-
dotnet add package Lepo.i18n
23+
dotnet add package Lepo.i18n.Wpf
24+
dotnet add package Lepo.i18n.DependencyInjection
2425
```
2526

2627
, or package manager console:
2728

2829
```powershell
29-
NuGet\Install-Package Lepo.i18n
30+
NuGet\Install-Package Lepo.i18n.Wpf
31+
NuGet\Install-Package Lepo.i18n.DependencyInjection
3032
```
3133

3234
### 🛠️ How to Use Lepo i18n
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// This Source Code Form is subject to the terms of the MIT License.
2+
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
3+
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
4+
// All Rights Reserved.
5+
6+
namespace Lepo.i18n.DependencyInjection;
7+
8+
/// <summary>
9+
/// Provides a localization set-based implementation of the <see cref="IStringLocalizer"/> interface.
10+
/// </summary>
11+
/// <remarks>
12+
/// This class uses a <see cref="LocalizationSet"/> to retrieve localized strings.
13+
/// </remarks>
14+
public class LocalizationSetStringLocalizer(LocalizationSet localizationSet) : IStringLocalizer
15+
{
16+
/// <inheritdoc />
17+
public LocalizedString this[string name] => this[name, []];
18+
19+
/// <inheritdoc />
20+
public LocalizedString this[string name, params object[] arguments] =>
21+
LocalizeString(name, arguments);
22+
23+
/// <inheritdoc />
24+
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
25+
{
26+
return localizationSet.Strings.Select(x => new LocalizedString(x.Key, x.Value ?? x.Key))
27+
?? [];
28+
}
29+
30+
/// <summary>
31+
/// Fills placeholders in a string with the provided values.
32+
/// </summary>
33+
/// <param name="name">The string with placeholders.</param>
34+
/// <param name="placeholders">The values to fill the placeholders with.</param>
35+
/// <returns>The string with filled placeholders.</returns>
36+
private LocalizedString LocalizeString(string name, object[] placeholders)
37+
{
38+
return new LocalizedString(
39+
name,
40+
FillPlaceholders(
41+
GetAllStrings(true).FirstOrDefault(x => x.Name == name) ?? name,
42+
placeholders
43+
)
44+
);
45+
}
46+
47+
/// <summary>
48+
/// Fills placeholders in a string with the provided values.
49+
/// </summary>
50+
/// <param name="value">The string with placeholders.</param>
51+
/// <param name="placeholders">The values to fill the placeholders with.</param>
52+
/// <returns>The string with filled placeholders.</returns>
53+
private static string FillPlaceholders(string value, object[] placeholders)
54+
{
55+
for (int i = 0; i < placeholders.Length; i++)
56+
{
57+
value = value.Replace($"{{{i}}}", placeholders[i].ToString());
58+
}
59+
60+
return value;
61+
}
62+
}

src/Lepo.i18n.DependencyInjection/StaticStringLocalizer.cs src/Lepo.i18n.DependencyInjection/ProviderBasedStringLocalizer.cs

+21-18
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,38 @@
66
namespace Lepo.i18n.DependencyInjection;
77

88
/// <summary>
9-
/// Provides functionality to localize strings.
9+
/// Provides a provider-based implementation of the <see cref="IStringLocalizer"/> interface.
1010
/// </summary>
11-
public class StaticStringLocalizer(
11+
/// <remarks>
12+
/// This class uses an <see cref="ILocalizationProvider"/> to retrieve localization sets,
13+
/// and an <see cref="ILocalizationCultureManager"/> to manage the current culture.
14+
/// </remarks>
15+
public class ProviderBasedStringLocalizer(
1216
ILocalizationProvider localizations,
1317
ILocalizationCultureManager cultureManager
1418
) : IStringLocalizer
1519
{
16-
/// <summary>
17-
/// Gets the localized string for the specified name.
18-
/// </summary>
19-
/// <param name="name">The name of the localized string.</param>
20-
/// <returns>The localized string.</returns>
20+
/// <inheritdoc />
2121
public LocalizedString this[string name] => this[name, []];
2222

23-
/// <summary>
24-
/// Gets the localized string for the specified name and format arguments.
25-
/// </summary>
26-
/// <param name="name">The name of the localized string.</param>
27-
/// <param name="arguments">The format arguments.</param>
28-
/// <returns>The localized string.</returns>
23+
/// <inheritdoc />
2924
public LocalizedString this[string name, params object[] arguments] =>
3025
LocalizeString(name, arguments);
3126

32-
/// <summary>
33-
/// Gets all the localized strings for the current culture.
34-
/// </summary>
35-
/// <param name="_">A boolean parameter (not used).</param>
36-
/// <returns>The localized strings.</returns>
27+
/// <inheritdoc />
3728
public IEnumerable<LocalizedString> GetAllStrings(bool _)
3829
{
3930
return localizations
4031
.GetLocalizationSet(cultureManager.GetCulture(), default)
4132
?.Strings.Select(x => new LocalizedString(x.Key, x.Value ?? x.Key)) ?? [];
4233
}
4334

35+
/// <summary>
36+
/// Fills placeholders in a string with the provided values.
37+
/// </summary>
38+
/// <param name="name">The string with placeholders.</param>
39+
/// <param name="placeholders">The values to fill the placeholders with.</param>
40+
/// <returns>The string with filled placeholders.</returns>
4441
private LocalizedString LocalizeString(string name, object[] placeholders)
4542
{
4643
return new LocalizedString(
@@ -52,6 +49,12 @@ private LocalizedString LocalizeString(string name, object[] placeholders)
5249
);
5350
}
5451

52+
/// <summary>
53+
/// Fills placeholders in a string with the provided values.
54+
/// </summary>
55+
/// <param name="value">The string with placeholders.</param>
56+
/// <param name="placeholders">The values to fill the placeholders with.</param>
57+
/// <returns>The string with filled placeholders.</returns>
5558
private static string FillPlaceholders(string value, object[] placeholders)
5659
{
5760
for (int i = 0; i < placeholders.Length; i++)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This Source Code Form is subject to the terms of the MIT License.
2+
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
3+
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
4+
// All Rights Reserved.
5+
6+
namespace Lepo.i18n.DependencyInjection;
7+
8+
/// <summary>
9+
/// Provides a provider-based implementation of the <see cref="IStringLocalizerFactory"/> interface.
10+
/// </summary>
11+
/// <remarks>
12+
/// This class uses an <see cref="ILocalizationProvider"/> to retrieve localization sets,
13+
/// and an <see cref="ILocalizationCultureManager"/> to manage the current culture.
14+
/// </remarks>
15+
public class ProviderBasedStringLocalizerFactory(
16+
ILocalizationProvider localizations,
17+
ILocalizationCultureManager cultureManager
18+
) : IStringLocalizerFactory
19+
{
20+
/// <inheritdoc />
21+
public IStringLocalizer Create(Type resourceSource)
22+
{
23+
string? baseName = resourceSource.FullName?.ToLower().Trim();
24+
25+
return Create(baseName ?? default, default);
26+
}
27+
28+
/// <inheritdoc />
29+
public IStringLocalizer Create(string? baseName, string? location)
30+
{
31+
LocalizationSet? resourceLocalizationSet = localizations.GetLocalizationSet(
32+
cultureManager.GetCulture(),
33+
baseName
34+
);
35+
36+
if (resourceLocalizationSet is null)
37+
{
38+
resourceLocalizationSet = localizations.GetLocalizationSet(
39+
cultureManager.GetCulture(),
40+
default
41+
);
42+
}
43+
44+
if (resourceLocalizationSet is null)
45+
{
46+
throw new InvalidOperationException(
47+
"No localization set found for the given resource source."
48+
);
49+
}
50+
51+
return new LocalizationSetStringLocalizer(resourceLocalizationSet);
52+
}
53+
}

src/Lepo.i18n.DependencyInjection/ServiceCollectionExtensions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ Action<LocalizationBuilder> configure
2828
LocalizationProviderFactory.SetInstance(builder.Build());
2929

3030
_ = services.AddSingleton(_ => LocalizationProviderFactory.GetInstance()!);
31+
_ = services.AddTransient<IStringLocalizerFactory, ProviderBasedStringLocalizerFactory>();
3132
_ = services.AddTransient<ILocalizationCultureManager, LocalizationCultureManager>();
32-
_ = services.AddTransient<IStringLocalizer, StaticStringLocalizer>();
33+
_ = services.AddTransient<IStringLocalizer, ProviderBasedStringLocalizer>();
3334

3435
return services;
3536
}

src/Lepo.i18n.Wpf/GlobalUsings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
global using System;
77
global using System.Globalization;
8+
global using System.Linq;
89
global using System.Runtime.InteropServices;
910
global using System.Windows;
1011
global using System.Windows.Markup;

0 commit comments

Comments
 (0)