Skip to content

Commit b7e9811

Browse files
committed
Google Translate API: Allow API key as well as JSON file for credentials.
1 parent 142ac52 commit b7e9811

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

HRGoogleTranslate/GoogleTranslateApi.cs

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4-
using System.Diagnostics;
54
using System.IO;
65
using System.Linq;
76
using System.Text.RegularExpressions;
87
using Google;
8+
using Google.Apis.Auth.OAuth2;
99
using Google.Cloud.Translation.V2;
1010
using Happy_Apps_Core;
1111
using Happy_Apps_Core.Translation;
@@ -16,7 +16,7 @@ namespace HRGoogleTranslate
1616
[UsedImplicitly]
1717
public class GoogleTranslateApi : ITranslator
1818
{
19-
private const string CredentialPropertyKey = "Credential Location";
19+
private const string CredentialPropertyKey = "API Key / File Path";
2020
private const string ModelPropertyKey = "Translation Model";
2121
private const string BadRepetitionKey = "Prevent Bad Repetition";
2222
private const string TargetLanguage = "en";
@@ -35,13 +35,13 @@ public class GoogleTranslateApi : ITranslator
3535
{ModelPropertyKey, typeof(TranslationModel)},
3636
{BadRepetitionKey, typeof(bool)}
3737
});
38-
38+
3939
public void Initialise()
4040
{
4141
Error = null;
4242
try
4343
{
44-
SetGoogleCredential(Settings.GoogleCredentialPath);
44+
SetGoogleCredential(Settings.GoogleCredential);
4545
}
4646
catch (Exception ex)
4747
{
@@ -125,7 +125,7 @@ public object GetProperty(string propertyKey)
125125
{
126126
return propertyKey switch
127127
{
128-
CredentialPropertyKey => Settings.GoogleCredentialPath,
128+
CredentialPropertyKey => Settings.GoogleCredential,
129129
ModelPropertyKey => Settings.TranslationModel,
130130
BadRepetitionKey => Settings.PreventBadRepetition,
131131
_ => throw new ArgumentOutOfRangeException(nameof(propertyKey), $"Unrecognized property key: {propertyKey}")
@@ -142,16 +142,14 @@ public void SaveProperties(string filePath)
142142
//done automatically whenever a property changes.
143143
}
144144

145-
private void SetGoogleCredential(string credentialPath)
145+
private void SetGoogleCredential(string credential)
146146
{
147-
Settings.GoogleCredentialPath = credentialPath;
148-
if (string.IsNullOrWhiteSpace(credentialPath)) throw new ArgumentNullException(credentialPath, "Google Credential path was empty.");
149-
if (!File.Exists(credentialPath)) throw new FileNotFoundException("Google Credential file not found", credentialPath);
147+
Settings.GoogleCredential = credential;
148+
if (string.IsNullOrWhiteSpace(credential)) throw new ArgumentNullException(credential, "Google Credential was empty.");
150149
try
151150
{
152-
Debug.Assert(credentialPath != null, nameof(credentialPath) + " != null");
153-
using var stream = File.OpenRead(credentialPath);
154-
_client = TranslationClient.Create(Google.Apis.Auth.OAuth2.GoogleCredential.FromStream(stream));
151+
_client = File.Exists(credential) ? TranslationClient.Create(GoogleCredential.FromFile(credential)) : TranslationClient.CreateFromApiKey(credential, Settings.TranslationModel);
152+
Error = null;
155153
}
156154
catch (Exception ex)
157155
{
@@ -161,18 +159,17 @@ private void SetGoogleCredential(string credentialPath)
161159

162160
private class ApiSettings : SettingsJsonFile
163161
{
164-
// ReSharper disable once StringLiteralTypo
165-
private string _googleCredentialPath = @"C:\Google\hrtranslate-credential.json";
162+
private string _googleCredential = @"";
166163
private TranslationModel _translationModel = TranslationModel.NeuralMachineTranslation;
167164
private bool _preventBadRepetition = true;
168165

169-
public string GoogleCredentialPath
166+
public string GoogleCredential
170167
{
171-
get => _googleCredentialPath;
168+
get => _googleCredential;
172169
set
173170
{
174-
if (_googleCredentialPath == value) return;
175-
_googleCredentialPath = value;
171+
if (_googleCredential == value) return;
172+
_googleCredential = value;
176173
if (Loaded) Save();
177174
}
178175
}

Happy Reader/Model/Translator.cs

+5
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,11 @@ private void SetTranslationAndSaveToCache(StringBuilder text, string translated,
657657
private void Translate(StringBuilder text)
658658
{
659659
if (TryGetWithoutApi(_useAnyCached ? null : SelectedTranslator.SourceName, text, false, out var input)) return;
660+
if (!string.IsNullOrWhiteSpace(SelectedTranslator.Error))
661+
{
662+
text.Append(SelectedTranslator.Error);
663+
return;
664+
}
660665
var success = SelectedTranslator.Translate(input, out var translated);
661666
if (success) SetTranslationAndSaveToCache(text, translated, input, SelectedTranslator.SourceName);
662667
else text.Append(translated);

0 commit comments

Comments
 (0)