param(
    [string]$langCode = "zh_CN"
)

$tsDir = "d:\aoyiduo\woterm\woterm\private\languages\ts"
$tsFile = Join-Path $tsDir "woterm_$langCode.ts.xml"

Write-Host "Processing $langCode..." -ForegroundColor Yellow
Write-Host "  TS file: $tsFile"

$translations = @(
    @("MoDBRestoreSetup", "SQLiteDB selection", "SQLite数据库选择"),
    @("MoLanguageView", "After modifying the language, you need to restart the application to take effect.", "修改语言后，需要重启应用才能生效。"),
    @("QKxCombineKeyActionDialog", "+Alt", "+Alt"),
    @("QKxCombineKeyActionDialog", "-Alt", "-Alt"),
    @("QKxCombineKeyActionDialog", "+Ansi", "+Ansi"),
    @("QKxCombineKeyActionDialog", "-Ansi", "-Ansi"),
    @("QKxCombineKeyActionDialog", "+AppCuKeys", "+应用光标键"),
    @("QKxCombineKeyActionDialog", "-AppCuKeys", "-应用光标键"),
    @("QKxCombineKeyActionDialog", "+AppKeyPad", "+应用小键盘"),
    @("QKxCombineKeyActionDialog", "-AppKeyPad", "-应用小键盘"),
    @("QKxCombineKeyActionDialog", "+AppScreen", "+应用屏幕"),
    @("QKxCombineKeyActionDialog", "-AppScreen", "-应用屏幕"),
    @("QKxCombineKeyActionDialog", "+Ctrl", "+Ctrl"),
    @("QKxCombineKeyActionDialog", "-Ctrl", "-Ctrl"),
    @("QKxCombineKeyActionDialog", "+Meta", "+Meta"),
    @("QKxCombineKeyActionDialog", "-Meta", "-Meta"),
    @("QKxCombineKeyActionDialog", "+NewLine", "+新行"),
    @("QKxCombineKeyActionDialog", "-NewLine", "-新行"),
    @("QKxCombineKeyActionDialog", "None", "无"),
    @("QKxCombineKeyActionDialog", "+Shift", "+Shift"),
    @("QKxCombineKeyActionDialog", "-Shift", "-Shift"),
    @("QKxDockGuider", "Drop here", "拖放到此处"),
    @("QKxDockGuider", "Form", "表单"),
    @("QKxFtpFilterInput", "Form", "表单"),
    @("QKxInputHistoryDialog", "Current input message is empty.", "当前输入消息为空。"),
    @("QKxInputHistoryDialog", "Dialog", "对话框"),
    @("QKxInputHistoryDialog", "Remove", "移除"),
    @("QKxItemFilterDialog", "Dialog", "对话框"),
    @("QKxLoadingAnimation", "Form", "表单"),
    @("QKxSearch", "Border", "边框"),
    @("QKxSessionPanel", "Search sessions…", "搜索会话…"),
    @("QKxSessionTreeView", "Sessions", "会话"),
    @("QKxSessionTreeView", "Delete Folder", "删除文件夹"),
    @("QKxSessionTreeView", 'Delete "%1" and all its contents?', '删除"%1"及其所有内容？'),
    @("QKxTermCompleter", "Form", "表单"),
    @("QKxTermScriptReplayer", "Form", "表单"),
    @("QWoAboutDialog", "OfficeWeb", "官网"),
    @("QWoBaseToolForm", "Form", "表单"),
    @("QWoBindPortPermissionDialog", "sudo sysctl -w net.ipv4.ip_unprivileged_port_start=0", "sudo sysctl -w net.ipv4.ip_unprivileged_port_start=0"),
    @("QWoDbBackupDialog", "backup success.", "备份成功。"),
    @("QWoDbBackupDialog", "Input a file path to backup.", "请输入备份文件路径。"),
    @("QWoDbPowerBackupDialog", "sftp server", "sftp服务器"),
    @("QWoDBServerMergeModel", "path", "路径"),
    @("QWoDBSftpDownListDialog", "Backup file list", "备份文件列表"),
    @("QWoEditorWindow", "Ctrl+E", "Ctrl+E"),
    @("QWoEditorWindow", "Ctrl+S", "Ctrl+S"),
    @("QWoFtpAuthorizeInput", "Explicit SSL/TLS", "显式SSL/TLS"),
    @("QWoFtpAuthorizeInput", "Form", "表单"),
    @("QWoFtpAuthorizeInput", "Implicit SSL/TLS", "隐式SSL/TLS"),
    @("QWoFtpAuthorizeInput", "No SSL/TLS", "无SSL/TLS"),
    @("QWoFtpTransferWidget", "Breakpoint resume is an exclusive feature of the professional version", "断点续传是专业版专属功能"),
    @("QWoFtpWidget", "Failed to open file:", "无法打开文件："),
    @("QWoFtpWidget", "Form", "表单"),
    @("QWoFtpWidget", "FTP", "FTP"),
    @("QWoHistoryWidget", "Form", "表单"),
    @("QWoHostSimpleList", "Dialog", "对话框"),
    @("QWoHostSimpleList", "Ok", "确定"),
    @("QWoIdentifyDialog", "ssh-agent", "ssh-agent"),
    @("QWoInternetAccessDetectionDialog", "Dialog", "对话框"),
    @("QWoInternetAccessDetectionDialog", "Ok", "确定"),
    @("QWoLockWidget", "Form", "表单"),
    @("QWoMainWindow", "Alt+L", "Alt+L"),
    @("QWoMainWindow", "Alt+O", "Alt+O"),
    @("QWoMainWindow", "d", "d"),
    @("QWoMainWindow", "Plugin", "插件"),
    @("QWoMenuButton", "PushButton", "按钮"),
    @("QWoPluginManageDialog", "Plugin Manager", "插件管理器"),
    @("QWoPluginManageDialog", "Author", "作者"),
    @("QWoPluginManageDialog", "Version", "版本"),
    @("QWoPluginManageDialog", "Description", "描述"),
    @("QWoPluginManageDialog", "Enable", "启用"),
    @("QWoPluginManageDialog", "Disable", "禁用"),
    @("QWoPluginManageDialog", "Enabled", "已启用"),
    @("QWoPluginManageDialog", "Disabled", "已禁用"),
    @("QWoPluginManageDialog", "Name", "名称"),
    @("QWoPluginManageDialog", "Plugin files (*.dll)", "插件文件 (*.dll)"),
    @("QWoPluginManageDialog", "Import Plugin", "导入插件"),
    @("QWoPluginManageDialog", "Plugin Name", "插件名称"),
    @("QWoPluginManageDialog", "Enter plugin name:", "输入插件名称："),
    @("QWoPluginManageDialog", "Confirm Remove", "确认移除"),
    @("QWoPluginManageDialog", "Copy Plugin", "复制插件"),
    @("QWoPluginManageDialog", "Enter new plugin name:", "输入新插件名称："),
    @("QWoPluginManageDialog", "Import", "导入"),
    @("QWoPluginManageDialog", "Remove", "移除"),
    @("QWoPluginManageDialog", "Copy", "复制"),
    @("QWoPluginManageDialog", "Edit", "编辑"),
    @("QWoPluginManageDialog", "Dialog", "对话框"),
    @("QWoPluginManageDialog", "Are you sure to remove plugin '%1'?", "确定要移除插件'%1'吗？"),
    @("QWoPluginSessionAdapter", "failed to upate information", "更新信息失败"),
    @("QWoSerialInput", "Form", "表单"),
    @("QWoSessionFileAssociationAddDialog", ".tar.gz, .ini, .sh, .log, .conf, .db, *", ".tar.gz, .ini, .sh, .log, .conf, .db, *"),
    @("QWoSessionFileAssociationAddDialog", "{file}", "{file}"),
    @("QWoSessionFileAssociationAddDialog", "Example: c:\Windows\notepad.exe {file}", "示例：c:\Windows\notepad.exe {file}"),
    @("QWoSessionFTPProperty", " (EPSV)", " (EPSV)"),
    @("QWoSessionInternalTreeView", "Add folder", "添加文件夹"),
    @("QWoSessionInternalTreeView", "Add session", "添加会话"),
    @("QWoSessionInternalTreeView", "Continue to delete the selected items?", "是否继续删除选中的项目？"),
    @("QWoSessionInternalTreeView", "Copy", "复制"),
    @("QWoSessionInternalTreeView", "Delete", "删除"),
    @("QWoSessionInternalTreeView", "Deletion confirmation", "删除确认"),
    @("QWoSessionInternalTreeView", "Edit", "编辑"),
    @("QWoSessionInternalTreeView", "Folder name:", "文件夹名称："),
    @("QWoSessionInternalTreeView", "Memo", "备忘"),
    @("QWoSessionInternalTreeView", "Name", "名称"),
    @("QWoSessionInternalTreeView", "New folder", "新建文件夹"),
    @("QWoSessionInternalWidget", "Enter keyword to search", "输入关键字搜索"),
    @("QWoSessionInternalWidget", "Form", "表单"),
    @("QWoSessionInternalWidget", "login name", "登录名"),
    @("QWoSessionInternalWidget", "memo", "备忘"),
    @("QWoSessionManage", "Confirmation", "确认"),
    @("QWoSessionManage", "New folder", "新建文件夹"),
    @("QWoSessionManage", "New session", "新建会话"),
    @("QWoSessionManage", "No selected items to open", "没有选中的项目可打开"),
    @("QWoSessionPluginTreeView", "Continue to delete the selected items?", "是否继续删除选中的项目？"),
    @("QWoSessionPluginTreeView", "Delete", "删除"),
    @("QWoSessionPluginTreeView", "Deletion confirmation", "删除确认"),
    @("QWoSessionPluginTreeView", "Edit", "编辑"),
    @("QWoSessionPluginTreeView", "Folder name:", "文件夹名称："),
    @("QWoSessionPluginTreeView", "Memo", "备忘"),
    @("QWoSessionPluginTreeView", "New folder", "新建文件夹"),
    @("QWoSessionPluginWidget", "Enter keyword to search", "输入关键字搜索"),
    @("QWoSessionPluginWidget", "Form", "表单"),
    @("QWoSessionPluginWidget", "login name", "登录名"),
    @("QWoSessionPluginWidget", "memo", "备忘"),
    @("QWoSessionProperty", "Explicit SSL/TLS", "显式SSL/TLS"),
    @("QWoSessionProperty", "Implicit SSL/TLS", "隐式SSL/TLS"),
    @("QWoSessionProperty", "No SSL/TLS", "无SSL/TLS"),
    @("QWoSessionProperty", "The name can't contain characters like (/)", "名称不能包含 (/) 等字符"),
    @("QWoSessionRDPProperty", "FreeRdp", "FreeRdp"),
    @("QWoSessionRDPProperty", "Mstsc", "Mstsc"),
    @("QWoSessionToolConfigureDialog", '"{file}"', '"{file}"'),
    @("QWoSessionToolConfigureDialog", 'Example: c:\Windows\notepad.exe "{file}"', '示例：c:\Windows\notepad.exe "{file}"'),
    @("QWoSessionToolConfigureDialog", "Tab 2", "标签 2"),
    @("QWoSessionTTYProperty", "[DISPLAY=:0]", "[DISPLAY=:0]"),
    @("QWoSessionTTYProperty", "localhost:0.0", "localhost:0.0"),
    @("QWoSftpTaskRunner", "Form", "表单"),
    @("QWoSftpTaskRunner", "Mb/s", "兆字节/秒"),
    @("QWoSftpWidget", "Chmod", "权限"),
    @("QWoSSHAuthorizeInput", "Form", "表单"),
    @("QWoSSHAuthorizeInput", "tr", "tr"),
    @("QWoSSHChartWidget", "CPU", "CPU"),
    @("QWoSSHChartWidget", "Form", "表单"),
    @("QWoSshMonitorWidget", "Form", "表单"),
    @("QWoSSHProcessModel", "CPU", "CPU"),
    @("QWoSshTermWidget", "Choose a way to accept the file.", "选择一种接受文件的方式。"),
    @("QWoSshTermWidget", "Paste text", "粘贴文本"),
    @("QWoSystemOptionDialog", "Ctrl+Shift+Tab", "Ctrl+Shift+Tab"),
    @("QWoSystemOptionDialog", "Ctrl+Tab", "Ctrl+Tab"),
    @("QWoTelnetTermWidgetImpl", "%1:%2", "%1:%2"),
    @("QWoTermMask", "Form", "表单"),
    @("QWoTermWidget", "Failed to create file.", "创建文件失败。"),
    @("QWoTermWidget", "log (*.log)", "日志 (*.log)"),
    @("QWoTermWidget", "Save history to file", "保存历史到文件"),
    @("QWoTunnelAddDialog", "HTTP/SOCKS4/SOCKS5 over SSH", "通过SSH的HTTP/SOCKS4/SOCKS5"),
    @("QWoTunnelAddDialog", "Plain HTTP/SOCKS4/SOCKS5", "普通HTTP/SOCKS4/SOCKS5"),
    @("QWoTunnelAddDialog", "Port Forward over SSH", "SSH端口转发"),
    @("QWoTunnelDialog", "[", "["),
    @("QWoTunnelDialog", "]", "]"),
    @("QWoTunnelDialog", "No system proxy", "无系统代理"),
    @("QWoTunnelHelpDialog", "Dialog", "对话框"),
    @("QWoTunnelHelpDialog", "OK", "确定"),
    @("QWoUserLoginDialog", "Email:", "邮箱："),
    @("QWoUserLoginDialog", "PushButton", "按钮"),
    @("QWoVncPlayWidget", "Form", "表单"),
    @("QWoVncToolForm", "Form", "表单"),
    @("WtPluginParamsDialog", "Edit Plugin: %1", "编辑插件：%1"),
    @("WtPluginParamsDialog", "Failed to get parameter template", "获取参数模板失败"),
    @("WtPluginParamsDialog", "Failed to load plugin library:`n%1", "加载插件库失败：`n%1"),
    @("WtPluginParamsDialog", "Internal plugin path cannot be modified", "内部插件路径不可修改"),
    @("WtPluginParamsDialog", "Invalid JSON format: %1", "无效的JSON格式：%1"),
    @("WtPluginParamsDialog", "Invalid template JSON format: %1", "无效的模板JSON格式：%1"),
    @("WtPluginParamsDialog", "Load Template", "加载模板"),
    @("WtPluginParamsDialog", "Loading template will overwrite current parameters. Continue?", "加载模板将覆盖当前参数，是否继续？"),
    @("WtPluginParamsDialog", "Name:", "名称："),
    @("WtPluginParamsDialog", "Parameters (JSON):", "参数 (JSON)："),
    @("WtPluginParamsDialog", "Parameters must be a JSON object", "参数必须是JSON对象"),
    @("WtPluginParamsDialog", "Path:", "路径："),
    @("WtPluginParamsDialog", "Plugin does not support parameter template", "插件不支持参数模板"),
    @("WtPluginParamsDialog", "Plugin name cannot be empty", "插件名称不能为空")
)

Write-Host "  Total translations defined: $($translations.Count)"

$xml = New-Object System.Xml.XmlDocument
$xml.Load($tsFile)
$updated = 0

foreach ($ctx in $xml.TS.context) {
    $ctxName = $ctx.name
    foreach ($msg in $ctx.message) {
        $src = $msg.source
        
        $transNode = $msg.translation
        $hasTranslation = $false
        
        if ($transNode -ne $null) {
            $txt = $transNode.InnerText
            if ($txt -ne $null -and $txt -ne "") {
                $hasTranslation = $true
            }
        }
        
        if (-not $hasTranslation) {
            foreach ($t in $translations) {
                if ($t[0] -eq $ctxName -and $t[1] -eq $src) {
                    $newValue = $t[2]
                    if ($transNode -eq $null) {
                        $transNode = $xml.CreateElement("translation")
                        $msg.AppendChild($transNode)
                    }
                    $transNode.InnerText = $newValue
                    if ($transNode.type -eq "unfinished") {
                        $transNode.RemoveAttribute("type")
                    }
                    $updated++
                    break
                }
            }
        }
    }
}

if ($updated -gt 0) {
    $xml.Save($tsFile)
    Write-Host "  Updated $updated translations" -ForegroundColor Green
} else {
    Write-Host "  No updates needed" -ForegroundColor Gray
}

Write-Host "Done!" -ForegroundColor Cyan
