1111using System . Threading ;
1212using Microsoft . Extensions . Configuration ;
1313using Xtensive . Core ;
14+ using Xtensive . Orm . Configuration ;
1415
1516namespace Xtensive . Orm . Localization . Configuration
1617{
@@ -38,7 +39,7 @@ private class LocalizationOptions
3839 /// Gets or sets the default culture.
3940 /// </summary>
4041 /// <value>The default culture.</value>
41- public CultureInfo DefaultCulture { get ; private set ; }
42+ public CultureInfo DefaultCulture { get ; internal set ; }
4243
4344 /// <summary>
4445 /// Loads the <see cref="LocalizationConfiguration"/>
@@ -114,90 +115,30 @@ private static LocalizationConfiguration GetConfigurationFromSection(Configurati
114115 return result ;
115116 }
116117
118+ /// <summary>
119+ /// Loads <see cref="LocalizationConfiguration"/> from given configuration section.
120+ /// </summary>
121+ /// <param name="configurationSection"><see cref="IConfigurationSection"/> to load from.</param>
122+ /// <returns>Loaded configuration or default configuration if loading failed for some reason.</returns>
117123 public static LocalizationConfiguration Load ( IConfigurationSection configurationSection )
118124 {
119125 ArgumentValidator . EnsureArgumentNotNull ( configurationSection , nameof ( configurationSection ) ) ;
120126
121- if ( TryReadAsOptions ( configurationSection , out var localizationConfiguration ) )
122- return localizationConfiguration ;
123-
124- // if failed then try to handle unusual formats or xml with name attribute
125- return TryReadUnusualOrOldFormats ( configurationSection , out var fallbackConfiguration )
126- ? fallbackConfiguration
127- : new LocalizationConfiguration {
128- DefaultCulture = Thread . CurrentThread . CurrentCulture
129- } ;
130- }
131-
132- private static bool TryReadAsOptions ( IConfigurationSection rootSection , out LocalizationConfiguration localizationConfiguration )
133- {
134- LocalizationOptions localizationOptions ;
135- try {
136- localizationOptions = rootSection . Get < LocalizationOptions > ( ) ;
137- }
138- catch {
139- localizationConfiguration = null ;
140- return false ;
141- }
142-
143- if ( localizationOptions != null ) {
144- if ( ! string . IsNullOrEmpty ( localizationOptions . DefaultCulture ) ) {
145- try {
146- var culture = new CultureInfo ( localizationOptions . DefaultCulture ) ;
147- localizationConfiguration = new LocalizationConfiguration ( ) { DefaultCulture = culture } ;
148- return true ;
149- }
150- catch ( CultureNotFoundException ) {
151- }
152- }
153- }
154- localizationConfiguration = null ;
155- return false ;
127+ return new LocalizationConfigurationReader ( ) . Read ( configurationSection ) ;
156128 }
157129
158130 /// <summary>
159- /// Tries to read configuration of old format that supported by
160- /// old <see cref="System.Configuration.ConfigurationManager"/>
161- /// or configuration where name of service is element, not attribute.
131+ /// Loads <see cref="LocalizationConfiguration"/> from given configuration section of <paramref name="configurationRoot"/>.
132+ /// If section name is not provided <see cref="LocalizationConfiguration.DefaultSectionName"/> is used.
162133 /// </summary>
163- /// <param name="rootSection">A configuration section that contains data to read.</param>
164- /// <param name="localizationConfiguration">Read configuration or null if reading was not successful.</param>
165- /// <returns><see landword="true"/> if reading is successful, otherwise <see landword="true"/>.</returns>
166- private static bool TryReadUnusualOrOldFormats ( IConfigurationSection rootSection ,
167- out LocalizationConfiguration localizationConfiguration )
134+ /// <param name="configurationRoot"><see cref="IConfigurationRoot"/> of sections.</param>
135+ /// <param name="sectionName">Custom section name to load from.</param>
136+ /// <returns>Loaded configuration or default configuration if loading failed for some reason.</returns>
137+ public static LocalizationConfiguration Load ( IConfigurationRoot configurationRoot , string sectionName = null )
168138 {
169- var defaultCultureSection = rootSection . GetSection ( DefaultCultureElementName ) ;
170-
171- if ( defaultCultureSection == null ) {
172- localizationConfiguration = null ;
173- return false ;
174- }
175-
176- var cultureName = defaultCultureSection . GetSection ( CultureNameAttributeName ) ? . Value ;
177- if ( cultureName == null ) {
178- var children = defaultCultureSection . GetChildren ( ) . ToList ( ) ;
179- if ( children . Count > 0 ) {
180- cultureName = children [ 0 ] . GetSection ( CultureNameAttributeName ) . Value ;
181- }
182- }
139+ ArgumentValidator . EnsureArgumentNotNull ( configurationRoot , nameof ( configurationRoot ) ) ;
183140
184- if ( cultureName != null && ! string . IsNullOrEmpty ( cultureName ) ) {
185- try {
186- var culture = new CultureInfo ( cultureName ) ;
187- localizationConfiguration = new LocalizationConfiguration ( ) {
188- DefaultCulture = culture
189- } ;
190- return true ;
191- }
192- catch ( CultureNotFoundException ) {
193- localizationConfiguration = new LocalizationConfiguration ( ) {
194- DefaultCulture = Thread . CurrentThread . CurrentCulture
195- } ;
196- return true ;
197- }
198- }
199- localizationConfiguration = null ;
200- return false ;
141+ return new LocalizationConfigurationReader ( ) . Read ( configurationRoot , sectionName ?? DefaultSectionName ) ;
201142 }
202143 }
203144}
0 commit comments