-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
2451412
commit 180ea48
Showing
25 changed files
with
1,089 additions
and
126 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
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
62 changes: 62 additions & 0 deletions
62
src/Lepo.i18n.DependencyInjection/LocalizationSetStringLocalizer.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,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; | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/Lepo.i18n.DependencyInjection/ProviderBasedStringLocalizerFactory.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,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); | ||
} | ||
} |
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
Oops, something went wrong.