Skip to content

Commit

Permalink
Microsoft Translation Provider: Make access from standalone apps easier
Browse files Browse the repository at this point in the history
  • Loading branch information
ealbu committed Oct 25, 2024
1 parent c96921e commit 07aa8a7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,4 @@ TMX_TranslationProvider/TMX_TranslationProviderWithUITest.sln
/Jobs/Sdl.Community.Jobs/Properties/launchSettings.json
StarTransit/.editorconfig
DSI Viewer/.editorconfig
MicrosoftTranslatorProvider/.editorconfig
28 changes: 15 additions & 13 deletions MicrosoftTranslatorProvider/ApplicationInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
using System;
using System.Collections.Generic;
using MicrosoftTranslatorProvider.Helpers;
using MicrosoftTranslatorProvider.Helpers;
using MicrosoftTranslatorProvider.Interfaces;
using Sdl.Desktop.IntegrationApi;
using Sdl.Desktop.IntegrationApi.Extensions;
using Sdl.LanguagePlatform.TranslationMemoryApi;
using System;
using System.Collections.Generic;

namespace MicrosoftTranslatorProvider
{
[ApplicationInitializer]
public class ApplicationInitializer : IApplicationInitializer
{
public static ITranslationProviderCredentialStore CredentialStore { get; set; }
[ApplicationInitializer]
public class ApplicationInitializer : IApplicationInitializer
{
public static ITranslationProviderCredentialStore CredentialStore { get; set; }

public static IDictionary<string, ITranslationOptions> TranslationOptions { get; set; } = new Dictionary<string, ITranslationOptions>();
public static IDictionary<string, ITranslationOptions> TranslationOptions { get; set; } = new Dictionary<string, ITranslationOptions>();
public static bool IsStandalone { get; set; } = true;

public void Execute()
{
Log.Setup();
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.CurrentDomain_AssemblyResolve;
}
}
{
IsStandalone = false;
Log.Setup();
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.CurrentDomain_AssemblyResolve;
}
}
}
8 changes: 3 additions & 5 deletions MicrosoftTranslatorProvider/Model/MicrosoftCredentials.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace MicrosoftTranslatorProvider.Model
{
public class MicrosoftCredentials
public class MicrosoftCredentials
{
public string AccessToken { get; set; }
public string APIKey { get; set; }

public string APIKey { get; set; }

public string Region { get; set; }
public string Region { get; set; }
}
}
26 changes: 11 additions & 15 deletions MicrosoftTranslatorProvider/Service/CredentialsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Security.Policy;
using MicrosoftTranslatorProvider.Interfaces;
using MicrosoftTranslatorProvider.Model;
using Newtonsoft.Json;
Expand Down Expand Up @@ -32,24 +33,19 @@ public static void GetCredentials(ITranslationOptions translationOptions)
private static void GetAndAssignCredentials<T>(ITranslationOptions translationOptions, string scheme)
{
var credentialStore = ApplicationInitializer.CredentialStore;
if (credentialStore is null)
{
return;
}

var uri = new Uri(scheme);
var translationProviderCredential = credentialStore.GetCredential(uri);
if (translationProviderCredential is null
|| translationProviderCredential.Credential is not string persistedCredentials)
{
return;
}

var translationProviderCredential = credentialStore?.GetCredential(new Uri(scheme));
if (translationProviderCredential?.Credential is not string persistedCredentials) return;
try
{
var parsedObject = JObject.Parse(persistedCredentials);
var credentials = parsedObject[CredentialsKey].ToString();
AssignCredentials<T>(translationOptions, parsedObject[CredentialsKey].ToString());
string credentials;
if (ApplicationInitializer.IsStandalone) credentials = translationProviderCredential.Credential;
else
{
var parsedObject = JObject.Parse(persistedCredentials);
credentials = parsedObject[CredentialsKey].ToString();
}
AssignCredentials<T>(translationOptions, credentials);
}
catch
{
Expand Down

0 comments on commit 07aa8a7

Please sign in to comment.