Skip to content

Instantly share code, notes, and snippets.

@KoheiKanagu
Created March 29, 2025 15:50
Show Gist options
  • Save KoheiKanagu/7db2b7635716652a66764080e8719819 to your computer and use it in GitHub Desktop.
Save KoheiKanagu/7db2b7635716652a66764080e8719819 to your computer and use it in GitHub Desktop.
https://github.com/nsk4762jp/OxygenNotIncluded-Japanese のpoファイルのタグに不整合が無いかチェックするスクリプト
#!/bin/bash
set -euo pipefail
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <poファイル> <出力JSONファイル>"
exit 1
fi
input_file="$1"
output_file="$2"
# エラー情報とカウンターを直接awkスクリプト内で処理
awk -v output_file="$output_file" '
BEGIN {
total = 0
errors = 0
printf "{\n \"summary\": {\n \"total\": 0,\n \"errors\": 0\n },\n \"error_details\": [\n" > output_file
current_msgctxt = ""
current_msgid = ""
current_msgstr = ""
in_msgid = 0
in_msgstr = 0
first_error = 1
}
function check_tags(msgid, msgstr, msgctxt, msgid_tags, msgstr_tags, msgid_count, msgstr_count) {
# タグを抽出して配列に格納
msgid_count = 0
msgstr_count = 0
split("", msgid_tags)
split("", msgstr_tags)
while (match(msgid, /<[^>]*>/)) {
msgid_tags[++msgid_count] = substr(msgid, RSTART, RLENGTH)
msgid = substr(msgid, RSTART + RLENGTH)
}
while (match(msgstr, /<[^>]*>/)) {
msgstr_tags[++msgstr_count] = substr(msgstr, RSTART, RLENGTH)
msgstr = substr(msgstr, RSTART + RLENGTH)
}
# タグ数のチェック
if (msgid_count != msgstr_count) {
if (!first_error) printf "," > output_file
printf "{\n" > output_file
printf " \"type\": \"tag_count_mismatch\",\n" > output_file
printf " \"context\": %s,\n", quote(msgctxt) > output_file
printf " \"msgid\": %s,\n", quote(current_msgid) > output_file
printf " \"msgstr\": %s,\n", quote(current_msgstr) > output_file
printf " \"msgid_tag_count\": %d,\n", msgid_count > output_file
printf " \"msgstr_tag_count\": %d\n", msgstr_count > output_file
printf "}" > output_file
first_error = 0
errors++
return 0
}
# タグの整合性チェック
if (msgid_count > 0) {
for (i = 1; i <= msgid_count; i++) {
found = 0
for (j = 1; j <= msgstr_count; j++) {
if (msgid_tags[i] == msgstr_tags[j]) {
found = 1
break
}
}
if (!found) {
if (!first_error) printf "," > output_file
printf "{\n" > output_file
printf " \"type\": \"tag_elements_mismatch\",\n" > output_file
printf " \"context\": %s,\n", quote(msgctxt) > output_file
printf " \"msgid\": %s,\n", quote(current_msgid) > output_file
printf " \"msgstr\": %s,\n", quote(current_msgstr) > output_file
printf " \"msgid_elements\": %s,\n", quote(join(msgid_tags, msgid_count)) > output_file
printf " \"msgstr_elements\": %s\n", quote(join(msgstr_tags, msgstr_count)) > output_file
printf "}" > output_file
first_error = 0
errors++
return 0
}
}
}
return 1
}
function quote(str) {
gsub(/\\/, "\\\\", str)
gsub(/"/, "\\\"", str)
gsub(/\n/, "\\n", str)
return "\"" str "\""
}
function join(arr, count, result, i) {
result = ""
for (i = 1; i <= count; i++) {
if (i > 1) result = result "\\n"
result = result arr[i]
}
return result
}
/^msgctxt/ {
if (current_msgid != "" && current_msgstr != "") {
total++
check_tags(current_msgid, current_msgstr, current_msgctxt)
}
current_msgctxt = substr($0, 10, length($0) - 10)
gsub(/"/, "", current_msgctxt)
current_msgid = ""
current_msgstr = ""
in_msgid = 0
in_msgstr = 0
next
}
/^msgid/ {
if ($0 == "msgid \"\"") {
in_msgid = 1
current_msgid = ""
} else {
current_msgid = substr($0, 8, length($0) - 8)
gsub(/"/, "", current_msgid)
in_msgid = 0
}
in_msgstr = 0
next
}
/^msgstr/ {
if ($0 == "msgstr \"\"") {
in_msgstr = 1
current_msgstr = ""
} else {
current_msgstr = substr($0, 9, length($0) - 9)
gsub(/"/, "", current_msgstr)
in_msgstr = 0
}
in_msgid = 0
next
}
/^"/ {
line = substr($0, 2, length($0) - 2)
if (in_msgid) {
current_msgid = current_msgid line
} else if (in_msgstr) {
current_msgstr = current_msgstr line
}
next
}
END {
if (current_msgid != "" && current_msgstr != "") {
total++
check_tags(current_msgid, current_msgstr, current_msgctxt)
}
printf "\n ]\n}" > output_file
# 一時ファイルを使用してsummaryを更新
system(sprintf("mv %s %s.tmp", output_file, output_file))
system(sprintf("sed \"s/\\\"total\\\": 0/\\\"total\\\": %d/\" %s.tmp > %s", total, output_file, output_file))
system(sprintf("sed -i \"\" \"s/\\\"errors\\\": 0/\\\"errors\\\": %d/\" %s", errors, output_file))
system(sprintf("rm %s.tmp", output_file))
# 結果を表示
printf "チェックが完了しました。結果は %s に出力されました。\n", output_file > "/dev/stderr"
printf "合計: %d エントリー\n", total > "/dev/stderr"
printf "エラー: %d エントリー\n", errors > "/dev/stderr"
exit (errors > 0 ? 1 : 0)
}' "$input_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment