Using POEdit -- only search for strings in specific domain
Asked Answered
M

2

9

I have created a WordPress theme that I wish to translate.

Inside my theme I use some translations from woocommerce (i.e. <?php _('Your cart', 'woocommerce'); ?>).

Theese woocommerce strings, I do not wish to translate again, naturally, as they are already translated. But when I use poedit to scan my theme, of course it will find ALL text domains.

So my question is:

How can I tell POEdit to only pick up only strings in a specific text domain?

  • __('Something', 'my-text-domain')
  • _e('Something else', 'my-text-domain')

I have found this answer:

However it does not seem to work for me. I have added ri:1,3c to the list as described, but it doesn't change anything, and I have no other clue as to what to do with it.

See how I did it:

poedit sources keywords

M16 answered 24/5, 2015 at 10:3 Comment(2)
Did you really not comprehend that answer (and question) you linked to at all? ri:1,3c is not some magic invocation, it’s for that specific use of a function named ri.Bon
Oh, and that linked answer is very wrong, it confuses domains and contexts, two completely different things, as Andy says there.Bon
P
3

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.

Posse answered 24/5, 2015 at 16:34 Comment(2)
Thats really interesting and very disapointing at the same time … why then even bothering using text domains in WordPress 👎Daglock
@Daglock Because your WordPress instance typically runs several packages, i.e. domains, at the same time (WP itself, the theme, all the plugins), and you don't want their translations to conflict. That's what domains are for.Bon
L
2

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
Lyingin answered 27/2, 2016 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.