-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GoogleCloudTranslationProvider 2.0.1.0 - CRQ-40113: Added Sample Code…
…s for Pretranslating using Google Provider
- Loading branch information
1 parent
02ad6a3
commit 13fe538
Showing
5 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
..._PreTranslateUsingGoogleProvider/StandAloneConsoleApp_PreTranslateUsingGoogleProvider.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.11.35327.3 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StandAloneConsoleApp_PreTranslateUsingGoogleProvider", "StandAloneConsoleApp_PreTranslateUsingGoogleProvider\StandAloneConsoleApp_PreTranslateUsingGoogleProvider.csproj", "{AE2AC3D0-A442-4722-AA2E-F847D8D72953}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{AE2AC3D0-A442-4722-AA2E-F847D8D72953}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{AE2AC3D0-A442-4722-AA2E-F847D8D72953}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{AE2AC3D0-A442-4722-AA2E-F847D8D72953}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{AE2AC3D0-A442-4722-AA2E-F847D8D72953}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {8D2BC2EA-3499-4128-894D-4AD923DF18F1} | ||
EndGlobalSection | ||
EndGlobal |
6 changes: 6 additions & 0 deletions
6
...nslateUsingGoogleProvider/StandAloneConsoleApp_PreTranslateUsingGoogleProvider/App.config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> | ||
</startup> | ||
</configuration> |
201 changes: 201 additions & 0 deletions
201
...nslateUsingGoogleProvider/StandAloneConsoleApp_PreTranslateUsingGoogleProvider/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
using Sdl.Core.Globalization; | ||
using Sdl.ProjectAutomation.Core; | ||
using Sdl.ProjectAutomation.FileBased; | ||
using Sdl.ProjectAutomation.Settings; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace StandAloneConsoleApp_PretranslateUsingLanguageWeaverProvider | ||
{ | ||
public static class Program | ||
{ | ||
private static List<string> assemblyFolders = new List<string>(); | ||
|
||
private static void AddFilesToProject(IProject project, string sourceFilesDirectory) | ||
{ | ||
project.AddFolderWithFiles(sourceFilesDirectory, true); | ||
|
||
var projectFiles = project.GetSourceLanguageFiles(); | ||
|
||
var scanResult = project.RunAutomaticTask( | ||
projectFiles.GetIds(), | ||
AutomaticTaskTemplateIds.Scan | ||
); | ||
|
||
Console.WriteLine("Scan: " + scanResult.Status); | ||
|
||
var files = project.GetSourceLanguageFiles(); | ||
|
||
for (var i = 0; i < project.GetSourceLanguageFiles().Length; i++) | ||
{ | ||
Guid[] currentFileId = { files[i].Id }; | ||
|
||
var currentFileName = files[i].Name; | ||
project.SetFileRole(currentFileId, FileRole.Translatable); | ||
} | ||
} | ||
|
||
private static string GetDateTimeToString(DateTime dateTime) | ||
{ | ||
var value = dateTime.Year + | ||
dateTime.Month.ToString().PadLeft(2, '0') + | ||
dateTime.Day.ToString().PadLeft(2, '0') + | ||
"-" + | ||
dateTime.Hour.ToString().PadLeft(2, '0') + | ||
dateTime.Minute.ToString().PadLeft(2, '0') + | ||
dateTime.Second.ToString().PadLeft(2, '0'); | ||
return value; | ||
} | ||
|
||
private static string GetOutputProjectDirectory(string baseFolder) | ||
{ | ||
var dateTimeString = GetDateTimeToString(DateTime.Now); | ||
var projectDirectory = Path.Combine(baseFolder, dateTimeString); | ||
|
||
if (!Directory.Exists(projectDirectory)) | ||
Directory.CreateDirectory(projectDirectory); | ||
|
||
return projectDirectory; | ||
} | ||
|
||
private static void Main(string[] args) | ||
{ | ||
// Specify the directory where new translation projects will be saved. | ||
var projectsDirectory = @""; | ||
|
||
// Provide the path to the project template that includes the Google Translate provider configuration. | ||
var templatePath = @""; // Place the project template that contains the Google Translate provider path | ||
|
||
var sourceLanguage = "en-US"; | ||
var targetLanguage = "de-DE"; | ||
|
||
if (!Directory.Exists(projectsDirectory)) | ||
Directory.CreateDirectory(projectsDirectory); | ||
var project = CreateProject(projectsDirectory, sourceLanguage, targetLanguage, templatePath); | ||
|
||
UpdateTranslationConfiguration(project); | ||
|
||
var tpUriString = "googletranslationprovider:///"; | ||
if (string.IsNullOrWhiteSpace(templatePath)) AddTranslationProviderToProject(templatePath, project, tpUriString); | ||
|
||
// Comment out this line if using Google Translate API V3, as it uses a different authentication method. | ||
// Otherwise, add the API key as a string in place of "" to enable Google Translate API V2 authentication. | ||
project.Credentials.AddCredential(new Uri(tpUriString), ""); | ||
|
||
// Specify the directory containing files to translate, and add these files to the project. | ||
project.Save(); | ||
|
||
AddFilesToProject(project, @""); // Place the translatable files location | ||
|
||
RunTasks(project, targetLanguage); | ||
|
||
project.Save(); | ||
|
||
Console.ReadLine(); | ||
} | ||
|
||
private static void AddTranslationProviderToProject(string templatePath, FileBasedProject project, | ||
string tpUriString) | ||
{ | ||
var tpConfig = project.GetTranslationProviderConfiguration(); | ||
|
||
var tpReference = new TranslationProviderReference(new Uri(tpUriString), null, true); | ||
var tpCascadeEntry = new TranslationProviderCascadeEntry(tpReference, true, true, false); | ||
tpConfig.Entries.Add(tpCascadeEntry); | ||
project.UpdateTranslationProviderConfiguration(tpConfig); | ||
} | ||
|
||
private static FileBasedProject CreateProject(string projectsDirectory, string sourceLanguage, string targetLanguage, | ||
string templatePath) | ||
{ | ||
var projectDirectory = GetOutputProjectDirectory(projectsDirectory); | ||
|
||
var projectName = "TestProject-" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + | ||
DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second; | ||
|
||
var projectInfo = new ProjectInfo | ||
{ | ||
Name = projectName, | ||
SourceLanguage = new Language(sourceLanguage), | ||
TargetLanguages = new[] { new Language(targetLanguage) }, | ||
LocalProjectFolder = projectDirectory | ||
}; | ||
|
||
FileBasedProject project; | ||
if (!string.IsNullOrEmpty(templatePath)) | ||
{ | ||
var projectTemplateReference = new ProjectTemplateReference(templatePath); | ||
project = new FileBasedProject(projectInfo, projectTemplateReference); | ||
} | ||
else project = new FileBasedProject(projectInfo); | ||
|
||
return project; | ||
} | ||
|
||
private static void RunAnalyzeTaskFiles(IProject project, string targetLanguage) | ||
{ | ||
var targetFiles = project.GetTargetLanguageFiles(Sdl.Core.Globalization.LanguageRegistry.LanguageRegistryApi.Instance.GetLanguage(targetLanguage)); | ||
|
||
var task = project.RunAutomaticTask( | ||
targetFiles.GetIds(), | ||
AutomaticTaskTemplateIds.AnalyzeFiles | ||
); | ||
|
||
Console.WriteLine("RunAnalyzeTaskFiles: " + task.Status); | ||
} | ||
|
||
private static void RunPreTranslateFiles(IProject project, string targetLanguage) | ||
{ | ||
var targetFiles = project.GetTargetLanguageFiles(Sdl.Core.Globalization.LanguageRegistry.LanguageRegistryApi.Instance.GetLanguage(targetLanguage)); | ||
|
||
var task = project.RunAutomaticTask( | ||
targetFiles.GetIds(), | ||
AutomaticTaskTemplateIds.PreTranslateFiles); | ||
|
||
Console.WriteLine("RunPreTranslateFiles: " + task.Status); | ||
} | ||
|
||
private static void RunScanTaskFiles(IProject project) | ||
{ | ||
var sourceFiles = project.GetSourceLanguageFiles(); | ||
var sourceFilesIds = sourceFiles.GetIds(); | ||
|
||
var scanTask = project.RunAutomaticTask( | ||
sourceFiles.GetIds(), | ||
AutomaticTaskTemplateIds.Scan); | ||
|
||
Console.WriteLine("RunScanTaskFiles: " + scanTask.Status); | ||
|
||
var convertTask = project.RunAutomaticTask( | ||
sourceFilesIds, | ||
AutomaticTaskTemplateIds.ConvertToTranslatableFormat); | ||
|
||
Console.WriteLine("RunConvertTaskFiles: " + convertTask.Status); | ||
|
||
var splitTask = project.RunAutomaticTask( | ||
sourceFilesIds, | ||
AutomaticTaskTemplateIds.CopyToTargetLanguages); | ||
|
||
Console.WriteLine("RunSplitTaskFiles: " + splitTask.Status); | ||
} | ||
|
||
private static void RunTasks(FileBasedProject project, string targetLanguage) | ||
{ | ||
RunScanTaskFiles(project); | ||
RunAnalyzeTaskFiles(project, targetLanguage); | ||
RunPreTranslateFiles(project, targetLanguage); | ||
} | ||
|
||
private static void UpdateTranslationConfiguration(FileBasedProject project) | ||
{ | ||
var settings = project.GetSettings(); | ||
var preTranslateSettings = settings.GetSettingsGroup<TranslateTaskSettings>(); | ||
preTranslateSettings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation; | ||
preTranslateSettings.MinimumMatchScore.Value = 75; | ||
|
||
project.UpdateSettings(settings); | ||
project.Save(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...eProvider/StandAloneConsoleApp_PreTranslateUsingGoogleProvider/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("StandAloneConsoleApp_PreTranslateUsingGoogleProvider")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("SDL")] | ||
[assembly: AssemblyProduct("StandAloneConsoleApp_PreTranslateUsingGoogleProvider")] | ||
[assembly: AssemblyCopyright("Copyright © SDL 2024")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("ae2ac3d0-a442-4722-aa2e-f847d8d72953")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
55 changes: 55 additions & 0 deletions
55
...eTranslateUsingGoogleProvider/StandAloneConsoleApp_PreTranslateUsingGoogleProvider.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net48</TargetFramework> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
<CreatePluginPackage>true</CreatePluginPackage> | ||
<AssemblyOriginatorKeyFile>..\..\SdlCommunity.snk</AssemblyOriginatorKeyFile> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
<EmbeddedResourceUseDependentUponConvention>true</EmbeddedResourceUseDependentUponConvention> | ||
<SignAssembly>true</SignAssembly> | ||
<TradosFolder>$(MSBuildProgramFiles32)\Trados\Trados Studio\Studio18</TradosFolder> | ||
<OutputType>Exe</OutputType> | ||
<OutputPath>$(TradosFolder)\</OutputPath> | ||
<PlatformTarget>x86</PlatformTarget> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Sdl.Core.Globalization"> | ||
<HintPath>$(TradosFolder)\Sdl.Core.Globalization.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Sdl.Core.Globalization.Async"> | ||
<HintPath>$(TradosFolder)\Sdl.Core.Globalization.Async.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Sdl.Core.Settings"> | ||
<HintPath>$(TradosFolder)\Sdl.Core.Settings.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Sdl.LanguagePlatform.TranslationMemoryApi"> | ||
<HintPath>$(TradosFolder)\Sdl.LanguagePlatform.TranslationMemoryApi.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Sdl.ProjectAutomation.Core"> | ||
<HintPath>$(TradosFolder)\Sdl.ProjectAutomation.Core.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Sdl.ProjectAutomation.FileBased"> | ||
<HintPath>$(TradosFolder)\Sdl.ProjectAutomation.FileBased.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Sdl.ProjectAutomation.Settings"> | ||
<HintPath>$(TradosFolder)\Sdl.ProjectAutomation.Settings.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Sdl.LanguageCloud.IdentityApi"> | ||
<HintPath>$(TradosFolder)\Sdl.LanguageCloud.IdentityApi.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Newtonsoft.Json"> | ||
<HintPath>$(TradosFolder)\Newtonsoft.Json.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
</Project> |