feat: add English and Chinese language recognition

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

@ -106,19 +106,36 @@
<KeyBinding Command="{Binding TranslateCmd}" Key="Enter" />
</TextBox.InputBindings>
</TextBox>
<Button Cursor="Hand"
Margin="5,0,0,5"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
<StackPanel Orientation="Horizontal"
Margin="5">
<Button Cursor="Hand"
Margin="5,0,0,5"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Visibility="{Binding InputTxt,Converter={StaticResource String2VisibilityConverter}}"
Command="{Binding CopyInputCmd}">
<Image Source="/Images/copy2.png"/>
<hc:FloatingBlock.ContentTemplate>
<DataTemplate>
<TextBlock Text="copied" />
</DataTemplate>
</hc:FloatingBlock.ContentTemplate>
</Button>
Command="{Binding CopyInputCmd}">
<Image Source="/Images/copy2.png"/>
<hc:FloatingBlock.ContentTemplate>
<DataTemplate>
<TextBlock Text="copied" />
</DataTemplate>
</hc:FloatingBlock.ContentTemplate>
</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>
</Border>
</Grid>

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

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

@ -32,7 +32,7 @@ namespace STranslate.ViewModel
InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.EN.GetDescription();
OutputComboSelected = LanguageEnum.AUTO.GetDescription();
config = ConfigUtil.ReadConfig(ConfigPath);
@ -73,17 +73,47 @@ namespace STranslate.ViewModel
}
#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()
{
try
{
OutputTxt = "翻译中...";
//获取结果
//DeepL Api
var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl?.url ?? defaultApi, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]);
//自动选择目标语言
if (OutputComboSelected == LanguageEnum.AUTO.GetDescription())
{
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
//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;
//如果不是英文则不进行转换
if (LanguageEnumDict[OutputComboSelected] != LanguageEnum.EN) return;
if (AutomaticLanguageRecognition(InputTxt).Item2 != LanguageEnum.EN.GetDescription() && LanguageEnumDict[OutputComboSelected] != LanguageEnum.EN)
{
return;
}
var splitList = OutputTxt.Split(' ').ToList();
if (splitList.Count > 1)
@ -157,7 +190,7 @@ namespace STranslate.ViewModel
#endregion handle
#region Params
private string translateResp;
public ICommand TranslateCmd { get; private set; }
public ICommand CopyInputCmd { get; private set; }
public ICommand CopyResultCmd { get; private set; }
@ -165,6 +198,11 @@ namespace STranslate.ViewModel
public ICommand CopySmallHumpResultCmd { 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>

Loading…
Cancel
Save