Guest User

Modified ProjectHelper.psm1

a guest
Jun 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. #Uncomment to select which version of Studio you are using
  2. #param([String]$StudioVersion = "Studio4")
  3. param([String]$StudioVersion = "Studio5")
  4.  
  5. if ("${Env:ProgramFiles(x86)}") {
  6. $ProgramFilesDir = "${Env:ProgramFiles(x86)}"
  7. }
  8. else {
  9. $ProgramFilesDir = "${Env:ProgramFiles}"
  10. }
  11.  
  12. Add-Type -Path "$ProgramFilesDir\SDL\SDL Trados Studio\$StudioVersion\Sdl.ProjectAutomation.FileBased.dll"
  13. Add-Type -Path "$ProgramFilesDir\SDL\SDL Trados Studio\$StudioVersion\Sdl.ProjectAutomation.Core.dll"
  14.  
  15. function Get-TaskFileInfoFiles
  16. {
  17. param([Sdl.Core.Globalization.Language] $language, [Sdl.ProjectAutomation.FileBased.FileBasedProject] $project)
  18. [Sdl.ProjectAutomation.Core.TaskFileInfo[]]$taskFilesList = @();
  19. foreach($taskfile in $project.GetTargetLanguageFiles($language))
  20. {
  21. $fileInfo = New-Object Sdl.ProjectAutomation.Core.TaskFileInfo;
  22. $fileInfo.ProjectFileId = $taskfile.Id;
  23. $fileInfo.ReadOnly = $false;
  24. $taskFilesList = $taskFilesList + $fileInfo;
  25. }
  26. return $taskFilesList;
  27. }
  28.  
  29. function Remove-Project
  30. {
  31. param ([Sdl.ProjectAutomation.FileBased.FileBasedProject] $projectToDelete)
  32. $projectToDelete.Delete();
  33. }
  34.  
  35. function Validate-Task
  36. {
  37. param ([Sdl.ProjectAutomation.Core.AutomaticTask] $taskToValidate)
  38.  
  39. if($taskToValidate.Status -eq [Sdl.ProjectAutomation.Core.TaskStatus]::Failed)
  40. {
  41. Write-Host "Task "$taskToValidate.Name"was not completed.";
  42. foreach($message in $taskToValidate.Messages)
  43. {
  44. Write-Host $message.Message -ForegroundColor red ;
  45. }
  46. }
  47. if($taskToValidate.Status -eq [Sdl.ProjectAutomation.Core.TaskStatus]::Invalid)
  48. {
  49. Write-Host "Task "$taskToValidate.Name"was not completed.";
  50. foreach($message in $taskToValidate.Messages)
  51. {
  52. Write-Host $message.Message -ForegroundColor red ;
  53. }
  54. }
  55. if($taskToValidate.Status -eq [Sdl.ProjectAutomation.Core.TaskStatus]::Rejected)
  56. {
  57. Write-Host "Task "$taskToValidate.Name"was not completed.";
  58. foreach($message in $taskToValidate.Messages)
  59. {
  60. Write-Host $message.Message -ForegroundColor red ;
  61. }
  62. }
  63. if($taskToValidate.Status -eq [Sdl.ProjectAutomation.Core.TaskStatus]::Cancelled)
  64. {
  65. Write-Host "Task "$taskToValidate.Name"was not completed.";
  66. foreach($message in $taskToValidate.Messages)
  67. {
  68. Write-Host $message.Message -ForegroundColor red ;
  69. }
  70. }
  71. if($taskToValidate.Status -eq [Sdl.ProjectAutomation.Core.TaskStatus]::Completed)
  72. {
  73. Write-Host "Task "$taskToValidate.Name"was completed." -ForegroundColor green;
  74. }
  75. }
  76.  
  77. <#
  78. .DESCRIPTION
  79. Creates a new file based project. TM's are automatically assigned to the target languages.
  80. Following tasks are run automatically:
  81. - scan
  82. - convert to translatable format
  83. - copy to target languages
  84. - analyze
  85. - pretranslate
  86. #>
  87. function New-Project {
  88. param([String] $projectName,
  89. [String] $projectDestination,
  90. [Sdl.Core.Globalization.Language] $sourceLanguage,
  91. [Sdl.Core.Globalization.Language[]] $targetLanguages,
  92. [String[]] $pathToTMs,
  93. [String] $sourceFilesFolder,
  94. [String] $templateURI
  95. )
  96.  
  97. #create project info
  98. $projectInfo = new-object Sdl.ProjectAutomation.Core.ProjectInfo;
  99. $projectInfo.Name = $projectName;
  100. $projectInfo.LocalProjectFolder = $projectDestination;
  101. $projectInfo.SourceLanguage = $sourceLanguage;
  102. $projectInfo.TargetLanguages = $targetLanguages;
  103.  
  104. #create file based project
  105. if ([string]::IsNullOrWhiteSpace($templateURI)) {
  106. $fileBasedProject = New-Object Sdl.ProjectAutomation.FileBased.FileBasedProject $projectInfo;
  107. } else {
  108. $projectTemplate = new-object Sdl.ProjectAutomation.Core.ProjectTemplateReference $templateURI;
  109. $fileBasedProject = New-Object Sdl.ProjectAutomation.FileBased.FileBasedProject ($projectInfo, $projectTemplate);
  110. }
  111.  
  112. #Copy-Item $pathSampleFile -Destination $sourceFilesFolder;
  113. $projectFiles = $fileBasedProject.AddFolderWithFiles($sourceFilesFolder, $false);
  114.  
  115. #Assign TM's to project languages
  116. foreach($tmPath in $pathToTMs) {
  117. $tmTargetLanguageCulture = Get-TargetTMLanguage $tmPath;
  118. $tmTargetLanguage = Get-Language $tmTargetLanguageCulture.Name;
  119. [Sdl.ProjectAutomation.Core.TranslationProviderConfiguration] $tmConfig = $fileBasedProject.GetTranslationProviderConfiguration($tmTargetLanguage);
  120. $entry = New-Object Sdl.ProjectAutomation.Core.TranslationProviderCascadeEntry ($tmPath, $true, $true, $true);
  121. $tmConfig.Entries.Add($entry);
  122. $tmConfig.OverrideParent = $true;
  123. $fileBasedProject.UpdateTranslationProviderConfiguration($tmTargetLanguage, $tmConfig);
  124. }
  125.  
  126. #Get source language project files IDs
  127. [Sdl.ProjectAutomation.Core.ProjectFile[]] $projectFiles = $fileBasedProject.GetSourceLanguageFiles();
  128. [System.Guid[]] $sourceFilesGuids = Get-Guids $projectFiles;
  129.  
  130. #run preparation tasks
  131. Validate-Task $fileBasedProject.RunAutomaticTask($sourceFilesGuids,[Sdl.ProjectAutomation.Core.AutomaticTaskTemplateIds]::Scan);
  132. Validate-Task $fileBasedProject.RunAutomaticTask($sourceFilesGuids,[Sdl.ProjectAutomation.Core.AutomaticTaskTemplateIds]::ConvertToTranslatableFormat);
  133. Validate-Task $fileBasedProject.RunAutomaticTask($sourceFilesGuids,[Sdl.ProjectAutomation.Core.AutomaticTaskTemplateIds]::CopyToTargetLanguages);
  134.  
  135. #run analyze and pretranslate
  136. foreach($targetLanguage in $targetLanguages)
  137. {
  138. #Get target language project files IDs
  139. $targetFiles = $fileBasedProject.GetTargetLanguageFiles($targetLanguage);
  140. [System.Guid[]] $targetFilesGuids = Get-Guids $targetFiles;
  141.  
  142. Validate-Task $fileBasedProject.RunAutomaticTask($targetFilesGuids,[Sdl.ProjectAutomation.Core.AutomaticTaskTemplateIds]::AnalyzeFiles);
  143. Validate-Task $fileBasedProject.RunAutomaticTask($targetFilesGuids,[Sdl.ProjectAutomation.Core.AutomaticTaskTemplateIds]::PreTranslateFiles);
  144. }
  145.  
  146. #save whole project
  147. $fileBasedProject.Save();
  148. }
  149.  
  150. <#
  151. .DESCRIPTION
  152. Opens project on specified path.
  153. #>
  154. function Get-Project
  155. {
  156. param([String] $projectDestinationPath)
  157. #open file based project
  158. $projectFilePath = Get-ChildItem $projectDestinationPath -Filter *.sdlproj -Recurse | % { $_.FullName };
  159. $fileBasedProject = New-Object Sdl.ProjectAutomation.FileBased.FileBasedProject($projectFilePath.ToString());
  160. return $fileBasedProject;
  161. }
  162.  
  163.  
  164.  
  165. function Get-AnalyzeStatistics
  166. {
  167. param([Sdl.ProjectAutomation.FileBased.FileBasedProject] $project)
  168.  
  169. $projectStatistics = $project.GetProjectStatistics();
  170.  
  171. $targetLanguagesStatistics = $projectStatistics.TargetLanguageStatistics;
  172.  
  173. foreach($targetLanguageStatistic in $targetLanguagesStatistics)
  174. {
  175. Write-Host ("Exact Matches (characters): " + $targetLanguageStatistic.AnalysisStatistics.Exact.Characters);
  176. Write-Host ("Exact Matches (words): " + $targetLanguageStatistic.AnalysisStatistics.Exact.Words);
  177. Write-Host ("New Matches (characters): " + $targetLanguageStatistic.AnalysisStatistics.New.Characters);
  178. Write-Host ("New Matches (words): " + $targetLanguageStatistic.AnalysisStatistics.New.Words);
  179. Write-Host ("New Matches (segments): " + $targetLanguageStatistic.AnalysisStatistics.New.Segments);
  180. Write-Host ("New Matches (placeables): " + $targetLanguageStatistic.AnalysisStatistics.New.Placeables);
  181. Write-Host ("New Matches (tags): " + $targetLanguageStatistic.AnalysisStatistics.New.Tags);
  182. }
  183. }
  184.  
  185. Export-ModuleMember Remove-Project;
  186. Export-ModuleMember New-Project;
  187. Export-ModuleMember Get-Project;
  188. Export-ModuleMember Get-AnalyzeStatistics;
  189. Export-ModuleMember Get-TaskFileInfoFiles;
Advertisement
Add Comment
Please, Sign In to add comment