-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180ea48
commit 3fbce60
Showing
9 changed files
with
157 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Lepo.i18n.DependencyInjection/DependencyInjectionLocalizationBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 dependency injection-based implementation of the <see cref="LocalizationBuilder"/> class. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class uses the .NET Core dependency injection framework to register localization services. | ||
/// It allows the registration of resources for specific cultures using the <see cref="FromResource{TResource}"/> method. | ||
/// </remarks> | ||
public class DependencyInjectionLocalizationBuilder(IServiceCollection services) | ||
: LocalizationBuilder | ||
{ | ||
/// <inheritdoc /> | ||
public override void FromResource<TResource>(CultureInfo culture) | ||
{ | ||
base.FromResource<TResource>(culture); | ||
|
||
_ = services.AddSingleton< | ||
IStringLocalizer<TResource>, | ||
ProviderBasedStringLocalizer<TResource> | ||
>(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Lepo.i18n.DependencyInjection/ProviderBasedStringLocalizer{TResource}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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="IStringLocalizer{TResource}"/> interface. | ||
/// </summary> | ||
/// <typeparam name="TResource">The type of the resource used for localization.</typeparam> | ||
/// <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<TResource>( | ||
ILocalizationProvider localizations, | ||
ILocalizationCultureManager cultureManager | ||
) : IStringLocalizer<TResource> | ||
{ | ||
/// <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 _) | ||
{ | ||
return localizations | ||
.GetLocalizationSet( | ||
cultureManager.GetCulture(), | ||
typeof(TResource).FullName?.ToLower() | ||
) | ||
?.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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters