Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsequent loadFile fails #257

Closed
vielhuber opened this issue Apr 12, 2020 · 4 comments · Fixed by #276
Closed

Subsequent loadFile fails #257

vielhuber opened this issue Apr 12, 2020 · 4 comments · Fixed by #276

Comments

@vielhuber
Copy link

vielhuber commented Apr 12, 2020

<?php
require_once(__DIR__ . '/vendor/autoload.php');
use Gettext\Loader\PoLoader;
use Gettext\Generator\PoGenerator;
use Gettext\Generator\MoGenerator;
use Gettext\Translation;
use Gettext\Translations;

// setup
$poLoader = new PoLoader();
$poGenerator = new PoGenerator();
file_put_contents('test.po', '');
$translations = $poLoader->loadFile('test.po');
$translation = Translation::create('foo', 'bar');
$translations->add($translation);
$poGenerator->generateFile($translations, 'test.po');

// load again
$poLoader = new PoLoader();
$poGenerator = new PoGenerator();
$translations = $poLoader->loadFile('test.po');
var_dump(count($translations->getTranslations())); // this returns "0"

Fix (Generator.php):

    public function generateFile(Translations $translations, string $filename): bool
    {
        $content = $this->generateString($translations);

        $res = file_put_contents($filename, $content) !== false;
        clearstatcache();
        return $res;
    }

Reason: https://www.php.net/manual/de/function.filesize.php#101996

@vielhuber vielhuber changed the title Ciritcal unexpected bug: getTranslations() returns only part of content Subsequent loadFile fails Apr 12, 2020
@vielhuber
Copy link
Author

The fix above is not sufficient: This has to be done inside loadFile.

@ideag
Copy link
Contributor

ideag commented Nov 5, 2021

Hi @oscarotero, I'd like to help fixing this, as we seem to have issues when we try to modify the same PO file several times.
Is there any specific reason for using fopen/fread in the readFile method instead of file_get_contents? In our experience using file_get_contents solves the issues for us, but maybe I'm not aware of something?

@oscarotero
Copy link
Member

Hi @ideag
Sorry, I missed this. Yes, you can use file_get_contents, I don't remember any special reason to use fopen.
Thanks for your contribution!

@ideag
Copy link
Contributor

ideag commented Nov 5, 2021

I'll make a PR 👍

ideag added a commit to ideag/Gettext that referenced this issue Nov 5, 2021
This fixes the issue php-gettext#257, where `filesize()` was misreading size of previously modified file due to PHP low level caching of that data
ideag added a commit to ideag/Gettext that referenced this issue Nov 5, 2021
ideag added a commit to ideag/Gettext that referenced this issue Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants