Commaaa2
This commit is contained in:
@@ -20,9 +20,6 @@ use Symfony\Component\Translation\MessageCatalogue;
|
||||
*/
|
||||
class ArrayLoader implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
|
||||
{
|
||||
$resource = $this->flatten($resource);
|
||||
@@ -46,9 +43,11 @@ class ArrayLoader implements LoaderInterface
|
||||
foreach ($messages as $key => $value) {
|
||||
if (\is_array($value)) {
|
||||
foreach ($this->flatten($value) as $k => $v) {
|
||||
$result[$key.'.'.$k] = $v;
|
||||
if (null !== $v) {
|
||||
$result[$key.'.'.$k] = $v;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} elseif (null !== $value) {
|
||||
$result[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,11 +22,8 @@ class CsvFileLoader extends FileLoader
|
||||
{
|
||||
private string $delimiter = ';';
|
||||
private string $enclosure = '"';
|
||||
private string $escape = '\\';
|
||||
private string $escape = '';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadResource(string $resource): array
|
||||
{
|
||||
$messages = [];
|
||||
@@ -45,7 +42,7 @@ class CsvFileLoader extends FileLoader
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === \count($data)) {
|
||||
if (!str_starts_with($data[0], '#') && isset($data[1]) && 2 === \count($data)) {
|
||||
$messages[$data[0]] = $data[1];
|
||||
}
|
||||
}
|
||||
@@ -55,8 +52,10 @@ class CsvFileLoader extends FileLoader
|
||||
|
||||
/**
|
||||
* Sets the delimiter, enclosure, and escape character for CSV.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = '\\')
|
||||
public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = '')
|
||||
{
|
||||
$this->delimiter = $delimiter;
|
||||
$this->enclosure = $enclosure;
|
||||
|
||||
@@ -21,9 +21,6 @@ use Symfony\Component\Translation\MessageCatalogue;
|
||||
*/
|
||||
abstract class FileLoader extends ArrayLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
|
||||
{
|
||||
if (!stream_is_local($resource)) {
|
||||
@@ -37,9 +34,7 @@ abstract class FileLoader extends ArrayLoader
|
||||
$messages = $this->loadResource($resource);
|
||||
|
||||
// empty resource
|
||||
if (null === $messages) {
|
||||
$messages = [];
|
||||
}
|
||||
$messages ??= [];
|
||||
|
||||
// not an array
|
||||
if (!\is_array($messages)) {
|
||||
|
||||
@@ -23,9 +23,6 @@ use Symfony\Component\Translation\MessageCatalogue;
|
||||
*/
|
||||
class IcuDatFileLoader extends IcuResFileLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
|
||||
{
|
||||
if (!stream_is_local($resource.'.dat')) {
|
||||
@@ -38,7 +35,7 @@ class IcuDatFileLoader extends IcuResFileLoader
|
||||
|
||||
try {
|
||||
$rb = new \ResourceBundle($locale, $resource);
|
||||
} catch (\Exception $e) {
|
||||
} catch (\Exception) {
|
||||
$rb = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,9 +23,6 @@ use Symfony\Component\Translation\MessageCatalogue;
|
||||
*/
|
||||
class IcuResFileLoader implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
|
||||
{
|
||||
if (!stream_is_local($resource)) {
|
||||
@@ -38,7 +35,7 @@ class IcuResFileLoader implements LoaderInterface
|
||||
|
||||
try {
|
||||
$rb = new \ResourceBundle($locale, $resource);
|
||||
} catch (\Exception $e) {
|
||||
} catch (\Exception) {
|
||||
$rb = null;
|
||||
}
|
||||
|
||||
@@ -71,9 +68,9 @@ class IcuResFileLoader implements LoaderInterface
|
||||
*
|
||||
* @param \ResourceBundle $rb The ResourceBundle that will be flattened
|
||||
* @param array $messages Used internally for recursive calls
|
||||
* @param string $path Current path being parsed, used internally for recursive calls
|
||||
* @param string|null $path Current path being parsed, used internally for recursive calls
|
||||
*/
|
||||
protected function flatten(\ResourceBundle $rb, array &$messages = [], string $path = null): array
|
||||
protected function flatten(\ResourceBundle $rb, array &$messages = [], ?string $path = null): array
|
||||
{
|
||||
foreach ($rb as $key => $value) {
|
||||
$nodePath = $path ? $path.'.'.$key : $key;
|
||||
|
||||
@@ -18,9 +18,6 @@ namespace Symfony\Component\Translation\Loader;
|
||||
*/
|
||||
class IniFileLoader extends FileLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadResource(string $resource): array
|
||||
{
|
||||
return parse_ini_file($resource, true);
|
||||
|
||||
@@ -20,9 +20,6 @@ use Symfony\Component\Translation\Exception\InvalidResourceException;
|
||||
*/
|
||||
class JsonFileLoader extends FileLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadResource(string $resource): array
|
||||
{
|
||||
$messages = [];
|
||||
@@ -42,19 +39,13 @@ class JsonFileLoader extends FileLoader
|
||||
*/
|
||||
private function getJSONErrorMessage(int $errorCode): string
|
||||
{
|
||||
switch ($errorCode) {
|
||||
case \JSON_ERROR_DEPTH:
|
||||
return 'Maximum stack depth exceeded';
|
||||
case \JSON_ERROR_STATE_MISMATCH:
|
||||
return 'Underflow or the modes mismatch';
|
||||
case \JSON_ERROR_CTRL_CHAR:
|
||||
return 'Unexpected control character found';
|
||||
case \JSON_ERROR_SYNTAX:
|
||||
return 'Syntax error, malformed JSON';
|
||||
case \JSON_ERROR_UTF8:
|
||||
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||
default:
|
||||
return 'Unknown error';
|
||||
}
|
||||
return match ($errorCode) {
|
||||
\JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
|
||||
\JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
|
||||
\JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
|
||||
\JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
|
||||
\JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
|
||||
default => 'Unknown error',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,8 +38,6 @@ class MoFileLoader extends FileLoader
|
||||
/**
|
||||
* Parses machine object (MO) format, independent of the machine's endian it
|
||||
* was created on. Both 32bit and 64bit systems are supported.
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadResource(string $resource): array
|
||||
{
|
||||
|
||||
@@ -20,12 +20,9 @@ class PhpFileLoader extends FileLoader
|
||||
{
|
||||
private static ?array $cache = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadResource(string $resource): array
|
||||
{
|
||||
if ([] === self::$cache && \function_exists('opcache_invalidate') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN) && (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) || filter_var(\ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOLEAN))) {
|
||||
if ([] === self::$cache && \function_exists('opcache_invalidate') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOL) && (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) || filter_var(\ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOL))) {
|
||||
self::$cache = null;
|
||||
}
|
||||
|
||||
@@ -33,10 +30,6 @@ class PhpFileLoader extends FileLoader
|
||||
return require $resource;
|
||||
}
|
||||
|
||||
if (isset(self::$cache[$resource])) {
|
||||
return self::$cache[$resource];
|
||||
}
|
||||
|
||||
return self::$cache[$resource] = require $resource;
|
||||
return self::$cache[$resource] ??= require $resource;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,8 +57,6 @@ class PoFileLoader extends FileLoader
|
||||
* - Message IDs are allowed to have other encodings as just US-ASCII.
|
||||
*
|
||||
* Items with an empty id are ignored.
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadResource(string $resource): array
|
||||
{
|
||||
@@ -83,15 +81,15 @@ class PoFileLoader extends FileLoader
|
||||
}
|
||||
$item = $defaults;
|
||||
$flags = [];
|
||||
} elseif ('#,' === substr($line, 0, 2)) {
|
||||
} elseif (str_starts_with($line, '#,')) {
|
||||
$flags = array_map('trim', explode(',', substr($line, 2)));
|
||||
} elseif ('msgid "' === substr($line, 0, 7)) {
|
||||
} elseif (str_starts_with($line, 'msgid "')) {
|
||||
// We start a new msg so save previous
|
||||
// TODO: this fails when comments or contexts are added
|
||||
$this->addMessage($messages, $item);
|
||||
$item = $defaults;
|
||||
$item['ids']['singular'] = substr($line, 7, -1);
|
||||
} elseif ('msgstr "' === substr($line, 0, 8)) {
|
||||
} elseif (str_starts_with($line, 'msgstr "')) {
|
||||
$item['translated'] = substr($line, 8, -1);
|
||||
} elseif ('"' === $line[0]) {
|
||||
$continues = isset($item['translated']) ? 'translated' : 'ids';
|
||||
@@ -102,9 +100,9 @@ class PoFileLoader extends FileLoader
|
||||
} else {
|
||||
$item[$continues] .= substr($line, 1, -1);
|
||||
}
|
||||
} elseif ('msgid_plural "' === substr($line, 0, 14)) {
|
||||
} elseif (str_starts_with($line, 'msgid_plural "')) {
|
||||
$item['ids']['plural'] = substr($line, 14, -1);
|
||||
} elseif ('msgstr[' === substr($line, 0, 7)) {
|
||||
} elseif (str_starts_with($line, 'msgstr[')) {
|
||||
$size = strpos($line, ']');
|
||||
$item['translated'][(int) substr($line, 7, 1)] = substr($line, $size + 3, -1);
|
||||
}
|
||||
@@ -124,7 +122,7 @@ class PoFileLoader extends FileLoader
|
||||
* A .po file could contain by error missing plural indexes. We need to
|
||||
* fix these before saving them.
|
||||
*/
|
||||
private function addMessage(array &$messages, array $item)
|
||||
private function addMessage(array &$messages, array $item): void
|
||||
{
|
||||
if (!empty($item['ids']['singular'])) {
|
||||
$id = stripcslashes($item['ids']['singular']);
|
||||
|
||||
@@ -25,9 +25,6 @@ use Symfony\Component\Translation\MessageCatalogue;
|
||||
*/
|
||||
class QtFileLoader implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
|
||||
{
|
||||
if (!class_exists(XmlUtils::class)) {
|
||||
@@ -67,7 +64,6 @@ class QtFileLoader implements LoaderInterface
|
||||
$domain
|
||||
);
|
||||
}
|
||||
$translation = $translation->nextSibling;
|
||||
}
|
||||
|
||||
if (class_exists(FileResource::class)) {
|
||||
|
||||
@@ -28,9 +28,6 @@ use Symfony\Component\Translation\Util\XliffUtils;
|
||||
*/
|
||||
class XliffFileLoader implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
|
||||
{
|
||||
if (!class_exists(XmlUtils::class)) {
|
||||
@@ -75,7 +72,7 @@ class XliffFileLoader implements LoaderInterface
|
||||
return $catalogue;
|
||||
}
|
||||
|
||||
private function extract(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
|
||||
private function extract(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain): void
|
||||
{
|
||||
$xliffVersion = XliffUtils::getVersionNumber($dom);
|
||||
|
||||
@@ -91,7 +88,7 @@ class XliffFileLoader implements LoaderInterface
|
||||
/**
|
||||
* Extract messages and metadata from DOMDocument into a MessageCatalogue.
|
||||
*/
|
||||
private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
|
||||
private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain): void
|
||||
{
|
||||
$xml = simplexml_import_dom($dom);
|
||||
$encoding = $dom->encoding ? strtoupper($dom->encoding) : null;
|
||||
@@ -104,6 +101,10 @@ class XliffFileLoader implements LoaderInterface
|
||||
|
||||
$file->registerXPathNamespace('xliff', $namespace);
|
||||
|
||||
foreach ($file->xpath('.//xliff:prop') as $prop) {
|
||||
$catalogue->setCatalogueMetadata($prop->attributes()['prop-type'], (string) $prop, $domain);
|
||||
}
|
||||
|
||||
foreach ($file->xpath('.//xliff:trans-unit') as $translation) {
|
||||
$attributes = $translation->attributes();
|
||||
|
||||
@@ -111,12 +112,20 @@ class XliffFileLoader implements LoaderInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
|
||||
$source = (string) (isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source);
|
||||
|
||||
if (isset($translation->target)
|
||||
&& 'needs-translation' === (string) $translation->target->attributes()['state']
|
||||
&& \in_array((string) $translation->target, [$source, (string) $translation->source], true)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the xlf file has another encoding specified, try to convert it because
|
||||
// simple_xml will always return utf-8 encoded values
|
||||
$target = $this->utf8ToCharset((string) ($translation->target ?? $translation->source), $encoding);
|
||||
|
||||
$catalogue->set((string) $source, $target, $domain);
|
||||
$catalogue->set($source, $target, $domain);
|
||||
|
||||
$metadata = [
|
||||
'source' => (string) $translation->source,
|
||||
@@ -139,12 +148,12 @@ class XliffFileLoader implements LoaderInterface
|
||||
$metadata['id'] = (string) $attributes['id'];
|
||||
}
|
||||
|
||||
$catalogue->setMetadata((string) $source, $metadata, $domain);
|
||||
$catalogue->setMetadata($source, $metadata, $domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
|
||||
private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain): void
|
||||
{
|
||||
$xml = simplexml_import_dom($dom);
|
||||
$encoding = $dom->encoding ? strtoupper($dom->encoding) : null;
|
||||
@@ -190,7 +199,7 @@ class XliffFileLoader implements LoaderInterface
|
||||
/**
|
||||
* Convert a UTF8 string to the specified encoding.
|
||||
*/
|
||||
private function utf8ToCharset(string $content, string $encoding = null): string
|
||||
private function utf8ToCharset(string $content, ?string $encoding = null): string
|
||||
{
|
||||
if ('UTF-8' !== $encoding && !empty($encoding)) {
|
||||
return mb_convert_encoding($content, $encoding, 'UTF-8');
|
||||
@@ -199,7 +208,7 @@ class XliffFileLoader implements LoaderInterface
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function parseNotesMetadata(\SimpleXMLElement $noteElement = null, string $encoding = null): array
|
||||
private function parseNotesMetadata(?\SimpleXMLElement $noteElement = null, ?string $encoding = null): array
|
||||
{
|
||||
$notes = [];
|
||||
|
||||
@@ -227,6 +236,6 @@ class XliffFileLoader implements LoaderInterface
|
||||
|
||||
private function isXmlString(string $resource): bool
|
||||
{
|
||||
return 0 === strpos($resource, '<?xml');
|
||||
return str_starts_with($resource, '<?xml');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,11 @@ use Symfony\Component\Yaml\Yaml;
|
||||
*/
|
||||
class YamlFileLoader extends FileLoader
|
||||
{
|
||||
private $yamlParser;
|
||||
private YamlParser $yamlParser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function loadResource(string $resource): array
|
||||
{
|
||||
if (null === $this->yamlParser) {
|
||||
if (!isset($this->yamlParser)) {
|
||||
if (!class_exists(\Symfony\Component\Yaml\Parser::class)) {
|
||||
throw new LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user