1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Collections . ObjectModel ;
4
- using System . Diagnostics ;
5
4
using System . IO ;
6
5
using System . Linq ;
7
6
using System . Text . RegularExpressions ;
8
7
using Google ;
8
+ using Google . Apis . Auth . OAuth2 ;
9
9
using Google . Cloud . Translation . V2 ;
10
10
using Happy_Apps_Core ;
11
11
using Happy_Apps_Core . Translation ;
@@ -16,7 +16,7 @@ namespace HRGoogleTranslate
16
16
[ UsedImplicitly ]
17
17
public class GoogleTranslateApi : ITranslator
18
18
{
19
- private const string CredentialPropertyKey = "Credential Location " ;
19
+ private const string CredentialPropertyKey = "API Key / File Path " ;
20
20
private const string ModelPropertyKey = "Translation Model" ;
21
21
private const string BadRepetitionKey = "Prevent Bad Repetition" ;
22
22
private const string TargetLanguage = "en" ;
@@ -35,13 +35,13 @@ public class GoogleTranslateApi : ITranslator
35
35
{ ModelPropertyKey , typeof ( TranslationModel ) } ,
36
36
{ BadRepetitionKey , typeof ( bool ) }
37
37
} ) ;
38
-
38
+
39
39
public void Initialise ( )
40
40
{
41
41
Error = null ;
42
42
try
43
43
{
44
- SetGoogleCredential ( Settings . GoogleCredentialPath ) ;
44
+ SetGoogleCredential ( Settings . GoogleCredential ) ;
45
45
}
46
46
catch ( Exception ex )
47
47
{
@@ -125,7 +125,7 @@ public object GetProperty(string propertyKey)
125
125
{
126
126
return propertyKey switch
127
127
{
128
- CredentialPropertyKey => Settings . GoogleCredentialPath ,
128
+ CredentialPropertyKey => Settings . GoogleCredential ,
129
129
ModelPropertyKey => Settings . TranslationModel ,
130
130
BadRepetitionKey => Settings . PreventBadRepetition ,
131
131
_ => throw new ArgumentOutOfRangeException ( nameof ( propertyKey ) , $ "Unrecognized property key: { propertyKey } ")
@@ -142,16 +142,14 @@ public void SaveProperties(string filePath)
142
142
//done automatically whenever a property changes.
143
143
}
144
144
145
- private void SetGoogleCredential ( string credentialPath )
145
+ private void SetGoogleCredential ( string credential )
146
146
{
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." ) ;
150
149
try
151
150
{
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 ;
155
153
}
156
154
catch ( Exception ex )
157
155
{
@@ -161,18 +159,17 @@ private void SetGoogleCredential(string credentialPath)
161
159
162
160
private class ApiSettings : SettingsJsonFile
163
161
{
164
- // ReSharper disable once StringLiteralTypo
165
- private string _googleCredentialPath = @"C:\Google\hrtranslate-credential.json" ;
162
+ private string _googleCredential = @"" ;
166
163
private TranslationModel _translationModel = TranslationModel . NeuralMachineTranslation ;
167
164
private bool _preventBadRepetition = true ;
168
165
169
- public string GoogleCredentialPath
166
+ public string GoogleCredential
170
167
{
171
- get => _googleCredentialPath ;
168
+ get => _googleCredential ;
172
169
set
173
170
{
174
- if ( _googleCredentialPath == value ) return ;
175
- _googleCredentialPath = value ;
171
+ if ( _googleCredential == value ) return ;
172
+ _googleCredential = value ;
176
173
if ( Loaded ) Save ( ) ;
177
174
}
178
175
}
0 commit comments