Skip to content

Commit

Permalink
Improve i18n for system plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Nov 1, 2024
1 parent 188d16a commit e6df3c3
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 157 deletions.
3 changes: 3 additions & 0 deletions Wox/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (a *APIImpl) ShowApp(ctx context.Context) {
}

func (a *APIImpl) Notify(ctx context.Context, message string) {
// translate message
message = a.GetTranslation(ctx, message)

if GetPluginManager().GetUI().IsPluginQuery(ctx, a.pluginInstance.Metadata.Id) {
GetPluginManager().GetUI().ShowToolbarMsg(ctx, share.ToolbarMsg{
Text: message,
Expand Down
25 changes: 13 additions & 12 deletions Wox/plugin/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sort"
"wox/i18n"
"wox/updater"
"wox/util"
"wox/util/permission"
Expand Down Expand Up @@ -40,17 +41,17 @@ func checkWoxVersion(ctx context.Context) DoctorCheckResult {
if err != nil {
if latestVersion.LatestVersion == updater.CURRENT_VERSION {
return DoctorCheckResult{
Name: "Version",
Name: "i18n:plugin_doctor_version",
Status: true,
Description: "Already using the latest version",
Description: fmt.Sprintf(i18n.GetI18nManager().TranslateWox(ctx, "plugin_doctor_version_latest"), latestVersion.LatestVersion),
ActionName: "",
Action: func(ctx context.Context) {
},
}
}

return DoctorCheckResult{
Name: "Version",
Name: "i18n:plugin_doctor_version",
Status: true,
Description: err.Error(),
ActionName: "",
Expand All @@ -60,12 +61,12 @@ func checkWoxVersion(ctx context.Context) DoctorCheckResult {
}

return DoctorCheckResult{
Name: "Version",
Name: "i18n:plugin_doctor_version",
Status: false,
Description: fmt.Sprintf("New version available: %s", latestVersion),
ActionName: "Check for updates",
Description: fmt.Sprintf(i18n.GetI18nManager().TranslateWox(ctx, "plugin_doctor_version_update_available"), latestVersion.CurrentVersion, latestVersion.LatestVersion),
ActionName: "i18n:plugin_doctor_version_download",
Action: func(ctx context.Context) {
//updater.OpenUpdatePage(ctx)
util.ShellOpen(latestVersion.DownloadUrl)
},
}
}
Expand All @@ -74,20 +75,20 @@ func checkAccessibilityPermission(ctx context.Context) DoctorCheckResult {
hasPermission := permission.HasAccessibilityPermission(ctx)
if !hasPermission {
return DoctorCheckResult{
Name: "Accessibility",
Name: "i18n:plugin_doctor_accessibility",
Status: false,
Description: "You need to grant Wox Accessibility permission to use this plugin",
ActionName: "Open Accessibility Settings",
Description: "i18n:plugin_doctor_accessibility_required",
ActionName: "i18n:plugin_doctor_accessibility_open_settings",
Action: func(ctx context.Context) {
permission.GrantAccessibilityPermission(ctx)
},
}
}

return DoctorCheckResult{
Name: "Accessibility",
Name: "i18n:plugin_doctor_accessibility",
Status: hasPermission,
Description: "You have granted Wox Accessibility permission",
Description: "i18n:plugin_doctor_accessibility_granted",
ActionName: "",
Action: func(ctx context.Context) {
},
Expand Down
4 changes: 4 additions & 0 deletions Wox/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ func (m *Manager) PolishResult(ctx context.Context, pluginInstance *Instance, qu
for actionIndex := range result.Actions {
result.Actions[actionIndex].Name = m.translatePlugin(ctx, pluginInstance, result.Actions[actionIndex].Name)
}
// translate preview data if preview type is text
if result.Preview.PreviewType == WoxPreviewTypeText {
result.Preview.PreviewData = m.translatePlugin(ctx, pluginInstance, result.Preview.PreviewData)
}

// set first action as default if no default action is set
defaultActionCount := lo.CountBy(result.Actions, func(item QueryResultAction) bool {
Expand Down
55 changes: 28 additions & 27 deletions Wox/plugin/system/ai_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image"
"strings"
"wox/ai"
"wox/i18n"
"wox/plugin"
"wox/setting/definition"
"wox/share"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *Plugin) GetMetadata() plugin.Metadata {
Version: "1.0.0",
MinWoxVersion: "2.0.0",
Runtime: "Go",
Description: "Make your daily tasks easier with AI commands",
Description: "i18n:plugin_ai_command_description",
Icon: aiCommandIcon.String(),
Entry: "",
TriggerKeywords: []string{
Expand All @@ -70,43 +71,43 @@ func (c *Plugin) GetMetadata() plugin.Metadata {
Type: definition.PluginSettingDefinitionTypeTable,
Value: &definition.PluginSettingValueTable{
Key: "commands",
Title: "Commands",
Tooltip: "The commands to run.\r\nE.g. `translate`, user will type `ai translate` to run translate based on the prompt",
Title: "i18n:plugin_ai_command_commands",
Tooltip: "i18n:plugin_ai_command_commands_tooltip",
Columns: []definition.PluginSettingValueTableColumn{
{
Key: "name",
Label: "Name",
Label: "i18n:plugin_ai_command_name",
Type: definition.PluginSettingValueTableColumnTypeText,
Width: 100,
Tooltip: "The name of the ai command. E.g. `Translator`",
Tooltip: "i18n:plugin_ai_command_name_tooltip",
},
{
Key: "command",
Label: "Command",
Label: "i18n:plugin_ai_command_command",
Type: definition.PluginSettingValueTableColumnTypeText,
Width: 80,
Tooltip: "The command to run. E.g. `translate`, user will type `ai translate` to run this command",
Tooltip: "i18n:plugin_ai_command_command_tooltip",
},
{
Key: "model",
Label: "Model",
Label: "i18n:plugin_ai_command_model",
Type: definition.PluginSettingValueTableColumnTypeSelectAIModel,
Width: 100,
Tooltip: "The ai model to use.",
Tooltip: "i18n:plugin_ai_command_model_tooltip",
},
{
Key: "prompt",
Label: "Prompt",
Label: "i18n:plugin_ai_command_prompt",
Type: definition.PluginSettingValueTableColumnTypeText,
TextMaxLines: 10,
Tooltip: "The prompt to send to the ai. %s will be replaced with the user input",
Tooltip: "i18n:plugin_ai_command_prompt_tooltip",
},
{
Key: "vision",
Label: "Vision",
Label: "i18n:plugin_ai_command_vision",
Type: definition.PluginSettingValueTableColumnTypeCheckbox,
Width: 60,
Tooltip: "Does the command interact with vision?",
Tooltip: "i18n:plugin_ai_command_vision_tooltip",
},
},
},
Expand Down Expand Up @@ -179,8 +180,8 @@ func (c *Plugin) querySelection(ctx context.Context, query plugin.Query) []plugi

var startAnsweringTime int64
onPreparing := func(current plugin.RefreshableResult) plugin.RefreshableResult {
current.Preview.PreviewData = "Answering..."
current.SubTitle = "Answering..."
current.Preview.PreviewData = "i18n:plugin_ai_command_answering"
current.SubTitle = "i18n:plugin_ai_command_answering"
startAnsweringTime = util.GetSystemTimestamp()
return current
}
Expand All @@ -199,12 +200,12 @@ func (c *Plugin) querySelection(ctx context.Context, query plugin.Query) []plugi

if isFinished {
current.RefreshInterval = 0 // stop refreshing
current.SubTitle = fmt.Sprintf("Answered, cost %d ms", util.GetSystemTimestamp()-startAnsweringTime)
current.SubTitle = fmt.Sprintf(i18n.GetI18nManager().TranslateWox(ctx, "plugin_ai_command_answered_cost"), util.GetSystemTimestamp()-startAnsweringTime)
answerText = current.Preview.PreviewData

current.Actions = []plugin.QueryResultAction{
{
Name: "Copy",
Name: "i18n:plugin_ai_command_copy",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
clipboard.WriteText(answerText)
},
Expand Down Expand Up @@ -255,14 +256,14 @@ func (c *Plugin) querySelection(ctx context.Context, query plugin.Query) []plugi
Title: command.Name,
SubTitle: fmt.Sprintf("%s - %s", command.AIModel().Provider, command.AIModel().Name),
Icon: aiCommandIcon,
Preview: plugin.WoxPreview{PreviewType: plugin.WoxPreviewTypeText, PreviewData: "Enter to start chat"},
Preview: plugin.WoxPreview{PreviewType: plugin.WoxPreviewTypeText, PreviewData: "i18n:plugin_ai_command_enter_to_start"},
RefreshInterval: 100,
OnRefresh: createLLMOnRefreshHandler(ctx, c.api.AIChatStream, command.AIModel(), conversations, func() bool {
return startGenerate
}, onPreparing, onAnswering, onAnswerErr),
Actions: []plugin.QueryResultAction{
{
Name: "Run",
Name: "i18n:plugin_ai_command_run",
PreventHideAfterAction: true,
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
startGenerate = true
Expand All @@ -289,7 +290,7 @@ func (c *Plugin) listAllCommands(ctx context.Context, query plugin.Query) []plug
if len(commands) == 0 {
return []plugin.QueryResult{
{
Title: "No ai commands found",
Title: "i18n:plugin_ai_command_no_commands",
Icon: aiCommandIcon,
},
}
Expand All @@ -303,7 +304,7 @@ func (c *Plugin) listAllCommands(ctx context.Context, query plugin.Query) []plug
Icon: aiCommandIcon,
Actions: []plugin.QueryResultAction{
{
Name: "Run",
Name: "i18n:plugin_ai_command_run",
PreventHideAfterAction: true,
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
c.api.ChangeQuery(ctx, share.PlainQuery{
Expand Down Expand Up @@ -332,7 +333,7 @@ func (c *Plugin) queryCommand(ctx context.Context, query plugin.Query) []plugin.
if query.Search == "" {
return []plugin.QueryResult{
{
Title: "Type to start chat",
Title: "i18n:plugin_ai_command_type_to_start",
Icon: aiCommandIcon,
},
}
Expand All @@ -351,7 +352,7 @@ func (c *Plugin) queryCommand(ctx context.Context, query plugin.Query) []plugin.
if len(commands) == 0 {
return []plugin.QueryResult{
{
Title: "No ai commands found",
Title: "i18n:plugin_ai_command_no_commands",
Icon: aiCommandIcon,
},
}
Expand All @@ -363,7 +364,7 @@ func (c *Plugin) queryCommand(ctx context.Context, query plugin.Query) []plugin.
if !commandExist {
return []plugin.QueryResult{
{
Title: "No ai command found",
Title: "i18n:plugin_ai_command_not_found",
Icon: aiCommandIcon,
},
}
Expand All @@ -372,7 +373,7 @@ func (c *Plugin) queryCommand(ctx context.Context, query plugin.Query) []plugin.
if aiCommandSetting.Prompt == "" {
return []plugin.QueryResult{
{
Title: "Prompt is empty for this ai command",
Title: "i18n:plugin_ai_command_empty_prompt",
Icon: aiCommandIcon,
},
}
Expand Down Expand Up @@ -412,7 +413,7 @@ func (c *Plugin) queryCommand(ctx context.Context, query plugin.Query) []plugin.
}

result := plugin.QueryResult{
Title: fmt.Sprintf("Chat with %s", aiCommandSetting.Name),
Title: fmt.Sprintf(i18n.GetI18nManager().TranslateWox(ctx, "plugin_ai_command_chat_with"), aiCommandSetting.Name),
SubTitle: fmt.Sprintf("%s - %s", aiCommandSetting.AIModel().Provider, aiCommandSetting.AIModel().Name),
Preview: plugin.WoxPreview{PreviewType: plugin.WoxPreviewTypeMarkdown, PreviewData: ""},
Icon: aiCommandIcon,
Expand All @@ -422,7 +423,7 @@ func (c *Plugin) queryCommand(ctx context.Context, query plugin.Query) []plugin.
}, nil, onAnswering, onAnswerErr),
Actions: []plugin.QueryResultAction{
{
Name: "Copy",
Name: "i18n:plugin_ai_command_copy",
Icon: plugin.CopyIcon,
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
clipboard.WriteText(actionContext.ContextData)
Expand Down
15 changes: 8 additions & 7 deletions Wox/plugin/system/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"slices"
"wox/i18n"
"wox/plugin"
"wox/setting"
"wox/util"
Expand Down Expand Up @@ -59,18 +60,18 @@ func (c *BackupPlugin) Query(ctx context.Context, query plugin.Query) []plugin.Q
func (c *BackupPlugin) backup(ctx context.Context, query plugin.Query) []plugin.QueryResult {
return []plugin.QueryResult{
{
Title: "Backup now",
SubTitle: "Backup Wox settings",
Title: "i18n:plugin_backup_now",
SubTitle: "i18n:plugin_backup_subtitle",
Icon: backupIcon,
Actions: []plugin.QueryResultAction{
{
Name: "Backup",
Name: "i18n:plugin_backup_action",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
backupErr := setting.GetSettingManager().Backup(ctx, setting.BackupTypeManual)
if backupErr != nil {
c.api.Notify(ctx, backupErr.Error())
} else {
c.api.Notify(ctx, "Wox settings backed up")
c.api.Notify(ctx, i18n.GetI18nManager().TranslateWox(ctx, "plugin_backup_success"))
}
},
},
Expand All @@ -84,7 +85,7 @@ func (c *BackupPlugin) restore(ctx context.Context, query plugin.Query) []plugin
if err != nil {
return []plugin.QueryResult{
{
Title: "Error",
Title: "i18n:plugin_backup_error",
SubTitle: err.Error(),
Icon: backupIcon,
},
Expand All @@ -104,13 +105,13 @@ func (c *BackupPlugin) restore(ctx context.Context, query plugin.Query) []plugin
Icon: backupIcon,
Actions: []plugin.QueryResultAction{
{
Name: "Restore",
Name: "i18n:plugin_backup_restore",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
restoreErr := setting.GetSettingManager().Restore(ctx, backup.Id)
if restoreErr != nil {
c.api.Notify(ctx, restoreErr.Error())
} else {
c.api.Notify(ctx, "Wox settings restored")
c.api.Notify(ctx, i18n.GetI18nManager().TranslateWox(ctx, "plugin_backup_restore_success"))
}
},
},
Expand Down
12 changes: 6 additions & 6 deletions Wox/plugin/system/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"strconv"
"strings"
"wox/i18n"
"wox/plugin"
"wox/setting/definition"
"wox/setting/validator"
Expand Down Expand Up @@ -84,8 +85,8 @@ func (c *BrowserPlugin) GetMetadata() plugin.Metadata {
Value: &definition.PluginSettingValueTextBox{
Key: browserWebsocketPortSettingKey,
DefaultValue: "34988",
Label: "Server Port",
Tooltip: "The port for the websocket server to communicate with the browser extension. Default is 34988. ",
Label: "i18n:plugin_browser_server_port",
Tooltip: "i18n:plugin_browser_server_port_tooltip",
Style: definition.PluginSettingValueStyle{
PaddingRight: 10,
},
Expand All @@ -109,7 +110,7 @@ func (c *BrowserPlugin) Init(ctx context.Context, initParams plugin.InitParams)
util.Go(ctx, "newWebsocketServer on init", func() {
err := c.newWebsocketServer(ctx)
if err != nil {
c.api.Notify(ctx, fmt.Sprintf("Failed to start websocket server: %s", err.Error()))
c.api.Notify(ctx, fmt.Sprintf(i18n.GetI18nManager().TranslateWox(ctx, "plugin_browser_server_start_error"), err.Error()))
}
})

Expand All @@ -118,7 +119,7 @@ func (c *BrowserPlugin) Init(ctx context.Context, initParams plugin.InitParams)
util.Go(ctx, "newWebsocketServer on port changed", func() {
err := c.newWebsocketServer(ctx)
if err != nil {
c.api.Notify(ctx, fmt.Sprintf("Failed to start websocket server: %s", err.Error()))
c.api.Notify(ctx, fmt.Sprintf(i18n.GetI18nManager().TranslateWox(ctx, "plugin_browser_server_start_error"), err.Error()))
}
})
}
Expand Down Expand Up @@ -151,7 +152,7 @@ func (c *BrowserPlugin) Query(ctx context.Context, query plugin.Query) (results
Icon: icon,
Actions: []plugin.QueryResultAction{
{
Name: "Open",
Name: "i18n:plugin_browser_open_tab",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
c.m.Broadcast([]byte(fmt.Sprintf(`{"method":"highlightTab","data":"{\"tabId\":%d,\"windowId\":%d,\"tabIndex\": %d}"}`, tab.TabId, tab.WindowId, tab.TabIndex)))
},
Expand Down Expand Up @@ -229,7 +230,6 @@ func (c *BrowserPlugin) newWebsocketServer(ctx context.Context) error {
})
})

c.api.Log(ctx, plugin.LogLevelInfo, fmt.Sprintf("browser websocket server start at:ws://localhost:%d", port))
c.server = &http.Server{Addr: fmt.Sprintf("localhost:%d", port), Handler: cors.Default().Handler(mux)}
err := c.server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
Expand Down
Loading

0 comments on commit e6df3c3

Please sign in to comment.