php-gettext / Gettext

PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)
MIT License
687 stars 134 forks source link

Calling __() without parameters outputs .po headers #199

Closed devnix closed 5 years ago

devnix commented 5 years ago

Hi! We've found an issue which could expose some information.

On development settings, if you call __() or $t->register() without parameters, it throws a warning like the following:

PHP Warning:  Missing argument 1 for Gettext\GettextTranslator::gettext(), called in /home/devnix/phptest.php on line 13 and defined in /home/devnix/.composer/vendor/gettext/gettext/src/GettextTranslator.php on line 75
PHP Stack trace:
PHP   1. {main}() /home/devnix/phptest.php:0
PHP   2. Gettext\GettextTranslator->gettext() /home/devnix/phptest.php:13
PHP Notice:  Undefined variable: original in /home/devnix/.composer/vendor/gettext/gettext/src/GettextTranslator.php on line 77
PHP Stack trace:
PHP   1. {main}() /home/devnix/phptest.php:0
PHP   2. Gettext\GettextTranslator->gettext() /home/devnix/phptest.php:13

Here is a sample of the code:

use Gettext\GettextTranslator;

$t = new GettextTranslator();
$t->setLanguage('en_US.utf8');
$t->loadDomain('traducciones', 'locale');

$t->register();

echo $t->gettext();

But on our production settings, it doesn't throw anything, and instead of returning null, which is what the original gettext functions would do, it returns the po header

Project-Id-Version: Inglés
Report-Msgid-Bugs-To: 
POT-Creation-Date: 2019-02-21 17:55+0100
PO-Revision-Date: 2019-02-28 10:04+0100
Last-Translator: <hidden information>
Language-Team: 
Language: en
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Generator: Poedit 1.8.7.1
X-Poedit-Basepath: ../../../..
Plural-Forms: nplurals=2; plural=(n != 1);
X-Poedit-SourceCharset: UTF-8
X-Poedit-KeywordsList: _;gettext;gettext_noop
X-Poedit-SearchPath-0: <hidden information>
X-Poedit-SearchPath-1: <hidden information>
X-Poedit-SearchPath-2: <hidden information>
X-Poedit-SearchPath-3: <hidden information>
X-Poedit-SearchPath-4: <hidden information>
X-Poedit-SearchPath-5: <hidden information>
X-Poedit-SearchPath-6: <hidden information>
X-Poedit-SearchPath-7: <hidden information>
X-Poedit-SearchPath-8: <hidden information>
X-Poedit-SearchPath-9: <hidden information>
X-Poedit-SearchPath-10: <hidden information>
X-Poedit-SearchPathExcluded-0: <hidden information>
X-Poedit-SearchPathExcluded-1: <hidden information>
X-Poedit-SearchPathExcluded-2: <hidden information>
X-Poedit-SearchPathExcluded-3: <hidden information>
X-Poedit-SearchPathExcluded-4: <hidden information>
X-Poedit-SearchPathExcluded-5: <hidden information>
X-Poedit-SearchPathExcluded-6: <hidden information>
X-Poedit-SearchPathExcluded-7: <hidden information>
X-Poedit-SearchPathExcluded-8: <hidden information>
X-Poedit-SearchPathExcluded-9: <hidden information>
X-Poedit-SearchPathExcluded-10: <hidden information>
X-Poedit-SearchPathExcluded-11: <hidden information>
X-Poedit-SearchPathExcluded-12: <hidden information>
X-Poedit-SearchPathExcluded-13: <hidden information>
X-Poedit-SearchPathExcluded-14: <hidden information>
X-Poedit-SearchPathExcluded-15: <hidden information>
X-Poedit-SearchPathExcluded-16: <hidden information>
X-Poedit-SearchPathExcluded-17: <hidden information>
X-Poedit-SearchPathExcluded-18: <hidden information>
X-Poedit-SearchPathExcluded-19: <hidden information>
X-Poedit-SearchPathExcluded-20: <hidden information>
X-Poedit-SearchPathExcluded-21: <hidden information>
X-Poedit-SearchPathExcluded-22: <hidden information>
X-Poedit-SearchPathExcluded-23: <hidden information>
X-Poedit-SearchPathExcluded-24: <hidden information>
X-Poedit-SearchPathExcluded-25: <hidden information>
X-Poedit-SearchPathExcluded-26: <hidden information>
X-Poedit-SearchPathExcluded-27: <hidden information>
X-Poedit-SearchPathExcluded-28: <hidden information>
X-Poedit-SearchPathExcluded-29: <hidden information>
X-Poedit-SearchPathExcluded-30: <hidden information>
X-Poedit-SearchPathExcluded-31: <hidden information>
X-Poedit-SearchPathExcluded-32: <hidden information>
X-Poedit-SearchPathExcluded-33: <hidden information>
X-Poedit-SearchPathExcluded-34: <hidden information>
X-Poedit-SearchPathExcluded-35: <hidden information>
X-Poedit-SearchPathExcluded-36: <hidden information>
X-Poedit-SearchPathExcluded-37: <hidden information>
X-Poedit-SearchPathExcluded-38: <hidden information>
X-Poedit-SearchPathExcluded-39: <hidden information>
X-Poedit-SearchPathExcluded-40: <hidden information>
X-Poedit-SearchPathExcluded-41: <hidden information>
X-Poedit-SearchPathExcluded-42: <hidden information>
X-Poedit-SearchPathExcluded-43: <hidden information>
X-Poedit-SearchPathExcluded-44: <hidden information>
X-Poedit-SearchPathExcluded-45: <hidden information>
X-Poedit-SearchPathExcluded-46: <hidden information>
X-Poedit-SearchPathExcluded-47: <hidden information>
X-Poedit-SearchPathExcluded-48: <hidden information>
X-Poedit-SearchPathExcluded-49: <hidden information>
X-Poedit-SearchPathExcluded-50: <hidden information>
X-Poedit-SearchPathExcluded-51: <hidden information>
X-Poedit-SearchPathExcluded-52: <hidden information>
X-Poedit-SearchPathExcluded-53: <hidden information>
X-Poedit-SearchPathExcluded-54: <hidden information>
X-Poedit-SearchPathExcluded-55: <hidden information>
X-Poedit-SearchPathExcluded-56: <hidden information>
X-Poedit-SearchPathExcluded-57: <hidden information>
X-Poedit-SearchPathExcluded-58: <hidden information>
X-Poedit-SearchPathExcluded-59: <hidden information>
X-Poedit-SearchPathExcluded-60: <hidden information>
X-Poedit-SearchPathExcluded-61: <hidden information>
X-Poedit-SearchPathExcluded-62: <hidden information>
X-Poedit-SearchPathExcluded-63: <hidden information>
X-Poedit-SearchPathExcluded-64: <hidden information>
X-Poedit-SearchPathExcluded-65: <hidden information>
X-Poedit-SearchPathExcluded-66: <hidden information>
X-Poedit-SearchPathExcluded-67: <hidden information>
X-Poedit-SearchPathExcluded-68: <hidden information>
X-Poedit-SearchPathExcluded-69: <hidden information>
X-Poedit-SearchPathExcluded-70: <hidden information>
X-Poedit-SearchPathExcluded-71: <hidden information>
X-Poedit-SearchPathExcluded-72: <hidden information>
X-Poedit-SearchPathExcluded-73: <hidden information>
X-Poedit-SearchPathExcluded-74: <hidden information>
X-Poedit-SearchPathExcluded-75: <hidden information>
X-Poedit-SearchPathExcluded-76: <hidden information>
X-Poedit-SearchPathExcluded-77: <hidden information>
X-Poedit-SearchPathExcluded-78: <hidden information>
X-Poedit-SearchPathExcluded-79: <hidden information>
X-Poedit-SearchPathExcluded-80: <hidden information>
X-Poedit-SearchPathExcluded-81: <hidden information>
X-Poedit-SearchPathExcluded-82: <hidden information>
X-Poedit-SearchPathExcluded-83: <hidden information>
X-Poedit-SearchPathExcluded-84: <hidden information>
X-Poedit-SearchPathExcluded-85: <hidden information>
X-Poedit-SearchPathExcluded-86: <hidden information>
X-Poedit-SearchPathExcluded-87: <hidden information>
X-Poedit-SearchPathExcluded-88: <hidden information>
X-Poedit-SearchPathExcluded-89: <hidden information>
X-Poedit-SearchPathExcluded-90: <hidden information>
X-Poedit-SearchPathExcluded-91: <hidden information>
X-Poedit-SearchPathExcluded-92: <hidden information>
X-Poedit-SearchPathExcluded-93: <hidden information>
X-Poedit-SearchPathExcluded-94: <hidden information>
X-Poedit-SearchPathExcluded-95: <hidden information>
X-Poedit-SearchPathExcluded-96: <hidden information>
X-Poedit-SearchPathExcluded-97: <hidden information>
X-Poedit-SearchPathExcluded-98: <hidden information>
X-Poedit-SearchPathExcluded-99: <hidden information>
X-Poedit-SearchPathExcluded-100: <hidden information>
X-Poedit-SearchPathExcluded-101: <hidden information>
X-Poedit-SearchPathExcluded-102: <hidden information>
X-Poedit-SearchPathExcluded-103: <hidden information>
X-Poedit-SearchPathExcluded-104: <hidden information>
X-Poedit-SearchPathExcluded-105: <hidden information>
X-Poedit-SearchPathExcluded-106: <hidden information>
X-Poedit-SearchPathExcluded-107: <hidden information>
X-Poedit-SearchPathExcluded-108: <hidden information>
X-Poedit-SearchPathExcluded-109: <hidden information>
X-Poedit-SearchPathExcluded-110: <hidden information>
X-Poedit-SearchPathExcluded-111: <hidden information>
X-Poedit-SearchPathExcluded-112: <hidden information>
X-Poedit-SearchPathExcluded-113: <hidden information>
X-Poedit-SearchPathExcluded-114: <hidden information>
X-Poedit-SearchPathExcluded-115: <hidden information>
X-Poedit-SearchPathExcluded-116: <hidden information>
X-Poedit-SearchPathExcluded-117: <hidden information>
X-Poedit-SearchPathExcluded-118: <hidden information>
X-Poedit-SearchPathExcluded-119: <hidden information>
X-Poedit-SearchPathExcluded-120: <hidden information>
X-Poedit-SearchPathExcluded-121: <hidden information>
X-Poedit-SearchPathExcluded-122: <hidden information>
X-Poedit-SearchPathExcluded-123: <hidden information>
X-Poedit-SearchPathExcluded-124: <hidden information>
X-Poedit-SearchPathExcluded-125: <hidden information>
X-Poedit-SearchPathExcluded-126: <hidden information>
X-Poedit-SearchPathExcluded-127: <hidden information>
X-Poedit-SearchPathExcluded-128: <hidden information>
X-Poedit-SearchPathExcluded-129: <hidden information>
oscarotero commented 5 years ago

I guess this is because in production the php may be configured in a different way so it does not display errors.

Some formats like mo or php array include the headers under the key "", so if you write, for example: echo __('') you have the headers. To prevent this, simply use the option includeHeaders to false. For example:

$translations->toMoFile('filename.mo', ['includeHeaders' => false]);
devnix commented 5 years ago

I will try it, right now I'm not able to test it on production. Thank you so much!