Access source segments

Hi,

I created a project in Studio and would like to access all source segments for the files in that project. What would be the easiest way?

I tried this:

FileBasedProject currFbp = new FileBasedProject(filepath);
projectsController.Open(currFbp);
var projectFiles = projectsController.CurrentProject.GetSourceLanguageFiles();
foreach (var projectFile in projectFiles)
{
    var editorController = SdlTradosStudio.Application.GetController<EditorController>();
    editorController.Activate();
    editorController.Open(projectFile);
}

It now opens the editor and I can access all segments. However, before it opens a file, I get a dialog.

I then tried to open all files:


FileBasedProject currFbp = new FileBasedProject(filepath);
projectsController.Open(currFbp);
var projectFiles = projectsController.CurrentProject.GetSourceLanguageFiles();
var editorController = SdlTradosStudio.Application.GetController<EditorController>();
editorController.Activate();
editorController.Open(projectFiles, EditingMode.Review);

However, now I get an exception: "An exception of type 'System.InvalidOperationException' occurred in Sdl.TranslationStudioAutomation.IntegrationApi.dll but was not handled in user code

Additional information: The file xyz.docx.sdlxliff belongs to a project that is not currently open. Open the project first."

Thanks,

Henk

Parents Reply Children
  • Hello, how can I access this custom batch task code, WITHOUT using the Trados UI? In other words, how do I initiate the code flow of this batch task and make it run, without using the Trados UI at all? I have my own Trados API application that needs to count the words per segment, again without ever opening Trados UI. Please help!

    emoji
  • I found a workaround and posting it here in case someone else needs it. I am creating a fake TU using the source segment and an arbitrary target segment text, to make the TU valid. Then I insert the TU into a (empty) TM just to let the TM tokenize the TU, and thus the source segment object reference. After that, the .tokens attribute of the source segment object are populated. Then I use WordCounts() of the Sdl.LanguagePlatform.TranslationMemory namespace and pass the list of tokens to get back the number of "words". Example code below:

    public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
            {
                if (paragraphUnit.IsStructure == false)
                {
                    foreach (ISegmentPair segmentPair in paragraphUnit.SegmentPairs)
                    {
                        string mid = segmentPair.Source.Properties.Id.ToString();

                        Segment sourceSegment = ISegmentToSegmentConvert.ConvertToSegment(segmentPair.Source,
                            CultureInfo.GetCultureInfo(this.sourceLang));  // convert from ISegment to Segment
                        
                        var tu = new TranslationUnit(sourceSegment, new Segment(new CultureInfo(this.targetLang)));                    
                        tu.TargetSegment.Add("Sample target text");  // random text just to fill the target of the TU
                        ErrorCode validation = tu.Validate();

                        // Hack: add the TU into the empty TM to force the .tokens of the source segment to be generated
                        this.tm.LanguageDirection.AddTranslationUnit(tu, new ImportSettings());

                        if (sourceSegment.Tokens == null)
                        {
                            Console.WriteLine("Word calculation returned null tokens");
                            Environment.Exit(1);
                        }

                        // calculate word count per source segment
                        int wc = new WordCounts(sourceSegment.Tokens, false, false, true, false).Words;
                    }
                }
            }
    emoji