You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用正则替换WordPress翻译文件中msgstr行内容为对应msgid内容?

解决LocoTranslate翻译文件msgstr同步为msgid内容的方案

方法一:Notepad++ 正则替换

适合快速处理单文件:

  • 打开目标.po翻译文件,按下Ctrl+H打开替换面板
  • 勾选「正则表达式」,同时勾选底部的「. 匹配换行符」(必须开启,否则无法跨行匹配)
  • 在「查找目标」输入:msgid "(.*)"\r?\nmsgstr ".*"
  • 在「替换为」输入:msgid "$1"\r\nmsgstr "$1"
  • 点击「全部替换」即可完成所有msgstr内容与对应msgid的同步

方法二:VS Code 正则替换

操作简洁,无需额外配置:

  • 打开翻译文件,按下Ctrl+H打开替换面板
  • 点击右上角的.*按钮启用正则表达式模式
  • 查找内容:msgid "(.*)"\nmsgstr ".*"
  • 替换内容:msgid "$1"\nmsgstr "$1"
  • 点击「替换全部」按钮完成批量替换

方法三:Python脚本批量处理

针对超大型文件或批量处理场景效率更高:

import re

# 替换为你的翻译文件路径
input_file = "your-translation-file.po"
output_file = "updated-translation-file.po"

with open(input_file, 'r', encoding='utf-8') as f:
    content = f.read()

# 执行正则替换,同步msgstr与msgid内容
updated_content = re.sub(
    r'msgid "(.*)"\nmsgstr ".*"',
    r'msgid "\1"\nmsgstr "\1"',
    content
)

with open(output_file, 'w', encoding='utf-8') as f:
    f.write(updated_content)

将脚本中的文件路径替换为实际路径,运行后会生成更新后的翻译文件。

内容的提问来源于stack exchange,提问作者Maramal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 08:35:24