Skip to content

Commit

Permalink
Fix legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed Jun 1, 2024
1 parent 2451412 commit 180ea48
Show file tree
Hide file tree
Showing 25 changed files with 1,089 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<PropertyGroup>
<Version>2.0.0</Version>
<PackageVersion>2.0.0-rc.1</PackageVersion>
<PackageVersion>2.0.0-rc.2</PackageVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="System.Text.Json" Version="6.0.0" />
<PackageVersion Include="PolySharp" Version="1.13.1" />
<PackageVersion Include="xunit.extensibility.core" Version="2.6.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageVersion Include="xunit" Version="2.6.2" />
</ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ https://www.nuget.org/packages/Lepo.i18n.Json
You can add it to your project using .NET CLI:

```powershell
dotnet add package Lepo.i18n
dotnet add package Lepo.i18n.Wpf
dotnet add package Lepo.i18n.DependencyInjection
```

, or package manager console:

```powershell
NuGet\Install-Package Lepo.i18n
NuGet\Install-Package Lepo.i18n.Wpf
NuGet\Install-Package Lepo.i18n.DependencyInjection
```

### 🛠️ How to Use Lepo i18n
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
// All Rights Reserved.

namespace Lepo.i18n.DependencyInjection;

/// <summary>
/// Provides a localization set-based implementation of the <see cref="IStringLocalizer"/> interface.
/// </summary>
/// <remarks>
/// This class uses a <see cref="LocalizationSet"/> to retrieve localized strings.
/// </remarks>
public class LocalizationSetStringLocalizer(LocalizationSet localizationSet) : IStringLocalizer
{
/// <inheritdoc />
public LocalizedString this[string name] => this[name, []];

/// <inheritdoc />
public LocalizedString this[string name, params object[] arguments] =>
LocalizeString(name, arguments);

/// <inheritdoc />
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{
return localizationSet.Strings.Select(x => new LocalizedString(x.Key, x.Value ?? x.Key))
?? [];
}

/// <summary>
/// Fills placeholders in a string with the provided values.
/// </summary>
/// <param name="name">The string with placeholders.</param>
/// <param name="placeholders">The values to fill the placeholders with.</param>
/// <returns>The string with filled placeholders.</returns>
private LocalizedString LocalizeString(string name, object[] placeholders)
{
return new LocalizedString(
name,
FillPlaceholders(
GetAllStrings(true).FirstOrDefault(x => x.Name == name) ?? name,
placeholders
)
);
}

/// <summary>
/// Fills placeholders in a string with the provided values.
/// </summary>
/// <param name="value">The string with placeholders.</param>
/// <param name="placeholders">The values to fill the placeholders with.</param>
/// <returns>The string with filled placeholders.</returns>
private static string FillPlaceholders(string value, object[] placeholders)
{
for (int i = 0; i < placeholders.Length; i++)
{
value = value.Replace($"{{{i}}}", placeholders[i].ToString());
}

return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,38 @@
namespace Lepo.i18n.DependencyInjection;

/// <summary>
/// Provides functionality to localize strings.
/// Provides a provider-based implementation of the <see cref="IStringLocalizer"/> interface.
/// </summary>
public class StaticStringLocalizer(
/// <remarks>
/// This class uses an <see cref="ILocalizationProvider"/> to retrieve localization sets,
/// and an <see cref="ILocalizationCultureManager"/> to manage the current culture.
/// </remarks>
public class ProviderBasedStringLocalizer(
ILocalizationProvider localizations,
ILocalizationCultureManager cultureManager
) : IStringLocalizer
{
/// <summary>
/// Gets the localized string for the specified name.
/// </summary>
/// <param name="name">The name of the localized string.</param>
/// <returns>The localized string.</returns>
/// <inheritdoc />
public LocalizedString this[string name] => this[name, []];

/// <summary>
/// Gets the localized string for the specified name and format arguments.
/// </summary>
/// <param name="name">The name of the localized string.</param>
/// <param name="arguments">The format arguments.</param>
/// <returns>The localized string.</returns>
/// <inheritdoc />
public LocalizedString this[string name, params object[] arguments] =>
LocalizeString(name, arguments);

/// <summary>
/// Gets all the localized strings for the current culture.
/// </summary>
/// <param name="_">A boolean parameter (not used).</param>
/// <returns>The localized strings.</returns>
/// <inheritdoc />
public IEnumerable<LocalizedString> GetAllStrings(bool _)
{
return localizations
.GetLocalizationSet(cultureManager.GetCulture(), default)
?.Strings.Select(x => new LocalizedString(x.Key, x.Value ?? x.Key)) ?? [];
}

/// <summary>
/// Fills placeholders in a string with the provided values.
/// </summary>
/// <param name="name">The string with placeholders.</param>
/// <param name="placeholders">The values to fill the placeholders with.</param>
/// <returns>The string with filled placeholders.</returns>
private LocalizedString LocalizeString(string name, object[] placeholders)
{
return new LocalizedString(
Expand All @@ -52,6 +49,12 @@ private LocalizedString LocalizeString(string name, object[] placeholders)
);
}

/// <summary>
/// Fills placeholders in a string with the provided values.
/// </summary>
/// <param name="value">The string with placeholders.</param>
/// <param name="placeholders">The values to fill the placeholders with.</param>
/// <returns>The string with filled placeholders.</returns>
private static string FillPlaceholders(string value, object[] placeholders)
{
for (int i = 0; i < placeholders.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
// All Rights Reserved.

namespace Lepo.i18n.DependencyInjection;

/// <summary>
/// Provides a provider-based implementation of the <see cref="IStringLocalizerFactory"/> interface.
/// </summary>
/// <remarks>
/// This class uses an <see cref="ILocalizationProvider"/> to retrieve localization sets,
/// and an <see cref="ILocalizationCultureManager"/> to manage the current culture.
/// </remarks>
public class ProviderBasedStringLocalizerFactory(
ILocalizationProvider localizations,
ILocalizationCultureManager cultureManager
) : IStringLocalizerFactory
{
/// <inheritdoc />
public IStringLocalizer Create(Type resourceSource)
{
string? baseName = resourceSource.FullName?.ToLower().Trim();

return Create(baseName ?? default, default);
}

/// <inheritdoc />
public IStringLocalizer Create(string? baseName, string? location)
{
LocalizationSet? resourceLocalizationSet = localizations.GetLocalizationSet(
cultureManager.GetCulture(),
baseName
);

if (resourceLocalizationSet is null)
{
resourceLocalizationSet = localizations.GetLocalizationSet(
cultureManager.GetCulture(),
default
);
}

if (resourceLocalizationSet is null)
{
throw new InvalidOperationException(
"No localization set found for the given resource source."
);
}

return new LocalizationSetStringLocalizer(resourceLocalizationSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ Action<LocalizationBuilder> configure
LocalizationProviderFactory.SetInstance(builder.Build());

_ = services.AddSingleton(_ => LocalizationProviderFactory.GetInstance()!);
_ = services.AddTransient<IStringLocalizerFactory, ProviderBasedStringLocalizerFactory>();
_ = services.AddTransient<ILocalizationCultureManager, LocalizationCultureManager>();
_ = services.AddTransient<IStringLocalizer, StaticStringLocalizer>();
_ = services.AddTransient<IStringLocalizer, ProviderBasedStringLocalizer>();

return services;
}
Expand Down
1 change: 1 addition & 0 deletions src/Lepo.i18n.Wpf/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

global using System;
global using System.Globalization;
global using System.Linq;
global using System.Runtime.InteropServices;
global using System.Windows;
global using System.Windows.Markup;
Loading

0 comments on commit 180ea48

Please sign in to comment.