feat: add English and Chinese language recognition

dev_without_third_libraries
DESKTOP-3BO4HSG\ksat 2 years ago
parent 8a28b52340
commit cf420e72f3

@ -106,6 +106,8 @@
<KeyBinding Command="{Binding TranslateCmd}" Key="Enter" /> <KeyBinding Command="{Binding TranslateCmd}" Key="Enter" />
</TextBox.InputBindings> </TextBox.InputBindings>
</TextBox> </TextBox>
<StackPanel Orientation="Horizontal"
Margin="5">
<Button Cursor="Hand" <Button Cursor="Hand"
Margin="5,0,0,5" Margin="5,0,0,5"
HorizontalAlignment="Left" HorizontalAlignment="Left"
@ -119,6 +121,21 @@
</DataTemplate> </DataTemplate>
</hc:FloatingBlock.ContentTemplate> </hc:FloatingBlock.ContentTemplate>
</Button> </Button>
<Border Margin="10,0,0,8"
Background="#eaeaea"
CornerRadius="10"
Visibility="{Binding IdentifyLanguage,Converter={StaticResource String2VisibilityConverter}}"
HorizontalAlignment="Left"
VerticalAlignment="Bottom">
<StackPanel Margin="5,2"
Orientation="Horizontal">
<TextBlock Text="识别为 "
Foreground="#999999"/>
<TextBlock Text="{Binding IdentifyLanguage}"
Foreground="#3478f6"/>
</StackPanel>
</Border>
</StackPanel>
</Grid> </Grid>
</Border> </Border>
</Grid> </Grid>

@ -178,8 +178,10 @@ namespace STranslate
{ {
vm.InputTxt = string.Empty; vm.InputTxt = string.Empty;
vm.OutputTxt = string.Empty; vm.OutputTxt = string.Empty;
vm.SmallHumpRet = string.Empty;
vm.SnakeRet = string.Empty; vm.SnakeRet = string.Empty;
vm.SmallHumpRet = string.Empty;
vm.LargeHumpRet = string.Empty;
vm.IdentifyLanguage = string.Empty;
} }
/// <summary> /// <summary>

@ -47,6 +47,6 @@ using System.Windows;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示: //通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.5.0")] [assembly: AssemblyVersion("0.0.6.0")]
[assembly: AssemblyFileVersion("0.0.5.0")] [assembly: AssemblyFileVersion("0.0.6.0")]
[assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")] [assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")]

@ -32,7 +32,7 @@ namespace STranslate.ViewModel
InputCombo = LanguageEnumDict.Keys.ToList(); InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription(); InputComboSelected = LanguageEnum.AUTO.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList(); OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.EN.GetDescription(); OutputComboSelected = LanguageEnum.AUTO.GetDescription();
config = ConfigUtil.ReadConfig(ConfigPath); config = ConfigUtil.ReadConfig(ConfigPath);
@ -73,17 +73,47 @@ namespace STranslate.ViewModel
} }
#region handle #region handle
/// <summary>
/// 自动识别语种
/// </summary>
/// <param name="text">输入语言</param>
/// <returns>
/// Item1: SourceLang
/// Item2: TargetLang
/// </returns>
private Tuple<string, string> AutomaticLanguageRecognition(string text)
{
//如果输入是中文
if (System.Text.RegularExpressions.Regex.IsMatch(text, @"^[\u4e00-\u9fa5]+$"))
{
return new Tuple<string, string>(LanguageEnum.ZH.GetDescription(), LanguageEnum.EN.GetDescription());
}
else
{
return new Tuple<string, string>(LanguageEnum.EN.GetDescription(), LanguageEnum.ZH.GetDescription());
}
}
/// <summary>
/// 翻译
/// </summary>
/// <returns></returns>
public async Task Translate() public async Task Translate()
{ {
try try
{ {
OutputTxt = "翻译中..."; OutputTxt = "翻译中...";
//获取结果 //自动选择目标语言
if (OutputComboSelected == LanguageEnum.AUTO.GetDescription())
//DeepL Api {
var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); var autoRet = AutomaticLanguageRecognition(InputTxt);
IdentifyLanguage = autoRet.Item1;
translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[autoRet.Item2], LanguageEnumDict[InputComboSelected]);
}
else
{
translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
}
//百度 Api //百度 Api
//var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); //var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
@ -96,7 +126,10 @@ namespace STranslate.ViewModel
OutputTxt = translateResp; OutputTxt = translateResp;
//如果不是英文则不进行转换 //如果不是英文则不进行转换
if (LanguageEnumDict[OutputComboSelected] != LanguageEnum.EN) return; if (AutomaticLanguageRecognition(InputTxt).Item2 != LanguageEnum.EN.GetDescription() && LanguageEnumDict[OutputComboSelected] != LanguageEnum.EN)
{
return;
}
var splitList = OutputTxt.Split(' ').ToList(); var splitList = OutputTxt.Split(' ').ToList();
if (splitList.Count > 1) if (splitList.Count > 1)
@ -157,7 +190,7 @@ namespace STranslate.ViewModel
#endregion handle #endregion handle
#region Params #region Params
private string translateResp;
public ICommand TranslateCmd { get; private set; } public ICommand TranslateCmd { get; private set; }
public ICommand CopyInputCmd { get; private set; } public ICommand CopyInputCmd { get; private set; }
public ICommand CopyResultCmd { get; private set; } public ICommand CopyResultCmd { get; private set; }
@ -165,6 +198,11 @@ namespace STranslate.ViewModel
public ICommand CopySmallHumpResultCmd { get; private set; } public ICommand CopySmallHumpResultCmd { get; private set; }
public ICommand CopyLargeHumpResultCmd { get; private set; } public ICommand CopyLargeHumpResultCmd { get; private set; }
/// <summary>
/// 识别语种
/// </summary>
private string _IdentifyLanguage;
public string IdentifyLanguage { get => _IdentifyLanguage; set => UpdateProperty(ref _IdentifyLanguage, value); }
/// <summary> /// <summary>
/// 构造蛇形结果 /// 构造蛇形结果
/// </summary> /// </summary>

Loading…
Cancel
Save