Skip to content

Instantly share code, notes, and snippets.

@cromica
Created March 11, 2015 13:11
Show Gist options
  • Save cromica/af1242f60511c52fba79 to your computer and use it in GitHub Desktop.
Save cromica/af1242f60511c52fba79 to your computer and use it in GitHub Desktop.
Sample code for merging files using SDL Studio Project Automation API
//specify project creation information
var pi = new ProjectInfo()
{
Name = "Your project name",
LocalProjectFolder = @"Project path",
CreatedBy = "name of the person who created the project",
SourceLanguage = new Language(new CultureInfo("source language")),
TargetLanguages = new[] {new Language(new CultureInfo("target language"))}
};
var proj = new FileBasedProject(pi);
//add files to be translated to the project
proj.AddFiles(new[] {@"first file to be translated", @"second file to be translated"});
//get file id's for added files, this is need to specify that the files are
//translatable and to create the merge structure
ProjectFile[] files = proj.GetSourceLanguageFiles();
Guid[] fileIds = files.GetIds();
proj.SetFileRole(fileIds, FileRole.Translatable);
//name of the bilingual file that must have .sdlxliff extension
const string masterFileName = "master.sdlxliff";
//create the merged file structure
var mergedFile = proj.CreateMergedProjectFile(masterFileName, string.Empty, fileIds);
//get the default file type manager which is needed to generated the
//bilingual files
var fileTypeManager = DefaultFileTypeManager.CreateInstance(true);
fileTypeManager.SettingsBundle = proj.GetSettings();
//get the local file paths for the files that we want to merge
var filePaths = new string[mergedFile.ChildFiles.Length];
for (var i = 0; i < mergedFile.ChildFiles.Length; i++)
{
filePaths[i] = mergedFile.ChildFiles[i].LocalFilePath;
}
//get the default file converter to bilingual files
var converter = fileTypeManager.GetConverterToDefaultBilingual(filePaths, mergedFile.LocalFilePath,
pi.SourceLanguage.CultureInfo, null,
(source, e) =>
Debug.WriteLine("{0}: {1} - {2}", (source != null ? source.GetType().Name : "null"),
e.Level.ToString(), e.Message));
//generate the bilingual file
converter.Parse();
//update the merge strucutre and the project
proj.AddNewFileVersion(mergedFile.Id, mergedFile.LocalFilePath);
//this is optional and has nothing to do with merging the files. This will
//copy the merged file to all the target languages
proj.RunAutomaticTask(new[] {mergedFile.Id}, AutomaticTaskTemplateIds.CopyToTargetLanguages);
proj.Save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment