Skip to content

Commit

Permalink
allow choosing the region when configuring the Microsoft Speech API a…
Browse files Browse the repository at this point in the history
…ccount
  • Loading branch information
louangr committed Aug 19, 2023
1 parent bd0a996 commit 0285590
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 10 deletions.
15 changes: 12 additions & 3 deletions Translator/Translator/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public sealed partial class MainWindow : Window

private static readonly string AZURE_LANGUAGE_RESOURCE_FILE_PATH = $"{AppDomain.CurrentDomain.BaseDirectory}PrebuiltNeuralVoicesAzureSpeech.json";

private static readonly string SPEECH_SERVICE_REGIONS_FILE_PATH = $"{AppDomain.CurrentDomain.BaseDirectory}SpeechServiceRegions.json";

#endregion Private fields

public MainWindow()
Expand All @@ -36,9 +38,16 @@ public MainWindow()

private void Initialize()
{
var streamReader = new StreamReader(AZURE_LANGUAGE_RESOURCE_FILE_PATH);
var jsonString = streamReader.ReadToEnd();
var prebuiltNeuralVoices = JsonConvert.DeserializeObject<List<PrebuiltNeuralVoice>>(jsonString).DistinctBy(v => v.Locale);
var speechServiceRegionsStreamReader = new StreamReader(SPEECH_SERVICE_REGIONS_FILE_PATH);
var speechServiceRegionsJsonString = speechServiceRegionsStreamReader.ReadToEnd();
var speechServiceRegions = JsonConvert.DeserializeObject<List<SpeechServiceRegion>>(speechServiceRegionsJsonString);

(App.Current.Services.GetService(typeof(SettingsPageViewModel)) as SettingsPageViewModel).SpeechServiceRegions = speechServiceRegions;

var azureLanguageResourceStreamReader = new StreamReader(AZURE_LANGUAGE_RESOURCE_FILE_PATH);
var azureLanguageResourceJsonString = azureLanguageResourceStreamReader.ReadToEnd();
var prebuiltNeuralVoices = JsonConvert.DeserializeObject<List<PrebuiltNeuralVoice>>(azureLanguageResourceJsonString).DistinctBy(v => v.Locale);

Navigate(typeof(HomePage), prebuiltNeuralVoices);
}

Expand Down
3 changes: 3 additions & 0 deletions Translator/Translator/Models/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ public class Account

[DataMember(Name = "apiKey")]
public string ApiKey { get; set; }

[DataMember(Name = "region")]
public string Region { get; set; }
}
}
14 changes: 14 additions & 0 deletions Translator/Translator/Models/SpeechServiceRegion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Runtime.Serialization;

namespace Translator.Models
{
[DataContract]
public class SpeechServiceRegion
{
[DataMember(Name = "id")]
public string Id { get; set; }

[DataMember(Name = "name")]
public string Name { get; set; }
}
}
122 changes: 122 additions & 0 deletions Translator/Translator/SpeechServiceRegions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[
{
"id": "australiaeast",
"name": "Australia East"
},
{
"id": "brazilsouth",
"name": "Brazil South"
},
{
"id": "canadacentral",
"name": "Canada Central"
},
{
"id": "centralindia",
"name": "Central India"
},
{
"id": "centralus",
"name": "Central US"
},
{
"id": "eastasia",
"name": "East Asia"
},
{
"id": "eastus",
"name": "East US"
},
{
"id": "eastus2",
"name": "East US 2"
},
{
"id": "francecentral",
"name": "France Central"
},
{
"id": "germanywestcentral",
"name": "Germany West Central"
},
{
"id": "japaneast",
"name": "Japan East"
},
{
"id": "japanwest",
"name": "Japan West"
},
{
"id": "koreacentral",
"name": "Korea Central"
},
{
"id": "northcentralus",
"name": "North Central US"
},
{
"id": "northeurope",
"name": "North Europe"
},
{
"id": "norwayeast",
"name": "Norway East"
},
{
"id": "qatarcentral",
"name": "Qatar Central"
},
{
"id": "southafricanorth",
"name": "South Africa North"
},
{
"id": "southcentralus",
"name": "South Central US"
},
{
"id": "southeastasia",
"name": "Southeast Asia"
},
{
"id": "swedencentral",
"name": "Sweden Central"
},
{
"id": "switzerlandnorth",
"name": "Switzerland North"
},
{
"id": "switzerlandwest",
"name": "Switzerland West"
},
{
"id": "uaenorth",
"name": "UAE North"
},
{
"id": "uksouth",
"name": "UK South"
},
{
"id": "westcentralus",
"name": "West Central US"
},
{
"id": "westeurope",
"name": "West Europe"
},
{
"id": "westus",
"name": "West US"
},
{
"id": "westus2",
"name": "West US 2"
},
{
"id": "westus3",
"name": "West US 3"
}
]
3 changes: 3 additions & 0 deletions Translator/Translator/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@
<data name="SettingsPage_PageNameTextBlock.Text" xml:space="preserve">
<value>Settings</value>
</data>
<data name="SettingsPage_RegionComboBox.PlaceholderText" xml:space="preserve">
<value>Region</value>
</data>
</root>
3 changes: 3 additions & 0 deletions Translator/Translator/Strings/fr-FR/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@
<data name="SettingsPage_PageNameTextBlock.Text" xml:space="preserve">
<value>Paramètres</value>
</data>
<data name="SettingsPage_RegionComboBox.PlaceholderText" xml:space="preserve">
<value>Région</value>
</data>
</root>
3 changes: 3 additions & 0 deletions Translator/Translator/Translator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<None Update="PrebuiltNeuralVoicesAzureSpeech.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SpeechServiceRegions.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Page Update="Views\TranslationPage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
13 changes: 11 additions & 2 deletions Translator/Translator/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,25 @@
<RowDefinition />
<RowDefinition Height="8" />
<RowDefinition />
<RowDefinition Height="8" />
<RowDefinition />
</Grid.RowDefinitions>

<TextBox x:Uid="SettingsPage_ApiKeyTextBox"
Grid.ColumnSpan="3"
Text="{x:Bind PageViewModel.ApiKeyInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Uid="SettingsPage_AccountNameTextBox"
Grid.Row="3"
Grid.Row="2"
Grid.ColumnSpan="3"
Text="{x:Bind PageViewModel.AccountNameInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox x:Uid="SettingsPage_RegionComboBox"
Grid.Row="4"
HorizontalAlignment="Stretch"
ItemsSource="{x:Bind PageViewModel.SpeechServiceRegions}"
DisplayMemberPath="Name"
SelectedValue="{Binding SelectedSpeechServiceRegion, Mode=TwoWay}" />
<Button x:Uid="SettingsPage_AddAccountButton"
Grid.Row="3"
Grid.Row="4"
Grid.Column="3"
Command="{x:Bind PageViewModel.AddAccountCommand}" />
</Grid>
Expand Down
32 changes: 29 additions & 3 deletions Translator/Translator/Views/SettingsPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Xaml.Controls.Primitives;
using Translator.Core;
using Translator.Models;
using Translator.Repositories.Interfaces;
Expand All @@ -17,6 +19,8 @@ public class SettingsPageViewModel : CorePageViewModel
private RelayCommand addAccountCommand;
private IAccountsRepository accountsRepository;
private ISettingsRepository settingsRepository;
private List<SpeechServiceRegion> speechServiceRegions;
private SpeechServiceRegion selectedSpeechServiceRegion;

#endregion Private fields

Expand Down Expand Up @@ -62,7 +66,28 @@ public Account SelectedAccount
}
}

public bool IsAddAccountButtonEnabled => ApiKeyInput != null && ApiKeyInput.Trim() != string.Empty && AccountNameInput != null && AccountNameInput.Trim() != string.Empty && !Accounts.Select((s, e) => s.Name).Contains(AccountNameInput.Trim());
public List<SpeechServiceRegion> SpeechServiceRegions
{
get => speechServiceRegions;
set
{
speechServiceRegions = value;
OnPropertyChanged(nameof(SpeechServiceRegions));
}
}

public SpeechServiceRegion SelectedSpeechServiceRegion
{
get => selectedSpeechServiceRegion;
set
{
selectedSpeechServiceRegion = value;
OnPropertyChanged(nameof(SelectedSpeechServiceRegion));
AddAccountCommand.NotifyCanExecuteChanged();
}
}

public bool IsAddAccountButtonEnabled => ApiKeyInput != null && ApiKeyInput.Trim() != string.Empty && AccountNameInput != null && AccountNameInput.Trim() != string.Empty && SelectedSpeechServiceRegion != null && !Accounts.Select((s, e) => s.Name).Contains(AccountNameInput.Trim());

public bool IsAccountsListViewVisible => Accounts != null && Accounts.Count > 0;

Expand Down Expand Up @@ -95,9 +120,10 @@ public void RemoveAccount(string accountName)

private void AddAccount()
{
Accounts.Add(new Account() { Name = AccountNameInput, ApiKey = ApiKeyInput });
Accounts.Add(new Account() { Name = AccountNameInput, ApiKey = ApiKeyInput, Region = SelectedSpeechServiceRegion.Id });
ApiKeyInput = string.Empty;
AccountNameInput = string.Empty;
SelectedSpeechServiceRegion = null;
OnPropertyChanged(nameof(Accounts));
OnPropertyChanged(nameof(IsAccountsListViewVisible));
}
Expand Down
3 changes: 1 addition & 2 deletions Translator/Translator/Views/TranslationPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class TranslationPageViewModel : CorePageViewModel
private string sourceLanguageCurrentText;
private string targetLanguageText;
private string targetLanguageCurrentText;
private int textSize;

#endregion Private fields

Expand Down Expand Up @@ -154,7 +153,7 @@ private async Task StartTranslationAsync()

private void CreateRecognizer()
{
var config = SpeechTranslationConfig.FromSubscription(settingsRepository.SelectedAccount.ApiKey, "francecentral");
var config = SpeechTranslationConfig.FromSubscription(settingsRepository.SelectedAccount.ApiKey, settingsRepository.SelectedAccount.Region);
config.SpeechRecognitionLanguage = settingsRepository.SourceTranslationLanguage;
config.AddTargetLanguage(settingsRepository.TargetTranslationLanguage);

Expand Down

0 comments on commit 0285590

Please sign in to comment.