11using System . Collections . Generic ;
22using System . IO ;
33using System . Linq ;
4- using System . Text . RegularExpressions ;
4+
55using ICSharpCode . SharpZipLib . Zip ;
66using log4net ;
77using Newtonsoft . Json . Linq ;
8+
89using CKAN . Extensions ;
910using CKAN . NetKAN . Extensions ;
1011using CKAN . NetKAN . Model ;
@@ -21,10 +22,11 @@ internal sealed class LocalizationsTransformer : ITransformer
2122 /// </summary>
2223 /// <param name="http">HTTP service</param>
2324 /// <param name="moduleService">Module service</param>
24- public LocalizationsTransformer ( IHttpService http , IModuleService moduleService )
25+ public LocalizationsTransformer ( IHttpService http , IModuleService moduleService , IConfigParser parser )
2526 {
2627 _http = http ;
2728 _moduleService = moduleService ;
29+ _parser = parser ;
2830 }
2931
3032 /// <summary>
@@ -56,16 +58,13 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
5658
5759 log . Debug ( "Extracting locales" ) ;
5860 // Extract the locale names from the ZIP's cfg files
59- var locales = _moduleService . GetConfigFiles ( mod , zip , inst )
60- . Select ( cfg => new StreamReader ( zip . GetInputStream ( cfg . source ) ) . ReadToEnd ( ) )
61- . SelectMany ( contents => localizationRegex . Matches ( contents ) . Cast < Match > ( )
62- . Select ( m => m . Groups [ "contents" ] . Value ) )
63- . SelectMany ( contents => localeRegex . Matches ( contents ) . Cast < Match > ( )
64- . Where ( m => m . Groups [ "contents" ] . Value . Contains ( "=" ) )
65- . Select ( m => m . Groups [ "locale" ] . Value ) )
66- . Distinct ( )
67- . OrderBy ( l => l )
68- . Memoize ( ) ;
61+ var locales = _parser . GetConfigNodes ( mod , zip , inst )
62+ . SelectMany ( kvp => kvp . Value )
63+ . Where ( node => node . Name == localizationsNodeName )
64+ . SelectMany ( node => node . Children . Select ( child => child . Name ) )
65+ . Distinct ( )
66+ . OrderBy ( l => l )
67+ . Memoize ( ) ;
6968 log . Debug ( "Locales extracted" ) ;
7069
7170 if ( locales . Any ( ) )
@@ -82,20 +81,13 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
8281 }
8382 }
8483
84+ private const string localizationsNodeName = "Localization" ;
8585 private const string localizationsProperty = "localizations" ;
8686
8787 private readonly IHttpService _http ;
8888 private readonly IModuleService _moduleService ;
89+ private readonly IConfigParser _parser ;
8990
9091 private static readonly ILog log = LogManager . GetLogger ( typeof ( LocalizationsTransformer ) ) ;
91-
92- private static readonly Regex localizationRegex = new Regex (
93- @"^\s*Localization\b\s*{(?<contents>[^{}]+({[^{}]*}[^{}]*)+)}" ,
94- RegexOptions . Compiled | RegexOptions . Multiline | RegexOptions . Singleline
95- ) ;
96- private static readonly Regex localeRegex = new Regex (
97- @"^\s*(?<locale>[-a-zA-Z]+).*?{(?<contents>.*?)}" ,
98- RegexOptions . Compiled | RegexOptions . Multiline | RegexOptions . Singleline
99- ) ;
10092 }
10193}
0 commit comments