Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize language options #56

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build on release
on:
release:
types: created

jobs:
build:
runs-on: windows-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Net
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
cache: true

- name: Dowmload Powertoys
run: |
curl -L https://github.com/microsoft/PowerToys/releases/download/v0.84.0/PowerToysUserSetup-0.84.0-x64.exe -o powertoys.exe
run powertoys.exe -install -quiet -norestart
timeout /t 15
mkdir lib
copy "%LOCALAPPDATA%\PowerToys\PowerToys.Common.UI.dll" "lib\PowerToys.Common.UI.dll"
copy "%LOCALAPPDATA%\PowerToys\PowerToys.ManagedCommon.dll" "lib\PowerToys.ManagedCommon.dll"
copy "%LOCALAPPDATA%\PowerToys\PowerToys.Settings.UI.Lib.dll" "lib\PowerToys.Settings.UI.Lib.dll"
copy "%LOCALAPPDATA%\PowerToys\Wox.Infrastructure.dll" "lib\Wox.Infrastructure.dll"
copy "%LOCALAPPDATA%\PowerToys\Wox.Plugin.dll" "lib\Wox.Plugin.dll"

- name: Package
run: dotnet pack

- name: Upload
uses: actions/upload-artifact@v4
with:
# Artifact name
name: Translator_x64
# A file, directory or wildcard pattern that describes what to upload
path: "bin/out"
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
obj
bin
*.dll
lib

.vs/
src/lib
src/lib
.idea
File renamed without changes.
1 change: 1 addition & 0 deletions Translater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore>
Expand Down
16 changes: 11 additions & 5 deletions lib.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
copy "C:\Program Files\PowerToys\PowerToys.Common.UI.dll" "lib\PowerToys.Common.UI.dll"
copy "C:\Program Files\PowerToys\PowerToys.ManagedCommon.dll" "lib\PowerToys.ManagedCommon.dll"
copy "C:\Program Files\PowerToys\PowerToys.Settings.UI.Lib.dll" "lib\PowerToys.Settings.UI.Lib.dll"
copy "C:\Program Files\PowerToys\Wox.Infrastructure.dll" "lib\Wox.Infrastructure.dll"
copy "C:\Program Files\PowerToys\Wox.Plugin.dll" "lib\Wox.Plugin.dll"
mkdir -p -e lib
copy "C:\Program Files\PowerToys\PowerToys.Common.UI.dll" "lib\PowerToys.Common.UI.dll" /y
copy "C:\Program Files\PowerToys\PowerToys.ManagedCommon.dll" "lib\PowerToys.ManagedCommon.dll" /y
copy "C:\Program Files\PowerToys\PowerToys.Settings.UI.Lib.dll" "lib\PowerToys.Settings.UI.Lib.dll" /y
copy "C:\Program Files\PowerToys\Wox.Infrastructure.dll" "lib\Wox.Infrastructure.dll" /y
copy "C:\Program Files\PowerToys\Wox.Plugin.dll" "lib\Wox.Plugin.dll" /y
copy "%LOCALAPPDATA%\PowerToys.Common.UI.dll" "lib\PowerToys.Common.UI.dll" /y
copy "%LOCALAPPDATA%\PowerToys.ManagedCommon.dll" "lib\PowerToys.ManagedCommon.dll" /y
copy "%LOCALAPPDATA%\PowerToys.Settings.UI.Lib.dll" "lib\PowerToys.Settings.UI.Lib.dll" /y
copy "%LOCALAPPDATA%\Wox.Infrastructure.dll" "lib\Wox.Infrastructure.dll" /y
copy "%LOCALAPPDATA%\Wox.Plugin.dll" "lib\Wox.Plugin.dll" /y
1 change: 1 addition & 0 deletions lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有必要再传 lib 文件夹,我理解有需要的话会运行 lib.bat 来生成

6 changes: 6 additions & 0 deletions packages.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 1,
"dependencies": {
"net8.0-windows7.0": {}
}
Comment on lines +1 to +5
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有必要用包管理

Copy link
Contributor Author

@NtskwK NtskwK Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,不需要。这只是为了通过现有的编译actions。
https://github.com/N0I0C0K/PowerTranslator/actions/runs/9919108964
如果你确实不需要包管理,可以在actions中删除对应的步骤。我会马上跟进。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来是这样,之前加 Action 目的是为了尝试一下 GitHub 能否提供 arm 平台,那倒是应该保留

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不必要的包管理器可以被移除了#58

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该可以在一个 pr 里去提交这些更改?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我更喜欢将不同的功能拆分。当一个pr的commit很多时,如果其中一个feat出了问题,可能导致其他的feat也被堵住。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我指的就是拆开,但是这里明显不是优化 language 相关的,分离出去

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

事实上,还有一个问题是。编译脚本的分支只能从release的commit点fork出来,否则现在的代码库无法通过编译。但是其他的没有问题,可以直接从HEAD节点fork。

}
71 changes: 53 additions & 18 deletions src/SettingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,62 @@ namespace Translator
{
public class SettingHelper
{
public static readonly List<string> languagesKeys = new List<string> { "auto", "zh-CHS", "zh-CHT", "en", "ja", "ko", "ru", "fr", "es", "ar", "de", "it", "he" };
public static readonly List<string> languagesOptions = new List<string> { "auto", "Chinese (Simplified)", "Chinese (Traditional)", "English", "Japanese", "Korean", "Russian", "French", "Spanish", "Arabic", "German", "Italian", "Hebrew" };
public static readonly List<string> dictUrlPatternKeys = new List<string> { "Youdao", "Oxford", "Cambridge" };
public static readonly List<string> dictUrlPatternValues = new List<string> { "https://www.youdao.com/result?word={0}&lang=en", "https://www.oed.com/search/dictionary/?scope=Entries&q={0}", "https://dictionary.cambridge.org/us/dictionary/english/{0}" };
public static readonly List<PluginAdditionalOption> pluginAdditionalOptions = GetAdditionalOptions();
public string defaultLanguageKey = "auto";
public bool enableSuggest = true;
public bool enableAutoRead = false;
public bool enableSecondLanuage = false;
public string secondLanuageKey = "auto";
public bool showOriginalQuery = false;
public bool enableJumpToDict = false;
public string dictUtlPattern = dictUrlPatternValues[0];
public static readonly List<string> DictUrlPatternKeys = new List<string> { "Youdao", "Oxford", "Cambridge" };
public static readonly List<string> DictUrlPatternValues = new List<string> { "https://www.youdao.com/result?word={0}&lang=en", "https://www.oed.com/search/dictionary/?scope=Entries&q={0}", "https://dictionary.cambridge.org/us/dictionary/english/{0}" };
public static readonly List<PluginAdditionalOption> PluginAdditionalOptions = GetAdditionalOptions();
public string DefaultLanguageKey = "auto";
public bool EnableSuggest = true;
public bool EnableAutoRead = false;
public bool EnableSecondLanuage = false;
public string SecondLanuageKey = "auto";
public bool ShowOriginalQuery = false;
public bool EnableJumpToDict = false;
public string DictUtlPattern = DictUrlPatternValues[0];
Comment on lines +7 to +17
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更倾向于 lower camel case

Copy link
Contributor Author

@NtskwK NtskwK Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抱歉,编辑器设置成了自动调整规范。C# 标识符命名规则和约定

如果你坚持,我会与你同步。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其他的规范我倒是没问题,主要是字段也是 PascalCase,首字母大写不是很舒服

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也可以改吧,我使用的是 vscode 作为编辑器,没有太多的 C# 内置支持,所以一直按照的是自己的规范


public static List<PluginAdditionalOption> GetAdditionalOptions()
public abstract class Languages
{
var lanuageItems = languagesOptions.Select((val, idx) =>
private static readonly Dictionary<string, string> LanguageContrast = new Dictionary<string, string>
{
return new KeyValuePair<string, string>(val, idx.ToString());
}).ToList();
var dictItems = dictUrlPatternKeys.Select((val, idx) =>
{ "auto", "auto" },
{ "zh-CHS", "Chinese (Simplified)" },
{ "zh-CHT", "Chinese (Traditional)" },
{ "en", "English" },
{ "ja", "Japanese" },
{ "ko", "Korean" },
{ "ru", "Russian" },
{ "fr", "French" },
{ "es", "Spanish" },
{ "ar", "Arabic" },
{ "de", "German" },
{ "it", "Italian" },
{ "he", "Hebrew" },
};

private static readonly Dictionary<string, string> LanguageAbbreviation = new Dictionary<string, string>
{
{ "zhs", "zh-CHS" },
{ "zht", "zh-CHT" },
};

public static string MatchLanguageCode(string languageKey)
{
string defaultLanguageCode = "auto";
languageKey = languageKey is null ? defaultLanguageCode : languageKey;
LanguageAbbreviation.TryGetValue(languageKey, out languageKey);
LanguageContrast.TryGetValue(languageKey, out defaultLanguageCode);
return defaultLanguageCode;
}

public static List<KeyValuePair<string, string>> ContrastList()
{
return LanguageContrast.ToList();
}
}
Comment on lines +19 to +57
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我倾向于单独出来,不要放到 SettingHelper下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

赞成,把配置里允许的选项和Setting本身单独封装是一个不错的想法。


public static List<PluginAdditionalOption> GetAdditionalOptions()
{
var lanuageItems = Languages.ContrastList();
var dictItems = DictUrlPatternKeys.Select((val, idx) =>
{
return new KeyValuePair<string, string>(val, idx.ToString());
}).ToList();
Expand Down
15 changes: 11 additions & 4 deletions src/TranslateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Wox.Plugin.Logger;
using Translator.Utils;
using System.Windows.Media;
using Translator.Youdao;

namespace Translator;

public class TranslateFailedException : Exception
Expand All @@ -16,6 +18,11 @@ public class TranslateHelper
{
public struct TranslateTarget
{
public TranslateTarget(string source, string target)
{
src = source;
target = SettingHelper.Languages.MatchLanguageCode(target);
}
public string src;
public string toLan;
}
Expand All @@ -24,7 +31,7 @@ public struct TranslateTarget
private object initLock = new Object();
private long lastInitTime = 0;
private IPublicAPI publicAPI;
private List<Youdao.ITranslator?> translators;
private List<Translator.Youdao.ITranslator?> translators;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多余的?同下

private List<Type> translatorTypes;
private bool isSpeaking = false;
private bool isIniting = false;
Expand All @@ -35,7 +42,7 @@ public TranslateHelper(IPublicAPI publicAPI, string defaultLanguageKey = "auto")
null, null, null
};
translatorTypes = new List<Type>{
typeof(Youdao.V2.YoudaoTranslator),
typeof(Translator),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能这样做,是需要实现了 ITranslator 的类型

typeof(Youdao.old.YoudaoTranslator),
typeof(Youdao.Backup.BackUpTranslator)
};
Expand Down Expand Up @@ -72,7 +79,7 @@ public List<ResultItem> QueryTranslate(string raw, string translateFrom = "user
var target = ParseRawSrc(raw);
string src = target.src;
string toLan = toLanuage ?? target.toLan;
Youdao.ITranslateResult? translateResult = null;
Translator.Youdao.ITranslateResult? translateResult = null;
int idx = 0;
translateResult = this.translators.FirstNotNoneCast((it) =>
{
Expand Down Expand Up @@ -199,7 +206,7 @@ public bool InitTranslater()
try
{
var tran = tp.GetConstructor(Type.EmptyTypes)?.Invoke(null);
this.translators[idx] = tran as Youdao.ITranslator;
this.translators[idx] = tran as Translator.Youdao.ITranslator;
}
catch (Exception ex)
{
Expand Down
20 changes: 10 additions & 10 deletions src/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Translator : IPlugin, IDisposable, IDelayedExecutionPlugin, ISettin
public string Name => "Translator";
public static string PluginID => "EY1EBAMTNIWIVLYM039DSOS5MWITDJOD";
public string Description => "A simple translation plugin, based on Youdao Translation";
public IEnumerable<PluginAdditionalOption> AdditionalOptions => SettingHelper.pluginAdditionalOptions;
public IEnumerable<PluginAdditionalOption> AdditionalOptions => SettingHelper.PluginAdditionalOptions;
public PluginMetadata? queryMetaData = null;
public IPublicAPI? publicAPI = null;
public const int delayQueryMillSecond = 500;
Expand Down Expand Up @@ -186,7 +186,7 @@ public List<Result> Query(Query query, bool delayedExecution)

// get suggest in other thread
Task<List<ResultItem>>? suggestTask = null;
if (settingHelper.enableSuggest)
if (settingHelper.EnableSuggest)
{
suggestTask = Task.Run(() =>
{
Expand All @@ -196,11 +196,11 @@ public List<Result> Query(Query query, bool delayedExecution)

// get second translate result in other thread
Task<List<ResultItem>>? secondTranslateTask = null;
if (settingHelper.enableSecondLanuage && settingHelper.secondLanuageKey != null)
if (settingHelper.EnableSecondLanuage && settingHelper.SecondLanuageKey != null)
{
secondTranslateTask = Task.Run(() =>
{
return translateHelper!.QueryTranslate(querySearch, toLanuage: settingHelper.secondLanuageKey);
return translateHelper!.QueryTranslate(querySearch, toLanuage: settingHelper.SecondLanuageKey);
});
}

Expand All @@ -220,7 +220,7 @@ public List<Result> Query(Query query, bool delayedExecution)
res.AddRange(suggest);
}

if (settingHelper.showOriginalQuery)
if (settingHelper.ShowOriginalQuery)
{
res.Add(new ResultItem
{
Expand All @@ -243,7 +243,7 @@ public List<Result> Query(Query query, bool delayedExecution)
});
}

if (settingHelper.enableAutoRead)
if (settingHelper.EnableAutoRead)
{
this.translateHelper?.Read(res.FirstOrDefault()?.Title);
}
Expand Down Expand Up @@ -273,7 +273,7 @@ public void Init(PluginInitContext context)
publicAPI = context.API;
var translaTask = Task.Factory.StartNew(() =>
{
translateHelper = new TranslateHelper(publicAPI, this.settingHelper.defaultLanguageKey);
translateHelper = new TranslateHelper(publicAPI, this.settingHelper.DefaultLanguageKey);
});
suggestHelper = new Suggest.SuggestHelper(publicAPI);
historyHelper = new History.HistoryHelper();
Expand Down Expand Up @@ -306,7 +306,7 @@ public void UpdateSettings(PowerLauncherPluginSettings settings)
this.settingHelper.UpdateSettings(settings);

if (this.translateHelper != null)
this.translateHelper.defaultLanguageKey = this.settingHelper.defaultLanguageKey;
this.translateHelper.defaultLanguageKey = this.settingHelper.DefaultLanguageKey;
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
Expand Down Expand Up @@ -340,15 +340,15 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
}

};
if (settingHelper.enableJumpToDict)
if (settingHelper.EnableJumpToDict)
{
contextMenu.Add(
new ContextMenuResult
{
Title = "Go to dictionary",
Action = context =>
{
Helper.OpenInShell(string.Format(settingHelper.dictUtlPattern, selectedResult.Title));
Helper.OpenInShell(string.Format(settingHelper.DictUtlPattern, selectedResult.Title));
return false;
},
Glyph = "\xE721",
Expand Down
2 changes: 1 addition & 1 deletion src/Youdao/YoudaoTranslatorV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Translator.Utils;
using Wox.Plugin.Logger;

namespace Translator.Youdao.V2;
namespace Translator.Youdao;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要单独开,他是 youdao 下单独的版本


public class KeyResponse
{
Expand Down