3
3
// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
4
4
// All Rights Reserved.
5
5
6
+ using Lepo . i18n . IO ;
7
+
6
8
namespace Lepo . i18n ;
7
9
8
10
/// <summary>
@@ -18,7 +20,7 @@ public class LocalizationBuilder
18
20
/// Builds an <see cref="ILocalizationProvider"/> using the current culture and localizations.
19
21
/// </summary>
20
22
/// <returns>An <see cref="ILocalizationProvider"/> with the current culture and localizations.</returns>
21
- public ILocalizationProvider Build ( )
23
+ public virtual ILocalizationProvider Build ( )
22
24
{
23
25
return new LocalizationProvider (
24
26
selectedCulture ?? CultureInfo . CurrentCulture ,
@@ -30,7 +32,7 @@ public ILocalizationProvider Build()
30
32
/// Sets the culture for the <see cref="LocalizationBuilder"/>.
31
33
/// </summary>
32
34
/// <param name="culture">The culture to set.</param>
33
- public void SetCulture ( CultureInfo culture )
35
+ public virtual void SetCulture ( CultureInfo culture )
34
36
{
35
37
selectedCulture = culture ;
36
38
}
@@ -40,7 +42,7 @@ public void SetCulture(CultureInfo culture)
40
42
/// </summary>
41
43
/// <param name="localization">The localization set to add.</param>
42
44
/// <exception cref="InvalidOperationException">Thrown when a localization set for the same culture already exists in the collection.</exception>
43
- public void AddLocalization ( LocalizationSet localization )
45
+ public virtual void AddLocalization ( LocalizationSet localization )
44
46
{
45
47
if (
46
48
localizations . Any ( x =>
@@ -56,4 +58,47 @@ public void AddLocalization(LocalizationSet localization)
56
58
57
59
_ = localizations . Add ( localization ) ;
58
60
}
61
+
62
+ /// <summary>
63
+ /// Adds localized strings from a resource in the calling assembly to the <see cref="LocalizationBuilder"/>.
64
+ /// </summary>
65
+ /// <typeparam name="TResource">The type of the resource.</typeparam>
66
+ /// <param name="builder">The <see cref="LocalizationBuilder"/> to add the localized strings to.</param>
67
+ /// <param name="culture">The culture for which the localized strings are provided.</param>
68
+ /// <returns>The <see cref="LocalizationBuilder"/> with the added localized strings.</returns>
69
+ public virtual void FromResource < TResource > ( CultureInfo culture )
70
+ {
71
+ Type resourceType = typeof ( TResource ) ;
72
+ string ? resourceName = resourceType . FullName ;
73
+
74
+ if ( resourceName is null )
75
+ {
76
+ return ;
77
+ }
78
+
79
+ FromResource ( resourceType . Assembly , resourceName , culture ) ;
80
+ }
81
+
82
+ /// <summary>
83
+ /// Adds localized strings from a resource with the specified base name in the specified assembly to the <see cref="LocalizationBuilder"/>.
84
+ /// </summary>
85
+ /// <param name="builder">The <see cref="LocalizationBuilder"/> to add the localized strings to.</param>
86
+ /// <param name="assembly">The assembly that contains the resource.</param>
87
+ /// <param name="baseName">The base name of the resource.</param>
88
+ /// <param name="culture">The culture for which the localized strings are provided.</param>
89
+ /// <returns>The <see cref="LocalizationBuilder"/> with the added localized strings.</returns>
90
+ /// <exception cref="LocalizationBuilderException">Thrown when the resource cannot be found.</exception>
91
+ public virtual void FromResource ( Assembly assembly , string baseName , CultureInfo culture )
92
+ {
93
+ LocalizationSet ? localizationSet = LocalizationSetResourceParser . Parse (
94
+ assembly ,
95
+ baseName ,
96
+ culture
97
+ ) ;
98
+
99
+ if ( localizationSet is not null )
100
+ {
101
+ AddLocalization ( localizationSet ) ;
102
+ }
103
+ }
59
104
}
0 commit comments