Using POEdit — only search for strings in specific domain

↘锁芯ラ 提交于 2019-12-01 17:55:33

Neither GNU gettext tools nor Poedit (which uses them) support this particular misuse of gettext.

In gettext, domain is roughly “a piece of software” — a program, a library, a plugin, a theme. As such, it typically resides in a single directory tree and is alone there — or at the very least, if you have multiple pieces=domains, you have them organized sanely into some subdirectories that you can limit the extraction to.

Mixing and matching domains within a single file as you do is not how gettext was intended to be used, and there’s no reasonable solution to handle it other than using your own helper function, e.g. by wrapping all woocommerce texts into __woo (which you must define, obviously) and not adding that to the list of keywords in Poedit.

nachtigall

The best solution would be if gettext or Poedit were able to restrict the translations to a certain domain. As Václav points out, this is not possible.

A workaround might be to postprocess the my-text-domain.pot or *.po files and remove all strings that are actually from other plugins like woocommerce:

#!/bin/bash

# DRY_RUN="echo"
DRY_RUN=""
MY_DOMAIN="my-text-domain"

for PO in *.po; do
  echo "Removing other domains from file: $PO"
  # OTHER_DOMAINS are all the domain that are not MY_DOMAIN
  OTHER_DOMAINS=`grep "# @ " $PO  | grep -v $MY_DOMAIN | sort | uniq | grep -o '\w*'`
  for OTHER_DOMAIN in $OTHER_DOMAINS; do
    echo "  Removing ${OTHER_DOMAIN}..."
    # Replace ever "foreign" strings with a linebreak
    # Begins with "# @ OTHER_DOMAIN" and ends with an empty line
    $DRY_RUN perl -0777 -i -pe "s/# @ ${OTHER_DOMAIN}\n.+?\n\n//smg" $PO
  done
done
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!