$languages = @(
    @("es", "Ejemplo: c:\Windows\notepad.exe {file}"),
    @("fr", "Exemple : c:\Windows\notepad.exe {file}"),
    @("hi", "उदाहरण: c:\Windows\notepad.exe {file}"),
    @("id", "Contoh: c:\Windows\notepad.exe {file}"),
    @("ja", "例: c:\Windows\notepad.exe {file}"),
    @("ko", "예: c:\Windows\notepad.exe {file}"),
    @("ru", "Пример: c:\Windows\notepad.exe {file}"),
    @("zh_TW", "範例：c:\Windows\notepad.exe {file}")
)

$tsDir = "d:\aoyiduo\woterm\woterm\private\languages\ts"

foreach ($lang in $languages) {
    $langCode = $lang[0]
    $translation = $lang[1]
    $tsFile = Join-Path $tsDir "woterm_$langCode.ts.xml"
    
    Write-Host "Processing $langCode..." -ForegroundColor Yellow
    
    $xml = New-Object System.Xml.XmlDocument
    $xml.Load($tsFile)
    
    $ctxNodes = $xml.SelectNodes("/TS/context")
    $found = $false
    
    foreach ($ctxNode in $ctxNodes) {
        $nameNode = $ctxNode.SelectSingleNode("name")
        if ($nameNode -eq $null) { continue }
        if ($nameNode.InnerText -ne "QWoSessionFileAssociationAddDialog") { continue }
        
        $msgNodes = $ctxNode.SelectNodes("message")
        foreach ($msgNode in $msgNodes) {
            $srcNode = $msgNode.SelectSingleNode("source")
            if ($srcNode -eq $null) { continue }
            if ($srcNode.InnerText -ne "Example: c:\Windows\notepad.exe {file}") { continue }
            
            $transNode = $msgNode.SelectSingleNode("translation")
            if ($transNode -eq $null) {
                $transNode = $xml.CreateElement("translation")
                $msgNode.AppendChild($transNode)
            }
            $transNode.InnerText = $translation
            $typeAttr = $transNode.GetAttributeNode("type")
            if ($typeAttr -ne $null -and $typeAttr.Value -eq "unfinished") {
                $transNode.RemoveAttribute("type")
            }
            $found = $true
            Write-Host "  Updated translation" -ForegroundColor Green
            break
        }
        if ($found) { break }
    }
    
    if ($found) {
        $xml.Save($tsFile)
    } else {
        Write-Host "  Not found!" -ForegroundColor Red
    }
}

Write-Host "Done!" -ForegroundColor Cyan
