feat: add translate api to view

dev_without_third_libraries
DESKTOP-3BO4HSG\ksat 2 years ago
parent dfe36cec41
commit 2f638524a2

@ -106,7 +106,7 @@
<!--ComBoxItem--> <!--ComBoxItem-->
<Style TargetType="ComboBoxItem"> <Style TargetType="ComboBoxItem">
<Setter Property="MinHeight" Value="22"></Setter> <Setter Property="MinHeight" Value="22"></Setter>
<Setter Property="MinWidth" Value="80"></Setter> <Setter Property="MinWidth" Value="40"></Setter>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="ComboBoxItem"> <ControlTemplate TargetType="ComboBoxItem">
@ -133,7 +133,7 @@
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}"> <ControlTemplate TargetType="{x:Type ComboBox}">
<Border BorderThickness="0" CornerRadius="5" Width="100" Height="30" Background="{TemplateBinding Background}" > <Border BorderThickness="0" CornerRadius="5" Width="80" Height="30" Background="{TemplateBinding Background}" >
<Grid> <Grid>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/> <ColumnDefinition Width="3*"/>
@ -302,6 +302,12 @@
</ControlTemplate> </ControlTemplate>
</Button.Template> </Button.Template>
</Button> </Button>
<ComboBox Style="{StaticResource cmbstyle}"
Margin="0,5,10,0"
ItemsSource="{Binding TranslationInterface}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedTranslationInterface}"
HorizontalAlignment="Right"/>
</Grid> </Grid>
<!--input--> <!--input-->

@ -19,7 +19,6 @@ namespace STranslate.ViewModel
//[System.Runtime.InteropServices.DllImport("deepl.dll", EntryPoint = "run")] //[System.Runtime.InteropServices.DllImport("deepl.dll", EntryPoint = "run")]
//extern static void run(); //extern static void run();
public string defaultApi = "https://zggsong.cn/tt"; //https://deepl.deno.dev/translate、https://zggsong.cn/tt、http://127.0.0.1:8000/translate
private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); } private static Dictionary<string, LanguageEnum> LanguageEnumDict { get => TranslateUtil.GetEnumList<LanguageEnum>(); }
public MainVM() public MainVM()
@ -30,6 +29,9 @@ namespace STranslate.ViewModel
OutputCombo = LanguageEnumDict.Keys.ToList(); OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.AUTO.GetDescription(); OutputComboSelected = LanguageEnum.AUTO.GetDescription();
//初始化接口
SelectedTranslationInterface = TranslationInterface[0];
//复制输入 //复制输入
CopyInputCmd = new RelayCommand((_) => true, (_) => CopyInputCmd = new RelayCommand((_) => true, (_) =>
{ {
@ -103,11 +105,11 @@ namespace STranslate.ViewModel
{ {
var autoRet = AutomaticLanguageRecognition(InputTxt); var autoRet = AutomaticLanguageRecognition(InputTxt);
IdentifyLanguage = autoRet.Item1; IdentifyLanguage = autoRet.Item1;
translateResp = await TranslateUtil.TranslateDeepLAsync(defaultApi, InputTxt, LanguageEnumDict[autoRet.Item2], LanguageEnumDict[InputComboSelected]); translateResp = await TranslateUtil.TranslateDeepLAsync(SelectedTranslationInterface.Api, InputTxt, LanguageEnumDict[autoRet.Item2], LanguageEnumDict[InputComboSelected]);
} }
else else
{ {
translateResp = await TranslateUtil.TranslateDeepLAsync(defaultApi, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); translateResp = await TranslateUtil.TranslateDeepLAsync(SelectedTranslationInterface.Api, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
} }
//百度 Api //百度 Api
@ -238,6 +240,38 @@ namespace STranslate.ViewModel
private string _OutputComboSelected; private string _OutputComboSelected;
public string OutputComboSelected { get => _OutputComboSelected; set => UpdateProperty(ref _OutputComboSelected, value); } public string OutputComboSelected { get => _OutputComboSelected; set => UpdateProperty(ref _OutputComboSelected, value); }
/// <summary>
/// 目标接口
/// </summary>
private List<TranslationInterface> _TranslationInterface = new List<TranslationInterface>
{
new TranslationInterface
{
Name = "zu1k",
Api = "https://deepl.deno.dev/translate"
},
new TranslationInterface
{
Name = "zggsong",
Api = "https://zggsong.cn/tt"
},
new TranslationInterface
{
Name = "local",
Api = "http://127.0.0.1:8000/translate"
}
};
public List<TranslationInterface> TranslationInterface { get => _TranslationInterface; set => UpdateProperty(ref _TranslationInterface, value); }
private TranslationInterface _SelectedTranslationInterface;
public TranslationInterface SelectedTranslationInterface { get => _SelectedTranslationInterface; set => UpdateProperty(ref _SelectedTranslationInterface, value); }
#endregion Params #endregion Params
} }
public class TranslationInterface
{
public string Name { get; set; }
public string Api { get; set; }
}
} }
Loading…
Cancel
Save