输出pot文件里已翻译的文本和未翻译的文本的Python脚本

filter translated strings

import os
import sys

# 获取脚本所在的目录,并切换过去
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# 配置文件名
input_file = 'zh_CN.po'
output_file = 'zh_CN_fixed.po'

def clean_po():
    if not os.path.exists(input_file):
        print(f"找不到文件: {input_file}")
        return

    with open(input_file, 'r', encoding='utf-8-sig') as f:
        lines = f.readlines()

    result = []
    current_entry = []
    is_translated = False
    
    # 首先保留 Header (直到第一个空白行或 msgid 出现)
    header_done = False
    header_end_idx = 0
    for i, line in enumerate(lines):
        if line.startswith('msgid ""') and i + 1 < len(lines) and line.strip():
            header_end_idx = i
            break
    
    # 提取并保护 Header
    header_lines = []
    for i in range(len(lines)):
        header_lines.append(lines[i])
        if 'msgstr' in lines[i] and i + 1 < len(lines) and lines[i+1].strip() == "":
            header_end_idx = i + 1
            break
    
    result.extend(header_lines)

    # 处理剩下的条目
    current_block = []
    has_content = False
    
    for line in lines[header_end_idx + 1:]:
        current_block.append(line)
        
        # 检查 msgstr 是否有内容
        if line.startswith('msgstr "') and not line.strip() == 'msgstr ""':
            has_content = True
            
        # 如果遇到空行,说明一个条目结束了
        if line.strip() == "":
            if has_content:
                result.extend(current_block)
            current_block = []
            has_content = False

    # 写入新文件
    with open(output_file, 'w', encoding='utf-8') as f:
        f.writelines(result)
    
    print(f"处理完成!已过滤未翻译字符串,结果保存至: {output_file}")

if __name__ == "__main__":
    clean_po()

filter untranslated strings

import os

# 自动定位:脚本在哪,就在哪运行
os.chdir(os.path.dirname(os.path.abspath(__file__)))

def extract_untranslated(filename):
    output_file = "ONLY_UNTRANSLATED_" + filename
    
    # 使用 utf-8-sig 容忍 Windows 的 BOM 头
    with open(filename, 'r', encoding='utf-8-sig', errors='ignore') as f:
        content = f.read()

    # 按双换行符分割条目(PO 文件的标准结构)
    entries = content.split('\n\n')
    
    result = []
    # 强制保留第一个条目(通常是 Header 头部信息)
    if entries:
        result.append(entries[0])

    count = 0
    for entry in entries[1:]:
        # 核心逻辑:如果包含空引号的 msgstr,且不包含已经翻译的内容
        if 'msgstr ""' in entry and 'msgstr "' + '"' in entry:
            # 排除掉只有空格的特殊情况
            result.append(entry)
            count += 1

    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n\n'.join(result))
    
    return output_file, count

# 自动寻找当前目录下的 po 或 pot 文件
files = [f for f in os.listdir('.') if f.endswith(('.po', '.pot'))]

if not files:
    print("❌ 错误:文件夹里没有找到任何 .po 或 .pot 文件!")
else:
    for f in files:
        try:
            out, num = extract_untranslated(f)
            print(f"✅ 成功!从 [{f}] 提取了 {num} 条未翻译字符串。")
            print(f"   结果已保存至: {out}")
        except Exception as e:
            print(f"❌ 处理 {f} 时出错: {e}")
×
输出pot文件里已翻译的文本和未翻译的文本的Python脚本

输出pot文件里已翻译的文本和未翻译的文本的Python脚本

filter translated st...