|
# Set encoding to UTF8
|
$OutputEncoding = [System.Text.Encoding]::UTF8
|
|
# Paths
|
$root = "f:\workFile\2026.1.12yuedan\zy-asrs"
|
$enPath = "$root\src\main\webapp\static\i18n\en.json"
|
$cnPath = "$root\src\main\webapp\static\i18n\zh-cn.json"
|
$mapPath = "$root\scripts\mapping.json"
|
$javaPath = "$root\src\main\java"
|
|
# 1. Load Mapping
|
if (Test-Path $mapPath) {
|
$mapContent = Get-Content $mapPath -Raw -Encoding UTF8 | ConvertFrom-Json
|
} else {
|
Write-Host "Mapping file not found!"
|
$mapContent = $null
|
}
|
|
# 2. Scan Java Files for keys
|
Write-Host "Scanning Java files..."
|
$javaFiles = Get-ChildItem -Path $javaPath -Recurse -Filter "*.java"
|
$foundKeys = @()
|
foreach ($file in $javaFiles) {
|
$content = Get-Content $file.FullName
|
# Regex to capture response.xxx
|
# Matches response. followed by letters, numbers, underscores, dots
|
$matches = [regex]::Matches($content, 'response\.[a-zA-Z0-9_]+')
|
foreach ($match in $matches) {
|
$foundKeys += $match.Value
|
}
|
}
|
|
# Manual keys mentioned by user
|
$manualKeys = @("response.mat_list", "response.menu_list", "response.mat_delete", "response.mat_update", "response.user_detail")
|
$foundKeys += $manualKeys
|
|
# Unique and Sort
|
$foundKeys = $foundKeys | Sort-Object | Get-Unique
|
|
# Exclude technical calls (heuristic)
|
$exclude = @("response.getOutputStream", "response.setContentType", "response.setCharacterEncoding", "response.setHeader", "response.sendRedirect", "response.getWriter", "response.addCookie", "response.setStatus", "response.reset", "response.isSuccessful", "response.getStatus", "response.put")
|
$foundKeys = $foundKeys | Where-Object { $exclude -notcontains $_ }
|
|
Write-Host "Found $($foundKeys.Count) unique 'response.*' keys."
|
|
# Helper Functions
|
function Get-EnTrans($key) {
|
$parts = $key.Replace("response.", "").Split("_")
|
$text = $parts | ForEach-Object { $_.Substring(0,1).ToUpper() + $_.Substring(1) }
|
return $text -join " "
|
}
|
|
function Get-CnTrans($key) {
|
$keyRaw = $key.Replace("response.", "")
|
$parts = $keyRaw.Split("_")
|
$trans = ""
|
foreach ($part in $parts) {
|
$found = $false
|
if ($mapContent -ne $null) {
|
if ($mapContent.PSObject.Properties.Name -contains $part) {
|
$trans += $mapContent.$part
|
$found = $true
|
}
|
}
|
|
if (-not $found) {
|
if ($part.Length -gt 0) {
|
# Fallback: capitalize
|
# Or maybe leave as English if no mapping?
|
# User wants Chinese, but better English than nothing.
|
# But we try to map.
|
# If valid mapping is missing, we might want to flag it?
|
# For now, just Capitalize
|
$trans += $part.Substring(0,1).ToUpper() + $part.Substring(1)
|
}
|
}
|
}
|
return $trans
|
}
|
|
function Process-JsonFile($path, $lang) {
|
Write-Host "Processing $path ..."
|
$jsonContent = Get-Content $path -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
# Convert to hashtable for easier manipulation if possible,
|
# but ConvertFrom-Json returns PSCustomObject.
|
# We will build a new Ordered Dictionary to store results.
|
|
$newDict = [Ordered]@{}
|
|
# Copy existing keys
|
foreach ($prop in $jsonContent.PSObject.Properties) {
|
$newDict[$prop.Name] = $prop.Value
|
}
|
|
# Update/Add keys
|
$addedCount = 0
|
$updatedCount = 0
|
|
foreach ($key in $foundKeys) {
|
$val = ""
|
if ($lang -eq "en") {
|
$val = Get-EnTrans $key
|
} else {
|
$val = Get-CnTrans $key
|
}
|
|
if ($newDict.Contains($key)) {
|
# Check if empty
|
if ([string]::IsNullOrEmpty($newDict[$key])) {
|
$newDict[$key] = $val
|
$updatedCount++
|
Write-Host " Updated empty key: $key -> $val"
|
}
|
} else {
|
# Add new
|
$newDict[$key] = $val
|
$addedCount++
|
Write-Host " Added new key: $key -> $val"
|
}
|
}
|
|
Write-Host " Added: $addedCount, Updated: $updatedCount"
|
|
# Sort keys
|
$sortedDict = [Ordered]@{}
|
$keys = $newDict.Keys | Sort-Object
|
foreach ($k in $keys) {
|
$sortedDict[$k] = $newDict[$k]
|
}
|
|
# Convert back to JSON
|
$jsonOutput = $sortedDict | ConvertTo-Json -Depth 100
|
|
# Unescape Unicode
|
$jsonOutput = [regex]::Replace($jsonOutput, "\\u([0-9a-fA-F]{4})", { param($m) [char][int]::Parse($m.Groups[1].Value, [System.Globalization.NumberStyles]::HexNumber) })
|
|
# Save
|
$jsonOutput | Set-Content -Path $path -Encoding UTF8
|
}
|
|
Process-JsonFile $enPath "en"
|
Process-JsonFile $cnPath "cn"
|
|
Write-Host "Done."
|