Fixed translations file dumper behavior
This commit is contained in:
parent
e1f7b7886e
commit
1c41ae7631
@ -67,36 +67,37 @@ abstract class FileDumper implements DumperInterface
|
||||
throw new InvalidArgumentException('The file dumper needs a path option.');
|
||||
}
|
||||
|
||||
$hasMessageFormatter = class_exists(\MessageFormatter::class);
|
||||
|
||||
// save a file for each domain
|
||||
foreach ($messages->getDomains() as $domain) {
|
||||
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
|
||||
if (!file_exists($fullpath)) {
|
||||
$directory = \dirname($fullpath);
|
||||
if ($hasMessageFormatter) {
|
||||
$defaultDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
|
||||
$altDomain = $domain;
|
||||
} 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)) {
|
||||
throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
|
||||
}
|
||||
}
|
||||
|
||||
$intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
|
||||
$intlMessages = $messages->all($intlDomain);
|
||||
|
||||
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);
|
||||
}
|
||||
if (file_exists($altPath)) {
|
||||
// clear alternative translation file
|
||||
file_put_contents($altPath, $this->formatCatalogue(new MessageCatalogue($messages->getLocale()), $altDomain, $options));
|
||||
}
|
||||
|
||||
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
|
||||
file_put_contents($defaultPath, $this->formatCatalogue($messages, $domain, $options));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,11 +27,15 @@ class FileDumperTest extends TestCase
|
||||
$dumper = new ConcreteFileDumper();
|
||||
$dumper->dump($catalogue, ['path' => $tempDir]);
|
||||
|
||||
$this->assertFileExists($tempDir.'/messages.en.concrete');
|
||||
$suffix = class_exists(\MessageFormatter::class) ? '+intl-icu' : '';
|
||||
$this->assertFileExists($tempDir."/messages$suffix.en.concrete");
|
||||
|
||||
@unlink($tempDir.'/messages.en.concrete');
|
||||
@unlink($tempDir."/messages$suffix.en.concrete");
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires extension intl
|
||||
*/
|
||||
public function testDumpIntl()
|
||||
{
|
||||
$tempDir = sys_get_temp_dir();
|
||||
@ -42,13 +46,11 @@ class FileDumperTest extends TestCase
|
||||
$catalogue->add(['bar' => 'foo'], 'd2+intl-icu');
|
||||
|
||||
$dumper = new ConcreteFileDumper();
|
||||
@unlink($tempDir.'/d2.en.concrete');
|
||||
$dumper->dump($catalogue, ['path' => $tempDir]);
|
||||
|
||||
$this->assertStringEqualsFile($tempDir.'/d1.en.concrete', 'foo=bar');
|
||||
@unlink($tempDir.'/d1.en.concrete');
|
||||
$this->assertFileNotExists($tempDir.'/d1.en.concrete');
|
||||
|
||||
$this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo');
|
||||
$this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo&foo=bar');
|
||||
@unlink($tempDir.'/d1+intl-icu.en.concrete');
|
||||
|
||||
$this->assertFileNotExists($tempDir.'/d2.en.concrete');
|
||||
@ -60,7 +62,8 @@ class FileDumperTest extends TestCase
|
||||
{
|
||||
$tempDir = sys_get_temp_dir();
|
||||
$translationsDir = $tempDir.'/test/translations';
|
||||
$file = $translationsDir.'/messages.en.concrete';
|
||||
$suffix = class_exists(\MessageFormatter::class) ? '+intl-icu' : '';
|
||||
$file = $translationsDir."/messages$suffix.en.concrete";
|
||||
|
||||
$catalogue = new MessageCatalogue('en');
|
||||
$catalogue->add(['foo' => 'bar']);
|
||||
|
Reference in New Issue
Block a user