1

In POEDIT, It seems that the code analyser removes any PHP comments before parsing the code.

This means that any translation that are not found in a PHP comment (// or #) or Documentation Block ( /* */) are skipped.

Is there any solution to includes them and make it detectable?

Here is an example of what I'm trying to do:

class One{
    public static $enum = array(
        '0' => 'No', // _('No')
        '1' => 'Yes' // _('Yes')
    );
}

I would like POEDIT to detect "// _('No')"

Then after, I could translate like this "echo _(One::$enum[0]);"

Thanks for any further replies :)

Carl.

-- EDIT -- Finally, after 3 years, I think I found a solution quite easy. Because the static variable is public, I can populate it youtside the class:

One::$enum = array(
    '0' => _('No'),
    '1' => _('Yes')
);
class One{
   public static $enum = array();
}

What do you think about this solution?

SequenceDigitale.com
  • 4,038
  • 1
  • 24
  • 23

2 Answers2

2

Rather old thread...
but I figure it might help to indicate my way of doing things

First of all, the major issue with your suggestion is code duplication

public static $enum = array(
    '0' => 'No', // _('No')
    '1' => 'Yes' // _('Yes')
);

This means that you have to remember to update the string twice if you were to change it...
Chances are that you will, at some point, forget or miss one.

This is how I deal with this kind of things

class One 
{
    const ENUM_NO  = 0;
    const ENUM_YES = 1;

    public static function getEnum() (
        return [
            self::ENUM_NO  => _('No'),
            self::ENUM_YES => _('Yes')
        ];
    );
}

Ok, so that means quite some extra lines...
But gettext works out of the box and the strings are only to be edited in one single location

Agreed, the best thing would be for PHP to allow

class One 
{
    public static $enum = array(
        '0' => _('No'),
        '1' => _('Yes')
    );
}
dGo
  • 2,951
  • 1
  • 18
  • 11
1

The way gettext works, xgettext (which is what Poedit calls -- nothing more going on in it) extracts translatable strings from source code. If a string is not used in source code, then it will obviously never be used at runtime and there's no point in translating it -- the translation wouldn't be used. Comments are not part of the code, so of course xgettext ignores them. It simply wouldn't make any sense to do otherwise.

Gettext has gettext_noop() function, described well in the manual to handle the rare situation like yours.

You may want to define some helper function like that and use it as an additional keyword in Poedit, although the answer to this StackOverflow question explains why such a thing is bit pointless in PHP.

Community
  • 1
  • 1
Václav Slavík
  • 6,445
  • 2
  • 28
  • 25
  • Hi! Thanks you for your reply. gettext_noop has the same problem, you cannot use any functions during class variable declaration. Is there any xgettext options to make it accept PHP comments? – SequenceDigitale.com Nov 14 '13 at 21:09
  • No -- it wouldn't make any sense, really. You should consider changing the code to work with PHP's gettext API instead. – Václav Slavík Nov 23 '13 at 06:56