33// Copyright (C) Leszek Pomianowski and Lepo.i18n Contributors.
44// All Rights Reserved.
55
6+ using Lepo . i18n . IO ;
7+
68namespace Lepo . i18n ;
79
810/// <summary>
@@ -18,7 +20,7 @@ public class LocalizationBuilder
1820 /// Builds an <see cref="ILocalizationProvider"/> using the current culture and localizations.
1921 /// </summary>
2022 /// <returns>An <see cref="ILocalizationProvider"/> with the current culture and localizations.</returns>
21- public ILocalizationProvider Build ( )
23+ public virtual ILocalizationProvider Build ( )
2224 {
2325 return new LocalizationProvider (
2426 selectedCulture ?? CultureInfo . CurrentCulture ,
@@ -30,7 +32,7 @@ public ILocalizationProvider Build()
3032 /// Sets the culture for the <see cref="LocalizationBuilder"/>.
3133 /// </summary>
3234 /// <param name="culture">The culture to set.</param>
33- public void SetCulture ( CultureInfo culture )
35+ public virtual void SetCulture ( CultureInfo culture )
3436 {
3537 selectedCulture = culture ;
3638 }
@@ -40,7 +42,7 @@ public void SetCulture(CultureInfo culture)
4042 /// </summary>
4143 /// <param name="localization">The localization set to add.</param>
4244 /// <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 )
4446 {
4547 if (
4648 localizations . Any ( x =>
@@ -56,4 +58,47 @@ public void AddLocalization(LocalizationSet localization)
5658
5759 _ = localizations . Add ( localization ) ;
5860 }
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+ }
59104}
0 commit comments