Revert "Fixed translations file dumper behavior"

This commit is contained in:
Yonel Ceruto 2020-01-15 08:29:06 -05:00
parent be84687263
commit 56e79fefa1
2 changed files with 28 additions and 32 deletions

View File

@ -67,37 +67,36 @@ abstract class FileDumper implements DumperInterface
throw new InvalidArgumentException('The file dumper needs a path option.'); throw new InvalidArgumentException('The file dumper needs a path option.');
} }
$hasMessageFormatter = class_exists(\MessageFormatter::class);
// save a file for each domain // save a file for each domain
foreach ($messages->getDomains() as $domain) { foreach ($messages->getDomains() as $domain) {
if ($hasMessageFormatter) { $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
$defaultDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX; if (!file_exists($fullpath)) {
$altDomain = $domain; $directory = \dirname($fullpath);
} else {
$defaultDomain = $domain;
$altDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
}
$defaultPath = $options['path'].'/'.$this->getRelativePath($defaultDomain, $messages->getLocale());
$altPath = $options['path'].'/'.$this->getRelativePath($altDomain, $messages->getLocale());
if (!file_exists($defaultPath) && file_exists($altPath)) {
[$defaultPath, $altPath] = [$altPath, $defaultPath];
}
if (!file_exists($defaultPath)) {
$directory = \dirname($defaultPath);
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) { if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory)); throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
} }
} }
if (file_exists($altPath)) { $intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
// clear alternative translation file $intlMessages = $messages->all($intlDomain);
file_put_contents($altPath, $this->formatCatalogue(new MessageCatalogue($messages->getLocale()), $altDomain, $options));
if ($intlMessages) {
$intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale());
file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options));
$messages->replace([], $intlDomain);
try {
if ($messages->all($domain)) {
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
}
continue;
} finally {
$messages->replace($intlMessages, $intlDomain);
}
} }
file_put_contents($defaultPath, $this->formatCatalogue($messages, $domain, $options)); file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
} }
} }

View File

@ -27,15 +27,11 @@ class FileDumperTest extends TestCase
$dumper = new ConcreteFileDumper(); $dumper = new ConcreteFileDumper();
$dumper->dump($catalogue, ['path' => $tempDir]); $dumper->dump($catalogue, ['path' => $tempDir]);
$suffix = class_exists(\MessageFormatter::class) ? '+intl-icu' : ''; $this->assertFileExists($tempDir.'/messages.en.concrete');
$this->assertFileExists($tempDir."/messages$suffix.en.concrete");
@unlink($tempDir."/messages$suffix.en.concrete"); @unlink($tempDir.'/messages.en.concrete');
} }
/**
* @requires extension intl
*/
public function testDumpIntl() public function testDumpIntl()
{ {
$tempDir = sys_get_temp_dir(); $tempDir = sys_get_temp_dir();
@ -46,11 +42,13 @@ class FileDumperTest extends TestCase
$catalogue->add(['bar' => 'foo'], 'd2+intl-icu'); $catalogue->add(['bar' => 'foo'], 'd2+intl-icu');
$dumper = new ConcreteFileDumper(); $dumper = new ConcreteFileDumper();
@unlink($tempDir.'/d2.en.concrete');
$dumper->dump($catalogue, ['path' => $tempDir]); $dumper->dump($catalogue, ['path' => $tempDir]);
$this->assertFileNotExists($tempDir.'/d1.en.concrete'); $this->assertStringEqualsFile($tempDir.'/d1.en.concrete', 'foo=bar');
@unlink($tempDir.'/d1.en.concrete');
$this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo&foo=bar'); $this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo');
@unlink($tempDir.'/d1+intl-icu.en.concrete'); @unlink($tempDir.'/d1+intl-icu.en.concrete');
$this->assertFileNotExists($tempDir.'/d2.en.concrete'); $this->assertFileNotExists($tempDir.'/d2.en.concrete');
@ -62,8 +60,7 @@ class FileDumperTest extends TestCase
{ {
$tempDir = sys_get_temp_dir(); $tempDir = sys_get_temp_dir();
$translationsDir = $tempDir.'/test/translations'; $translationsDir = $tempDir.'/test/translations';
$suffix = class_exists(\MessageFormatter::class) ? '+intl-icu' : ''; $file = $translationsDir.'/messages.en.concrete';
$file = $translationsDir."/messages$suffix.en.concrete";
$catalogue = new MessageCatalogue('en'); $catalogue = new MessageCatalogue('en');
$catalogue->add(['foo' => 'bar']); $catalogue->add(['foo' => 'bar']);