/// <summary> /// Merge /// </summary> /// <param name="source">源 Translation Package,将合并到此包内</param> /// <param name="other">需要合并的素材 Translation Package,合并后此包将抛弃</param> /// <param name="level">合并级别</param> /// <param name="customMergeProvider">自定义合并方法提供者程序</param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static ITranslatePackage Merge( ITranslatePackage source, ITranslatePackage other, MergeLevel level = MergeLevel.Level_1, Func <ITranslatePackageMergeOps, ITranslatePackageMergeOps, ITranslatePackage> customMergeProvider = null) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (other == null) { throw new ArgumentNullException(nameof(other)); } var left = source as ITranslatePackageMergeOps; var right = other as ITranslatePackageMergeOps; if (left == null) { throw new ArgumentException($"The source translate package should be an implementation of '{nameof(ITranslatePackageMergeOps)}'"); } if (right == null) { throw new ArgumentException($"The target translate package should be an implementation of '{nameof(ITranslatePackageMergeOps)}'"); } var ret = source; switch (level) { case MergeLevel.Level_1: MergeInLevel1(left, right); break; case MergeLevel.Level_2: MergeInLevel2(left, right); break; case MergeLevel.Custom: if (customMergeProvider == null) { throw new ArgumentNullException(nameof(customMergeProvider)); } ret = customMergeProvider(left, right); break; } return(ret); }
/// <summary> /// Add translation package /// </summary> /// <param name="package"></param> /// <param name="level"></param> /// <param name="customMergeProvider"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public I18NOptions AddPackage(ITranslatePackage package, MergeLevel level = MergeLevel.Level_1, Func <ITranslatePackageMergeOps, ITranslatePackageMergeOps, ITranslatePackage> customMergeProvider = null) { if (package == null) { throw new ArgumentNullException(nameof(package)); } lock (__lock_package) { var hashOfPackageKey = package.PackageKey.GetHashCode(); if (__translationPackages.TryGetValue(hashOfPackageKey, out var packageInstance)) { var template = TranslatePackageMerger.Merge(packageInstance, package, level, customMergeProvider); __translationPackages[hashOfPackageKey] = template; } else { AddPackageInternal(package); } } return(this); }
int DateFolderLength( MergeLevel level ) { /* Separate out the parts of a DateFolder filename * based on the merge level. * * yyyy-MM-dd-HHmmss * | | | | | | * 12345678901234567 */ switch( level ) { case MergeLevel.Year : return 4; case MergeLevel.Month : return 7; case MergeLevel.Day : return 10; case MergeLevel.Hour : return 13; case MergeLevel.Minute: return 15; default : return 17; } }
void Merge( MergeLevel level ) { if( cancel ) { return; } string s = DateFolderSkipCurrent ( level ); int z = DateFolderLength ( level ); if( ! string.IsNullOrEmpty( s ) && (z > 0) ) { //..merge directories string[] a = ctrl.HistoryFolders(); int i = a.Length; do { --i; } while( (i > 0) && (string.Compare( a[i], s, true ) >= 0 ) ); while( i > 0 ) { int j = i; int x = 0x00; while( (--i >= 0) && (string.Compare( a[i], 0, a[j], 0, z, true ) == 0) ) { if( x == 0x00 ) { LogInfo( Status( " " + level.ToString(), a[j] ) ); x |= 0x01; } LogInfo( Status( " ..remove/merge", a[i] ) ); if( ! Merge( a[i], a[j] ) ) { LogInfo( " merge failed" ); x |= 0x02; } } //rename minute folders which have seconds on them if( (x <= 0x01) && (level == MergeLevel.Minute) && (a[j].Length > z) ) { string t = a[j].Substring( 0, z ); LogInfo( Status( " ..rename", a[j] + " " + t ) ); DirectoryMove( PathCombine( ctrl.Path, a[j] ), PathCombine( ctrl.Path, t ) ); } } } }
string DateFolderSkipCurrent( MergeLevel level ) { DateTime dt = DateTime.MinValue; CtrlArchive archive = ctrl.Archive; if( archive != null ) { switch( level ) { case MergeLevel.Year : if( archive.Month > 0 ) { dt = start.AddMonths ( 0 - archive.Month ); } break; case MergeLevel.Month : if( archive.Day > 0 ) { dt = start.AddDays ( 0 - archive.Day ); } break; case MergeLevel.Day : if( archive.Hour > 0 ) { dt = start.AddHours ( 0 - archive.Hour ); } break; case MergeLevel.Hour : if( archive.Minute > 0 ) { dt = start.AddMinutes ( 0 - archive.Minute ); } break; case MergeLevel.Minute: dt = start; break; } } return (dt == DateTime.MinValue) ? null : DateFolderSecond( dt ); }
/// <summary> /// Add translation package /// </summary> /// <param name="packageProvider"></param> /// <param name="level"></param> /// <param name="customMergeProvider"></param> /// <returns></returns> public I18NOptions AddPackage(Func <ITranslatePackage> packageProvider, MergeLevel level = MergeLevel.Level_1, Func <ITranslatePackageMergeOps, ITranslatePackageMergeOps, ITranslatePackage> customMergeProvider = null) { return(AddPackage(packageProvider(), level, customMergeProvider)); }