Skip to content

Instantly share code, notes, and snippets.

@maokwen
Last active October 18, 2022 10:50
Show Gist options
  • Select an option

  • Save maokwen/74156506a559f6e04a6a91b3c7c83b04 to your computer and use it in GitHub Desktop.

Select an option

Save maokwen/74156506a559f6e04a6a91b3c7c83b04 to your computer and use it in GitHub Desktop.
Turing Complete 翻译时用到的脚本
# 将由 ParaTranz 生成的 json 格式转换为待上传的格式
# Usage: json_to_translation.ps1 stage input.json output.txt
# stage: 1.已翻译 3.已校对 5.已审核
$stage = $args[0]
$json = (Get-Content -Encoding utf8 $args[1] | ConvertFrom-Json)
$out = $args[2]
Out-File $out -Encoding utf8 -NoNewline
foreach ($trans in $json) {
if ($trans.stage -ge $stage) {
$output = "=== " + $trans.context + " ===`n"
Write-Output $output | Add-Content $out -Encoding utf8 -NoNewline
if ($trans -match '\\n') {
$output = '$' + $trans.key + "`n" + $trans.translation.replace('\n', "`n")
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
} else {
$output = '$' + $trans.key + ' ' + $trans.translation
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
}
Write-Output "`n" | Add-Content $out -Encoding utf8 -NoNewline
}
}
# 校验专用,把已审核的转换为“translation”形式,把已检查的转换为“key+translation”形式,把未检查的转换为“key”形式
# 如果是第一次校验,推荐把 elseif 后面的值从 3 改为 1
# Usage: json_to_translation_for_review.ps1 input.json output.txt
$json = (Get-Content -Encoding utf8 $args[0] | ConvertFrom-Json)
$out = $args[1]
Out-File $out -Encoding utf8 -NoNewline
foreach ($trans in $json) {
# $output = "=== " + $trans.context + " ===`n"
# Write-Output $output | Add-Content $out -Encoding utf8 -NoNewline
if ($trans.stage -eq 5) {
if ($trans -match '\\n') {
$output = '$' + $trans.key + "`n" + $trans.translation.replace('\n', "`n")
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
} else {
$output = '$' + $trans.key + ' ' + $trans.translation
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
}
} elseif ($trans.stage -eq 3) {
if ($trans -match '\\n') {
$output = '$' + $trans.key + "`n" + $trans.key + $trans.translation.replace('\n', "`n")
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
} else {
$output = '$' + $trans.key + ' ' + $trans.key + $trans.translation
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
}
} else {
if ($trans -match '\\n') {
$output = '$' + $trans.key + "`n" + $trans.key
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
} else {
$output = '$' + $trans.key + ' ' + $trans.key
Write-Output $output "`n" | Add-Content $out -Encoding utf8 -NoNewline
}
}
Write-Output "`n" | Add-Content $out -Encoding utf8 -NoNewline
}
# Usage: source_to_json.ps1 input.txt > output.json
$file = $args[0]
$context_regex = '^=== (.+) ===$'
$oneline_regex = '^\$(\d+)\* (.+)$'
$multline_regex = '^\$(\d+)\*\s*$'
$id = ""
$source = ""
$context = ""
$multline_mode = $false
$array = [System.Collections.ArrayList]::new()
foreach ($line in Get-Content -Path $file) {
if ($line -match $context_regex) {
if ($multline_mode) {
$multline_mode = $false
$source = $source.trimend("`n")
$tmp = $array.Add(@{
key = $id;
source = $source;
context = $context
})
}
$context = $Matches.1
}
elseif ($line -match $oneline_regex) {
if ($multline_mode) {
$multline_mode = $false
$source = $source.trimend("`n")
$tmp = $array.Add(@{
key = $id;
source = $source;
context = $context
})
}
$id = $Matches.1
$source = $Matches.2
$tmp = $array.Add(@{
key = $id;
source = $source;
context = $context
})
}
elseif ($line -match $multline_regex) {
if ($multline_mode) {
$multline_mode = $false
$source = $source.trimend("`n")
$tmp = $array.Add(@{
key = $id;
source = $source;
context = $context
})
}
$id = $Matches.1
$source = ''
$multline_mode = $true
}
elseif ($multline_mode) {
$source += $line
$source += "`n"
}
}
($array.ToArray() | ConvertTo-Json -Compress) -replace '\},\{', "},`n{"
# 将翻译好的文件转换为 ParaTranz 中的 json 格式
# Usage: translate_to_json.ps1 input.txt > output.json
$file = $args[0]
$context_regex = '^=== (.+) ===$'
$oneline_regex = '^\$(\d+)\* (.+)$'
$multline_regex = '^\$(\d+)\*\s*$'
$id = ""
$translation = ""
$context = ""
$multline_mode = $false
$array = [System.Collections.ArrayList]::new()
foreach ($line in Get-Content -Path $file) {
if ($line -match $context_regex) {
if ($multline_mode) {
$multline_mode = $false
$translation = $translation.trimend("`n")
$tmp = $array.Add(@{
key = $id;
translation = $translation;
context = $context
})
}
$context = $Matches.1
}
elseif ($line -match $oneline_regex) {
if ($multline_mode) {
$multline_mode = $false
$translation = $translation.trimend("`n")
$tmp = $array.Add(@{
key = $id;
translation = $translation;
context = $context
})
}
$id = $Matches.1
$translation = $Matches.2
$tmp = $array.Add(@{
key = $id;
translation = $translation;
context = $context
})
}
elseif ($line -match $multline_regex) {
if ($multline_mode) {
$multline_mode = $false
$translation = $translation.trimend("`n")
$tmp = $array.Add(@{
key = $id;
translation = $translation;
context = $context
})
}
$id = $Matches.1
$translation = ''
$multline_mode = $true
}
elseif ($multline_mode) {
$translation += $line
$translation += "`n"
}
}
($array.ToArray() | ConvertTo-Json -Compress) -replace '\},\{', "},`n{"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment