Last active
November 5, 2019 08:20
-
-
Save rugbymauri/0409eb342715341438f03ad9efc7de6b to your computer and use it in GitHub Desktop.
Yaml Translation FileDump with append functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
translation.dumper.yaml: | |
class: App\Dumper\YamlxFileDumper | |
tags: | |
- { name: translation.dumper, alias: yaml } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Dumper; | |
use Symfony\Component\Translation\Dumper\FileDumper; | |
use Symfony\Component\Translation\Dumper\YamlFileDumper; | |
use Symfony\Component\Translation\Exception\InvalidArgumentException; | |
use Symfony\Component\Translation\Exception\LogicException; | |
use Symfony\Component\Translation\Exception\RuntimeException; | |
use Symfony\Component\Translation\MessageCatalogue; | |
use Symfony\Component\Translation\Util\ArrayConverter; | |
use Symfony\Component\Yaml\Yaml; | |
class YamlxFileDumper extends YamlFileDumper | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getExtension() | |
{ | |
return 'yaml'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function dump(MessageCatalogue $messages, $options = []) | |
{ | |
if (!\array_key_exists('path', $options)) { | |
throw new InvalidArgumentException('The file dumper needs a path option.'); | |
} | |
// 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 (!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($fullpath)) { | |
$hasNewMessages = false; | |
$existingYaml = Yaml::parseFile($fullpath); | |
if ($existingYaml) { | |
$flattenYaml = $this->array_flat($existingYaml); | |
$newMessages = new MessageCatalogue($messages->getLocale()); | |
$all = $messages->all($domain); | |
foreach ($all as $k => $me) { | |
if (!isset($flattenYaml[$k])) { | |
$hasNewMessages = true; | |
$newMessages->add([$k => $me], $domain); | |
} | |
} | |
if ($hasNewMessages) { | |
$output = file_get_contents($fullpath) . PHP_EOL . $this->formatCatalogue($newMessages, $domain, $options); | |
file_put_contents($fullpath, $output); | |
} | |
} | |
} else { | |
$output = $this->formatCatalogue($messages, $domain, $options); | |
file_put_contents($fullpath, $output); | |
} | |
} | |
} | |
/** | |
* Gets the relative file path using the template. | |
*/ | |
private function getRelativePath(string $domain, string $locale): string | |
{ | |
return strtr($this->relativePathTemplate, [ | |
'%domain%' => $domain, | |
'%locale%' => $locale, | |
'%extension%' => $this->getExtension(), | |
]); | |
} | |
function array_flat(array $array, string $prefix = ''): array | |
{ | |
$result = array(); | |
foreach ($array as $key => $value) { | |
$new_key = $prefix . (empty($prefix) ? '' : '.') . $key; | |
if (is_array($value)) { | |
$result = array_merge($result, $this->array_flat($value, $new_key)); | |
} else { | |
$result[$new_key] = $value; | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment