-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLocalizationExtensions.cs
More file actions
64 lines (58 loc) · 3.89 KB
/
Copy pathLocalizationExtensions.cs
File metadata and controls
64 lines (58 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Runtime.CompilerServices;
using SecureFolderFS.Sdk.Services;
using SecureFolderFS.Shared;
using SecureFolderFS.Shared.Helpers;
using SmartFormat;
namespace SecureFolderFS.Sdk.Extensions
{
public static class LocalizationExtensions
{
private static ILocalizationService? _fallbackLocalizationService;
/// <summary>
/// Converts the specified resource key to its localized string representation using the provided localization service.
/// If no localization service is provided, a fallback service is used.
/// </summary>
/// <param name="resourceKey">The key representing the resource to be localized.</param>
/// <param name="localizationService">An optional instance of <see cref="ILocalizationService"/> used to localize the resource key. If null, a fallback service will be used.</param>
/// <returns>A localized string corresponding to the specified resource key. If localization fails, the resource key surrounded by curly braces ("{...}") is returned.</returns>
public static string ToLocalized(this string resourceKey, ILocalizationService? localizationService = null)
{
localizationService = GetLocalizationService(localizationService);
var resource = localizationService?.GetResource(resourceKey);
if (string.IsNullOrEmpty(resource))
return $"{{{resourceKey}}}";
return resource.Replace("\\n", Environment.NewLine);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static ILocalizationService? GetLocalizationService(ILocalizationService? fallback)
{
return _fallbackLocalizationService ??= fallback ?? DI.OptionalService<ILocalizationService>();
}
}
/// <summary>
/// Converts the specified resource key to its localized string representation using the provided localization service
/// and formats it with the provided interpolation parameters.
/// </summary>
/// <param name="resourceKey">The key representing the resource to be localized and formatted.</param>
/// <param name="interpolate">An array of parameters used for string interpolation in the localized string representation.</param>
/// <returns>A formatted and localized string corresponding to the specified resource key. If localization or formatting fails, the resource key surrounded by curly braces ("{...}") is returned.</returns>
public static string ToLocalized(this string resourceKey, params object?[] interpolate)
{
var localized = ToLocalized(resourceKey);
return SafetyHelpers.NoFailureResult(() => Smart.Format(localized, interpolate)) ?? $"{{{resourceKey}}}";
}
/// <summary>
/// Converts the specified resource key to its localized string representation using the provided localization service
/// and formats it with the provided interpolation parameters.
/// </summary>
/// <param name="resourceKey">The key representing the resource to be localized and formatted.</param>
/// <param name="localizationService">The <see cref="ILocalizationService"/> to use.</param>
/// <param name="interpolate">An array of parameters used for string interpolation in the localized string representation.</param>
/// <returns>A formatted and localized string corresponding to the specified resource key. If localization or formatting fails, the resource key surrounded by curly braces ("{...}") is returned.</returns>
public static string ToLocalized(this string resourceKey, ILocalizationService localizationService, params object?[] interpolate)
{
var localized = ToLocalized(resourceKey, localizationService);
return SafetyHelpers.NoFailureResult(() => Smart.Format(localized, interpolate)) ?? $"{{{resourceKey}}}";
}
}
}