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

如何让Poedit兼容ES6模板字符串,解决翻译字符串提取报错问题?

Fixing Poedit's "RegExp literal terminated too early" with ES6 Template Strings

Hey there, let's work through this frustrating issue you're hitting! That warning definitely stems from Poedit's underlying gettext tooling struggling with ES6 template strings—gettext's default JavaScript parser doesn't handle the ${} interpolation syntax correctly, leading it to misinterpret parts of your code as broken regex literals.

Here are a few practical solutions to get past this:

1. Switch Poedit to a JS-friendly parser

Poedit lets you replace the default gettext parser with tools built specifically for modern JavaScript. Here's how:

  • First, install xgettext-js (a JS-focused gettext extractor) via npm: npm install -g xgettext-js
  • Open your Poedit catalog, go to Catalog > Settings
  • Navigate to the Sources tab, find the Parser section
  • For JavaScript files, replace the default parser command with:
    xgettext-js --language=JavaScript --from-code=UTF-8 -o - %F
    
  • Save the settings and run "Update from code" again—this parser understands ES6 template strings and should avoid the regex error.

2. Temporary code workaround (quick fix)

If you can't switch parsers right now, adjust your code to avoid putting interpolated template strings directly inside your translation functions. For example:

  • Instead of this (which triggers the error):
    const myVariable = 5;
    const myString = __('My variable value is: ${myVariable}');
    
  • Use gettext's built-in placeholder syntax:
    const myVariable = 5;
    const myString = __('My variable value is: %s', myVariable);
    
  • Or split the string and variable:
    const myVariable = 5;
    const myString = __('My variable value is:') + ` ${myVariable}`;
    

This keeps the translation string plain and recognizable to Poedit's default parser.

3. Switch to a modern i18n toolchain (long-term solution)

If you're working with lots of ES6+ code, Poedit's gettext foundation might feel limiting. Consider tools that are built for modern JavaScript but still work with .po files if you need them:

  • i18next: Use i18next-parser to extract translations from template strings, then export to .po format for editing in Poedit (or use i18next's own tools).
  • LinguiJS: A lightweight library with first-class support for ES6 template strings and JSX, plus a CLI that generates gettext-compatible files.
  • react-intl (for React projects): Its extraction tool handles template strings seamlessly, and you can convert its output to .po files if needed.

These tools eliminate the gettext compatibility issues entirely since they're designed for the JS ecosystem.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 19:22:48