updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -1,3 +0,0 @@
vendor/
composer.lock
phpunit.xml

View File

@@ -1,6 +1,19 @@
CHANGELOG
=========
4.4.0
-----
* deprecated support for using `null` as the locale in `Translator`
* deprecated accepting STDIN implicitly when using the `lint:xliff` command, use `lint:xliff -` (append a dash) instead to make it explicit.
* Marked the `TranslationDataCollector` class as `@final`.
4.3.0
-----
* Improved Xliff 1.2 loader to load the original file's metadata
* Added `TranslatorPathsPass`
4.2.0
-----

View File

@@ -40,19 +40,19 @@ abstract class AbstractOperation implements OperationInterface
*
* The data structure of this array is as follows:
*
* array(
* 'domain 1' => array(
* 'all' => array(...),
* 'new' => array(...),
* 'obsolete' => array(...)
* ),
* 'domain 2' => array(
* 'all' => array(...),
* 'new' => array(...),
* 'obsolete' => array(...)
* ),
* [
* 'domain 1' => [
* 'all' => [...],
* 'new' => [...],
* 'obsolete' => [...]
* ],
* 'domain 2' => [
* 'all' => [...],
* 'new' => [...],
* 'obsolete' => [...]
* ],
* ...
* )
* ]
*
* @var array The array that stores 'all', 'new' and 'obsolete' messages
*/
@@ -70,7 +70,7 @@ abstract class AbstractOperation implements OperationInterface
$this->source = $source;
$this->target = $target;
$this->result = new MessageCatalogue($source->getLocale());
$this->messages = array();
$this->messages = [];
}
/**
@@ -91,7 +91,7 @@ abstract class AbstractOperation implements OperationInterface
public function getMessages($domain)
{
if (!\in_array($domain, $this->getDomains())) {
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
throw new InvalidArgumentException(sprintf('Invalid domain: "%s".', $domain));
}
if (!isset($this->messages[$domain]['all'])) {
@@ -107,7 +107,7 @@ abstract class AbstractOperation implements OperationInterface
public function getNewMessages($domain)
{
if (!\in_array($domain, $this->getDomains())) {
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
throw new InvalidArgumentException(sprintf('Invalid domain: "%s".', $domain));
}
if (!isset($this->messages[$domain]['new'])) {
@@ -123,7 +123,7 @@ abstract class AbstractOperation implements OperationInterface
public function getObsoleteMessages($domain)
{
if (!\in_array($domain, $this->getDomains())) {
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
throw new InvalidArgumentException(sprintf('Invalid domain: "%s".', $domain));
}
if (!isset($this->messages[$domain]['obsolete'])) {

View File

@@ -29,18 +29,19 @@ class MergeOperation extends AbstractOperation
*/
protected function processDomain($domain)
{
$this->messages[$domain] = array(
'all' => array(),
'new' => array(),
'obsolete' => array(),
);
$this->messages[$domain] = [
'all' => [],
'new' => [],
'obsolete' => [],
];
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
foreach ($this->source->all($domain) as $id => $message) {
$this->messages[$domain]['all'][$id] = $message;
$this->result->add(array($id => $message), $this->source->defines($id, $intlDomain) ? $intlDomain : $domain);
if (null !== $keyMetadata = $this->source->getMetadata($id, $domain)) {
$this->result->setMetadata($id, $keyMetadata, $domain);
$d = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
$this->result->add([$id => $message], $d);
if (null !== $keyMetadata = $this->source->getMetadata($id, $d)) {
$this->result->setMetadata($id, $keyMetadata, $d);
}
}
@@ -48,9 +49,10 @@ class MergeOperation extends AbstractOperation
if (!$this->source->has($id, $domain)) {
$this->messages[$domain]['all'][$id] = $message;
$this->messages[$domain]['new'][$id] = $message;
$this->result->add(array($id => $message), $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
$this->result->setMetadata($id, $keyMetadata, $domain);
$d = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
$this->result->add([$id => $message], $d);
if (null !== $keyMetadata = $this->target->getMetadata($id, $d)) {
$this->result->setMetadata($id, $keyMetadata, $d);
}
}
}

View File

@@ -30,28 +30,29 @@ class TargetOperation extends AbstractOperation
*/
protected function processDomain($domain)
{
$this->messages[$domain] = array(
'all' => array(),
'new' => array(),
'obsolete' => array(),
);
$this->messages[$domain] = [
'all' => [],
'new' => [],
'obsolete' => [],
];
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
// For 'all' messages, the code can't be simplified as ``$this->messages[$domain]['all'] = $target->all($domain);``,
// because doing so will drop messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
//
// For 'new' messages, the code can't be simplied as ``array_diff_assoc($this->target->all($domain), $this->source->all($domain));``
// For 'new' messages, the code can't be simplified as ``array_diff_assoc($this->target->all($domain), $this->source->all($domain));``
// because doing so will not exclude messages like {x: x ∈ target ∧ x ∉ source.all ∧ x ∈ source.fallback}
//
// For 'obsolete' messages, the code can't be simplifed as ``array_diff_assoc($this->source->all($domain), $this->target->all($domain))``
// For 'obsolete' messages, the code can't be simplified as ``array_diff_assoc($this->source->all($domain), $this->target->all($domain))``
// because doing so will not exclude messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
foreach ($this->source->all($domain) as $id => $message) {
if ($this->target->has($id, $domain)) {
$this->messages[$domain]['all'][$id] = $message;
$this->result->add(array($id => $message), $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
if (null !== $keyMetadata = $this->source->getMetadata($id, $domain)) {
$this->result->setMetadata($id, $keyMetadata, $domain);
$d = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
$this->result->add([$id => $message], $d);
if (null !== $keyMetadata = $this->source->getMetadata($id, $d)) {
$this->result->setMetadata($id, $keyMetadata, $d);
}
} else {
$this->messages[$domain]['obsolete'][$id] = $message;
@@ -62,9 +63,10 @@ class TargetOperation extends AbstractOperation
if (!$this->source->has($id, $domain)) {
$this->messages[$domain]['all'][$id] = $message;
$this->messages[$domain]['new'][$id] = $message;
$this->result->add(array($id => $message), $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
$this->result->setMetadata($id, $keyMetadata, $domain);
$d = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
$this->result->add([$id => $message], $d);
if (null !== $keyMetadata = $this->target->getMetadata($id, $d)) {
$this->result->setMetadata($id, $keyMetadata, $d);
}
}
}

View File

@@ -18,6 +18,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\Util\XliffUtils;
/**
@@ -52,16 +53,16 @@ class XliffLintCommand extends Command
protected function configure()
{
$this
->setDescription('Lints a XLIFF file and outputs encountered errors')
->addArgument('filename', InputArgument::IS_ARRAY, 'A file or a directory or STDIN')
->setDescription('Lint an XLIFF file and outputs encountered errors')
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
->setHelp(<<<EOF
The <info>%command.name%</info> command lints a XLIFF file and outputs to STDOUT
The <info>%command.name%</info> command lints an XLIFF file and outputs to STDOUT
the first encountered syntax error.
You can validates XLIFF contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
<info>cat filename | php %command.full_name% -</info>
You can also validate the syntax of a file:
@@ -84,15 +85,22 @@ EOF
$this->format = $input->getOption('format');
$this->displayCorrectFiles = $output->isVerbose();
if (0 === \count($filenames)) {
if (!$stdin = $this->getStdin()) {
if (['-'] === $filenames) {
return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
}
// @deprecated to be removed in 5.0
if (!$filenames) {
if (0 !== ftell(\STDIN)) {
throw new RuntimeException('Please provide a filename or pipe file content to STDIN.');
}
return $this->display($io, array($this->validate($stdin)));
@trigger_error('Piping content from STDIN to the "lint:xliff" command without passing the dash symbol "-" as argument is deprecated since Symfony 4.4.', \E_USER_DEPRECATED);
return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
}
$filesInfo = array();
$filesInfo = [];
foreach ($filenames as $filename) {
if (!$this->isReadable($filename)) {
throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
@@ -106,44 +114,49 @@ EOF
return $this->display($io, $filesInfo);
}
private function validate($content, $file = null)
private function validate(string $content, string $file = null): array
{
$errors = array();
$errors = [];
// Avoid: Warning DOMDocument::loadXML(): Empty string supplied as input
if ('' === trim($content)) {
return array('file' => $file, 'valid' => true);
return ['file' => $file, 'valid' => true];
}
libxml_use_internal_errors(true);
$internal = libxml_use_internal_errors(true);
$document = new \DOMDocument();
$document->loadXML($content);
if (null !== $targetLanguage = $this->getTargetLanguageFromFile($document)) {
$normalizedLocale = preg_quote(str_replace('-', '_', $targetLanguage), '/');
$normalizedLocalePattern = sprintf('(%s|%s)', preg_quote($targetLanguage, '/'), preg_quote(str_replace('-', '_', $targetLanguage), '/'));
// strict file names require translation files to be named '____.locale.xlf'
// otherwise, both '____.locale.xlf' and 'locale.____.xlf' are allowed
$expectedFilenamePattern = $this->requireStrictFileNames ? sprintf('/^.*\.%s\.xlf/', $normalizedLocale) : sprintf('/^(.*\.%s\.xlf|%s\..*\.xlf)/', $normalizedLocale, $normalizedLocale);
// also, the regexp matching must be case-insensitive, as defined for 'target-language' values
// http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#target-language
$expectedFilenamePattern = $this->requireStrictFileNames ? sprintf('/^.*\.(?i:%s)\.(?:xlf|xliff)/', $normalizedLocalePattern) : sprintf('/^(?:.*\.(?i:%s)|(?i:%s)\..*)\.(?:xlf|xliff)/', $normalizedLocalePattern, $normalizedLocalePattern);
if (0 === preg_match($expectedFilenamePattern, basename($file))) {
$errors[] = array(
$errors[] = [
'line' => -1,
'column' => -1,
'message' => sprintf('There is a mismatch between the language included in the file name ("%s") and the "%s" value used in the "target-language" attribute of the file.', basename($file), $targetLanguage),
);
];
}
}
foreach (XliffUtils::validateSchema($document) as $xmlError) {
$errors[] = array(
$errors[] = [
'line' => $xmlError['line'],
'column' => $xmlError['column'],
'message' => $xmlError['message'],
);
];
}
return array('file' => $file, 'valid' => 0 === \count($errors), 'messages' => $errors);
libxml_clear_errors();
libxml_use_internal_errors($internal);
return ['file' => $file, 'valid' => 0 === \count($errors), 'messages' => $errors];
}
private function display(SymfonyStyle $io, array $files)
@@ -196,12 +209,12 @@ EOF
}
});
$io->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$io->writeln(json_encode($filesInfo, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
return min($errors, 1);
}
private function getFiles($fileOrDirectory)
private function getFiles(string $fileOrDirectory)
{
if (is_file($fileOrDirectory)) {
yield new \SplFileInfo($fileOrDirectory);
@@ -210,7 +223,7 @@ EOF
}
foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
if (!\in_array($file->getExtension(), array('xlf', 'xliff'))) {
if (!\in_array($file->getExtension(), ['xlf', 'xliff'])) {
continue;
}
@@ -218,21 +231,7 @@ EOF
}
}
private function getStdin()
{
if (0 !== ftell(STDIN)) {
return;
}
$inputs = '';
while (!feof(STDIN)) {
$inputs .= fread(STDIN, 1024);
}
return $inputs;
}
private function getDirectoryIterator($directory)
private function getDirectoryIterator(string $directory)
{
$default = function ($directory) {
return new \RecursiveIteratorIterator(
@@ -242,20 +241,20 @@ EOF
};
if (null !== $this->directoryIteratorProvider) {
return \call_user_func($this->directoryIteratorProvider, $directory, $default);
return ($this->directoryIteratorProvider)($directory, $default);
}
return $default($directory);
}
private function isReadable($fileOrDirectory)
private function isReadable(string $fileOrDirectory)
{
$default = function ($fileOrDirectory) {
return is_readable($fileOrDirectory);
};
if (null !== $this->isReadableProvider) {
return \call_user_func($this->isReadableProvider, $fileOrDirectory, $default);
return ($this->isReadableProvider)($fileOrDirectory, $default);
}
return $default($fileOrDirectory);
@@ -263,7 +262,7 @@ EOF
private function getTargetLanguageFromFile(\DOMDocument $xliffContents): ?string
{
foreach ($xliffContents->getElementsByTagName('file')[0]->attributes ?? array() as $attribute) {
foreach ($xliffContents->getElementsByTagName('file')[0]->attributes ?? [] as $attribute) {
if ('target-language' === $attribute->nodeName) {
return $attribute->nodeValue;
}

42
vendor/symfony/translation/DataCollector/TranslationDataCollector.php vendored Executable file → Normal file
View File

@@ -16,9 +16,12 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\VarDumper\Cloner\Data;
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*
* @final since Symfony 4.4
*/
class TranslationDataCollector extends DataCollector implements LateDataCollectorInterface
{
@@ -36,20 +39,21 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
{
$messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
$this->data = $this->computeCount($messages);
$this->data += $this->computeCount($messages);
$this->data['messages'] = $messages;
$this->data['locale'] = $this->translator->getLocale();
$this->data['fallback_locales'] = $this->translator->getFallbackLocales();
$this->data = $this->cloneVar($this->data);
}
/**
* {@inheritdoc}
*
* @param \Throwable|null $exception
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
public function collect(Request $request, Response $response/* , \Throwable $exception = null */)
{
$this->data['locale'] = $this->translator->getLocale();
$this->data['fallback_locales'] = $this->translator->getFallbackLocales();
}
/**
@@ -57,15 +61,15 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
*/
public function reset()
{
$this->data = array();
$this->data = [];
}
/**
* @return array
* @return array|Data
*/
public function getMessages()
{
return isset($this->data['messages']) ? $this->data['messages'] : array();
return $this->data['messages'] ?? [];
}
/**
@@ -73,7 +77,7 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
*/
public function getCountMissings()
{
return isset($this->data[DataCollectorTranslator::MESSAGE_MISSING]) ? $this->data[DataCollectorTranslator::MESSAGE_MISSING] : 0;
return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
}
/**
@@ -81,7 +85,7 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
*/
public function getCountFallbacks()
{
return isset($this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK]) ? $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] : 0;
return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
}
/**
@@ -89,7 +93,7 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
*/
public function getCountDefines()
{
return isset($this->data[DataCollectorTranslator::MESSAGE_DEFINED]) ? $this->data[DataCollectorTranslator::MESSAGE_DEFINED] : 0;
return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
}
public function getLocale()
@@ -102,7 +106,7 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
*/
public function getFallbackLocales()
{
return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : array();
return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
}
/**
@@ -113,15 +117,15 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
return 'translation';
}
private function sanitizeCollectedMessages($messages)
private function sanitizeCollectedMessages(array $messages)
{
$result = array();
$result = [];
foreach ($messages as $key => $message) {
$messageId = $message['locale'].$message['domain'].$message['id'];
if (!isset($result[$messageId])) {
$message['count'] = 1;
$message['parameters'] = !empty($message['parameters']) ? array($message['parameters']) : array();
$message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : [];
$messages[$key]['translation'] = $this->sanitizeString($message['translation']);
$result[$messageId] = $message;
} else {
@@ -138,13 +142,13 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
return $result;
}
private function computeCount($messages)
private function computeCount(array $messages)
{
$count = array(
$count = [
DataCollectorTranslator::MESSAGE_DEFINED => 0,
DataCollectorTranslator::MESSAGE_MISSING => 0,
DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
);
];
foreach ($messages as $message) {
++$count[$message['state']];
@@ -153,7 +157,7 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
return $count;
}
private function sanitizeString($string, $length = 80)
private function sanitizeString(string $string, int $length = 80)
{
$string = trim(preg_replace('/\s+/', ' ', $string));

View File

@@ -11,25 +11,27 @@
namespace Symfony\Component\Translation;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface
class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface, WarmableInterface
{
const MESSAGE_DEFINED = 0;
const MESSAGE_MISSING = 1;
const MESSAGE_EQUALS_FALLBACK = 2;
public const MESSAGE_DEFINED = 0;
public const MESSAGE_MISSING = 1;
public const MESSAGE_EQUALS_FALLBACK = 2;
/**
* @var TranslatorInterface|TranslatorBagInterface
*/
private $translator;
private $messages = array();
private $messages = [];
/**
* @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
@@ -37,10 +39,10 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
public function __construct($translator)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be an instance of "%s", "%s" given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
if (!$translator instanceof TranslatorBagInterface) {
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', \get_class($translator)));
if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator)));
}
$this->translator = $translator;
@@ -49,7 +51,7 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
/**
* {@inheritdoc}
*/
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
$trans = $this->translator->trans($id, $parameters, $domain, $locale);
$this->collectMessage($locale, $domain, $id, $trans, $parameters);
@@ -62,15 +64,15 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
*
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
if ($this->translator instanceof TranslatorInterface) {
$trans = $this->translator->trans($id, array('%count%' => $number) + $parameters, $domain, $locale);
$trans = $this->translator->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
} else {
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
$this->collectMessage($locale, $domain, $id, $trans, array('%count%' => $number) + $parameters);
$this->collectMessage($locale, $domain, $id, $trans, ['%count%' => $number] + $parameters);
return $trans;
}
@@ -99,6 +101,16 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
return $this->translator->getCatalogue($locale);
}
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
if ($this->translator instanceof WarmableInterface) {
$this->translator->warmUp($cacheDir);
}
}
/**
* Gets the fallback locales.
*
@@ -110,7 +122,7 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
return $this->translator->getFallbackLocales();
}
return array();
return [];
}
/**
@@ -129,14 +141,7 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
return $this->messages;
}
/**
* @param string|null $locale
* @param string|null $domain
* @param string $id
* @param string $translation
* @param array|null $parameters
*/
private function collectMessage($locale, $domain, $id, $translation, $parameters = array())
private function collectMessage(?string $locale, ?string $domain, ?string $id, string $translation, ?array $parameters = [])
{
if (null === $domain) {
$domain = 'messages';
@@ -145,6 +150,7 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
$id = (string) $id;
$catalogue = $this->translator->getCatalogue($locale);
$locale = $catalogue->getLocale();
$fallbackLocale = null;
if ($catalogue->defines($id, $domain)) {
$state = self::MESSAGE_DEFINED;
} elseif ($catalogue->has($id, $domain)) {
@@ -153,24 +159,24 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
while ($fallbackCatalogue) {
if ($fallbackCatalogue->defines($id, $domain)) {
$locale = $fallbackCatalogue->getLocale();
$fallbackLocale = $fallbackCatalogue->getLocale();
break;
}
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
}
} else {
$state = self::MESSAGE_MISSING;
}
$this->messages[] = array(
$this->messages[] = [
'locale' => $locale,
'fallbackLocale' => $fallbackLocale,
'domain' => $domain,
'id' => $id,
'translation' => $translation,
'parameters' => $parameters,
'state' => $state,
'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
);
];
}
}

View File

@@ -38,7 +38,7 @@ class TranslationDumperPass implements CompilerPassInterface
$definition = $container->getDefinition($this->writerServiceId);
foreach ($container->findTaggedServiceIds($this->dumperTag, true) as $id => $attributes) {
$definition->addMethodCall('addDumper', array($attributes[0]['alias'], new Reference($id)));
$definition->addMethodCall('addDumper', [$attributes[0]['alias'], new Reference($id)]);
}
}
}

View File

@@ -43,7 +43,7 @@ class TranslationExtractorPass implements CompilerPassInterface
throw new RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id));
}
$definition->addMethodCall('addExtractor', array($attributes[0]['alias'], new Reference($id)));
$definition->addMethodCall('addExtractor', [$attributes[0]['alias'], new Reference($id)]);
}
}
}

View File

@@ -39,8 +39,8 @@ class TranslatorPass implements CompilerPassInterface
return;
}
$loaders = array();
$loaderRefs = array();
$loaders = [];
$loaderRefs = [];
foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) {
$loaderRefs[$id] = new Reference($id);
$loaders[$id][] = $attributes[0]['alias'];
@@ -53,7 +53,7 @@ class TranslatorPass implements CompilerPassInterface
$definition = $container->getDefinition($this->readerServiceId);
foreach ($loaders as $id => $formats) {
foreach ($formats as $format) {
$definition->addMethodCall('addLoader', array($format, $loaderRefs[$id]));
$definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
}
}
}
@@ -68,12 +68,22 @@ class TranslatorPass implements CompilerPassInterface
return;
}
$paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(2));
if ($container->hasDefinition($this->debugCommandServiceId)) {
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
}
$definition = $container->getDefinition($this->debugCommandServiceId);
$definition->replaceArgument(4, $container->getParameter('twig.default_path'));
if (\count($definition->getArguments()) > 6) {
$definition->replaceArgument(6, $paths);
}
}
if ($container->hasDefinition($this->updateCommandServiceId)) {
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
$definition = $container->getDefinition($this->updateCommandServiceId);
$definition->replaceArgument(5, $container->getParameter('twig.default_path'));
if (\count($definition->getArguments()) > 7) {
$definition->replaceArgument(7, $paths);
}
}
}
}

View File

@@ -0,0 +1,147 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class TranslatorPathsPass extends AbstractRecursivePass
{
private $translatorServiceId;
private $debugCommandServiceId;
private $updateCommandServiceId;
private $resolverServiceId;
private $level = 0;
private $paths = [];
private $definitions = [];
private $controllers = [];
public function __construct(string $translatorServiceId = 'translator', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update', string $resolverServiceId = 'argument_resolver.service')
{
$this->translatorServiceId = $translatorServiceId;
$this->debugCommandServiceId = $debugCommandServiceId;
$this->updateCommandServiceId = $updateCommandServiceId;
$this->resolverServiceId = $resolverServiceId;
}
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition($this->translatorServiceId)) {
return;
}
foreach ($this->findControllerArguments($container) as $controller => $argument) {
$id = substr($controller, 0, strpos($controller, ':') ?: \strlen($controller));
if ($container->hasDefinition($id)) {
[$locatorRef] = $argument->getValues();
$this->controllers[(string) $locatorRef][$container->getDefinition($id)->getClass()] = true;
}
}
try {
parent::process($container);
$paths = [];
foreach ($this->paths as $class => $_) {
if (($r = $container->getReflectionClass($class)) && !$r->isInterface()) {
$paths[] = $r->getFileName();
foreach ($r->getTraits() as $trait) {
$paths[] = $trait->getFileName();
}
}
}
if ($paths) {
if ($container->hasDefinition($this->debugCommandServiceId)) {
$definition = $container->getDefinition($this->debugCommandServiceId);
$definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths));
}
if ($container->hasDefinition($this->updateCommandServiceId)) {
$definition = $container->getDefinition($this->updateCommandServiceId);
$definition->replaceArgument(7, array_merge($definition->getArgument(7), $paths));
}
}
} finally {
$this->level = 0;
$this->paths = [];
$this->definitions = [];
}
}
protected function processValue($value, $isRoot = false)
{
if ($value instanceof Reference) {
if ((string) $value === $this->translatorServiceId) {
for ($i = $this->level - 1; $i >= 0; --$i) {
$class = $this->definitions[$i]->getClass();
if (ServiceLocator::class === $class) {
if (!isset($this->controllers[$this->currentId])) {
continue;
}
foreach ($this->controllers[$this->currentId] as $class => $_) {
$this->paths[$class] = true;
}
} else {
$this->paths[$class] = true;
}
break;
}
}
return $value;
}
if ($value instanceof Definition) {
$this->definitions[$this->level++] = $value;
$value = parent::processValue($value, $isRoot);
unset($this->definitions[--$this->level]);
return $value;
}
return parent::processValue($value, $isRoot);
}
private function findControllerArguments(ContainerBuilder $container): array
{
if ($container->hasDefinition($this->resolverServiceId)) {
$argument = $container->getDefinition($this->resolverServiceId)->getArgument(0);
if ($argument instanceof Reference) {
$argument = $container->getDefinition($argument);
}
return $argument->getArgument(0);
}
if ($container->hasDefinition('debug.'.$this->resolverServiceId)) {
$argument = $container->getDefinition('debug.'.$this->resolverServiceId)->getArgument(0);
if ($argument instanceof Reference) {
$argument = $container->getDefinition($argument);
}
$argument = $argument->getArgument(0);
if ($argument instanceof Reference) {
$argument = $container->getDefinition($argument);
}
return $argument->getArgument(0);
}
return [];
}
}

View File

@@ -26,12 +26,12 @@ class CsvFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$handle = fopen('php://memory', 'r+b');
$handle = fopen('php://memory', 'r+');
foreach ($messages->all($domain) as $source => $target) {
fputcsv($handle, array($source, $target), $this->delimiter, $this->enclosure);
fputcsv($handle, [$source, $target], $this->delimiter, $this->enclosure);
}
rewind($handle);

View File

@@ -24,8 +24,7 @@ interface DumperInterface
/**
* Dumps the message catalogue.
*
* @param MessageCatalogue $messages The message catalogue
* @param array $options Options that are used by the dumper
* @param array $options Options that are used by the dumper
*/
public function dump(MessageCatalogue $messages, $options = array());
public function dump(MessageCatalogue $messages, $options = []);
}

View File

@@ -51,7 +51,7 @@ abstract class FileDumper implements DumperInterface
*/
public function setBackup($backup)
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1.', __METHOD__), \E_USER_DEPRECATED);
if (false !== $backup) {
throw new \LogicException('The backup feature is no longer supported.');
@@ -61,9 +61,9 @@ abstract class FileDumper implements DumperInterface
/**
* {@inheritdoc}
*/
public function dump(MessageCatalogue $messages, $options = array())
public function dump(MessageCatalogue $messages, $options = [])
{
if (!array_key_exists('path', $options)) {
if (!\array_key_exists('path', $options)) {
throw new InvalidArgumentException('The file dumper needs a path option.');
}
@@ -84,7 +84,7 @@ abstract class FileDumper implements DumperInterface
$intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale());
file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options));
$messages->replace(array(), $intlDomain);
$messages->replace([], $intlDomain);
try {
if ($messages->all($domain)) {
@@ -103,13 +103,11 @@ abstract class FileDumper implements DumperInterface
/**
* Transforms a domain of a message catalogue to its string representation.
*
* @param MessageCatalogue $messages
* @param string $domain
* @param array $options
* @param string $domain
*
* @return string representation
*/
abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array());
abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []);
/**
* Gets the file extension of the dumper.
@@ -123,10 +121,10 @@ abstract class FileDumper implements DumperInterface
*/
private function getRelativePath(string $domain, string $locale): string
{
return strtr($this->relativePathTemplate, array(
return strtr($this->relativePathTemplate, [
'%domain%' => $domain,
'%locale%' => $locale,
'%extension%' => $this->getExtension(),
));
]);
}
}

View File

@@ -28,7 +28,7 @@ class IcuResFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$data = $indexes = $resources = '';
@@ -47,7 +47,7 @@ class IcuResFileDumper extends FileDumper
$data .= pack('V', \strlen($target))
.mb_convert_encoding($target."\0", 'UTF-16LE', 'UTF-8')
.$this->writePadding($data)
;
;
}
$resOffset = $this->getPosition($data);
@@ -56,7 +56,7 @@ class IcuResFileDumper extends FileDumper
.$indexes
.$this->writePadding($data)
.$resources
;
;
$bundleTop = $this->getPosition($data);
@@ -82,16 +82,14 @@ class IcuResFileDumper extends FileDumper
return $header.$root.$data;
}
private function writePadding($data)
private function writePadding(string $data): ?string
{
$padding = \strlen($data) % 4;
if ($padding) {
return str_repeat("\xAA", 4 - $padding);
}
return $padding ? str_repeat("\xAA", 4 - $padding) : null;
}
private function getPosition($data)
private function getPosition(string $data)
{
return (\strlen($data) + 28) / 4;
}

View File

@@ -23,7 +23,7 @@ class IniFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$output = '';

View File

@@ -23,9 +23,9 @@ class JsonFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$flags = $options['json_encoding'] ?? JSON_PRETTY_PRINT;
$flags = $options['json_encoding'] ?? \JSON_PRETTY_PRINT;
return json_encode($messages->all($domain), $flags);
}

View File

@@ -24,20 +24,20 @@ class MoFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$sources = $targets = $sourceOffsets = $targetOffsets = '';
$offsets = array();
$offsets = [];
$size = 0;
foreach ($messages->all($domain) as $source => $target) {
$offsets[] = array_map('strlen', array($sources, $source, $targets, $target));
$offsets[] = array_map('strlen', [$sources, $source, $targets, $target]);
$sources .= "\0".$source;
$targets .= "\0".$target;
++$size;
}
$header = array(
$header = [
'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC,
'formatRevision' => 0,
'count' => $size,
@@ -45,7 +45,7 @@ class MoFileDumper extends FileDumper
'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size),
'sizeHashes' => 0,
'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size),
);
];
$sourcesSize = \strlen($sources);
$sourcesStart = $header['offsetHashes'] + 1;
@@ -57,12 +57,12 @@ class MoFileDumper extends FileDumper
.$this->writeLong($offset[2] + $sourcesStart + $sourcesSize);
}
$output = implode('', array_map(array($this, 'writeLong'), $header))
$output = implode('', array_map([$this, 'writeLong'], $header))
.$sourceOffsets
.$targetOffsets
.$sources
.$targets
;
;
return $output;
}
@@ -75,7 +75,7 @@ class MoFileDumper extends FileDumper
return 'mo';
}
private function writeLong($str)
private function writeLong($str): string
{
return pack('V*', $str);
}

View File

@@ -23,7 +23,7 @@ class PhpFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
return "<?php\n\nreturn ".var_export($messages->all($domain), true).";\n";
}

View File

@@ -23,7 +23,7 @@ class PoFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$output = 'msgid ""'."\n";
$output .= 'msgstr ""'."\n";
@@ -39,13 +39,78 @@ class PoFileDumper extends FileDumper
} else {
$newLine = true;
}
$output .= sprintf('msgid "%s"'."\n", $this->escape($source));
$output .= sprintf('msgstr "%s"', $this->escape($target));
$metadata = $messages->getMetadata($source, $domain);
if (isset($metadata['comments'])) {
$output .= $this->formatComments($metadata['comments']);
}
if (isset($metadata['flags'])) {
$output .= $this->formatComments(implode(',', (array) $metadata['flags']), ',');
}
if (isset($metadata['sources'])) {
$output .= $this->formatComments(implode(' ', (array) $metadata['sources']), ':');
}
$sourceRules = $this->getStandardRules($source);
$targetRules = $this->getStandardRules($target);
if (2 == \count($sourceRules) && [] !== $targetRules) {
$output .= sprintf('msgid "%s"'."\n", $this->escape($sourceRules[0]));
$output .= sprintf('msgid_plural "%s"'."\n", $this->escape($sourceRules[1]));
foreach ($targetRules as $i => $targetRule) {
$output .= sprintf('msgstr[%d] "%s"'."\n", $i, $this->escape($targetRule));
}
} else {
$output .= sprintf('msgid "%s"'."\n", $this->escape($source));
$output .= sprintf('msgstr "%s"'."\n", $this->escape($target));
}
}
return $output;
}
private function getStandardRules(string $id)
{
// Partly copied from TranslatorTrait::trans.
$parts = [];
if (preg_match('/^\|++$/', $id)) {
$parts = explode('|', $id);
} elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) {
$parts = $matches[0];
}
$intervalRegexp = <<<'EOF'
/^(?P<interval>
({\s*
(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
\s*})
|
(?P<left_delimiter>[\[\]])
\s*
(?P<left>-Inf|\-?\d+(\.\d+)?)
\s*,\s*
(?P<right>\+?Inf|\-?\d+(\.\d+)?)
\s*
(?P<right_delimiter>[\[\]])
)\s*(?P<message>.*?)$/xs
EOF;
$standardRules = [];
foreach ($parts as $part) {
$part = trim(str_replace('||', '|', $part));
if (preg_match($intervalRegexp, $part)) {
// Explicit rule is not a standard rule.
return [];
} else {
$standardRules[] = $part;
}
}
return $standardRules;
}
/**
* {@inheritdoc}
*/
@@ -54,8 +119,19 @@ class PoFileDumper extends FileDumper
return 'po';
}
private function escape($str)
private function escape(string $str): string
{
return addcslashes($str, "\0..\37\42\134");
}
private function formatComments($comments, string $prefix = ''): ?string
{
$output = null;
foreach ((array) $comments as $comment) {
$output .= sprintf('#%s %s'."\n", $prefix, $comment);
}
return $output;
}
}

View File

@@ -23,7 +23,7 @@ class QtFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
@@ -33,6 +33,17 @@ class QtFileDumper extends FileDumper
foreach ($messages->all($domain) as $source => $target) {
$message = $context->appendChild($dom->createElement('message'));
$metadata = $messages->getMetadata($source, $domain);
if (isset($metadata['sources'])) {
foreach ((array) $metadata['sources'] as $location) {
$loc = explode(':', $location, 2);
$location = $message->appendChild($dom->createElement('location'));
$location->setAttribute('filename', $loc[0]);
if (isset($loc[1])) {
$location->setAttribute('line', $loc[1]);
}
}
}
$message->appendChild($dom->createElement('source', $source));
$message->appendChild($dom->createElement('translation', $target));
}

View File

@@ -24,14 +24,14 @@ class XliffFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
$xliffVersion = '1.2';
if (array_key_exists('xliff_version', $options)) {
if (\array_key_exists('xliff_version', $options)) {
$xliffVersion = $options['xliff_version'];
}
if (array_key_exists('default_locale', $options)) {
if (\array_key_exists('default_locale', $options)) {
$defaultLocale = $options['default_locale'];
} else {
$defaultLocale = \Locale::getDefault();
@@ -41,7 +41,7 @@ class XliffFileDumper extends FileDumper
return $this->dumpXliff1($defaultLocale, $messages, $domain, $options);
}
if ('2.0' === $xliffVersion) {
return $this->dumpXliff2($defaultLocale, $messages, $domain, $options);
return $this->dumpXliff2($defaultLocale, $messages, $domain);
}
throw new InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
@@ -55,10 +55,10 @@ class XliffFileDumper extends FileDumper
return 'xlf';
}
private function dumpXliff1($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
private function dumpXliff1(string $defaultLocale, MessageCatalogue $messages, ?string $domain, array $options = [])
{
$toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony');
if (array_key_exists('tool_info', $options)) {
$toolInfo = ['tool-id' => 'symfony', 'tool-name' => 'Symfony'];
if (\array_key_exists('tool_info', $options)) {
$toolInfo = array_merge($toolInfo, $options['tool_info']);
}
@@ -129,7 +129,7 @@ class XliffFileDumper extends FileDumper
return $dom->saveXML();
}
private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
private function dumpXliff2(string $defaultLocale, MessageCatalogue $messages, ?string $domain)
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
@@ -141,16 +141,20 @@ class XliffFileDumper extends FileDumper
$xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
$xliffFile = $xliff->appendChild($dom->createElement('file'));
$xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
if (str_ends_with($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
$xliffFile->setAttribute('id', substr($domain, 0, -\strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX)).'.'.$messages->getLocale());
} else {
$xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
}
foreach ($messages->all($domain) as $source => $target) {
$translation = $dom->createElement('unit');
$translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
$name = $source;
if (\strlen($source) > 80) {
$name = substr(md5($source), -7);
if (\strlen($source) <= 80) {
$translation->setAttribute('name', $source);
}
$translation->setAttribute('name', $name);
$metadata = $messages->getMetadata($source, $domain);
// Add notes section
@@ -158,7 +162,7 @@ class XliffFileDumper extends FileDumper
$notesElement = $dom->createElement('notes');
foreach ($metadata['notes'] as $note) {
$n = $dom->createElement('note');
$n->appendChild($dom->createTextNode(isset($note['content']) ? $note['content'] : ''));
$n->appendChild($dom->createTextNode($note['content'] ?? ''));
unset($note['content']);
foreach ($note as $name => $value) {
@@ -192,14 +196,8 @@ class XliffFileDumper extends FileDumper
return $dom->saveXML();
}
/**
* @param string $key
* @param array|null $metadata
*
* @return bool
*/
private function hasMetadataArrayInfo($key, $metadata = null)
private function hasMetadataArrayInfo(string $key, array $metadata = null): bool
{
return null !== $metadata && array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || \is_array($metadata[$key]));
return is_iterable($metadata[$key] ?? null);
}
}

View File

@@ -33,9 +33,9 @@ class YamlFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
{
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
if (!class_exists(Yaml::class)) {
throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
}

View File

@@ -21,21 +21,21 @@ use Symfony\Component\Translation\Exception\InvalidArgumentException;
abstract class AbstractFileExtractor
{
/**
* @param string|array $resource Files, a file or a directory
* @param string|iterable $resource Files, a file or a directory
*
* @return array
* @return iterable
*/
protected function extractFiles($resource)
{
if (\is_array($resource) || $resource instanceof \Traversable) {
$files = array();
if (is_iterable($resource)) {
$files = [];
foreach ($resource as $file) {
if ($this->canBeExtracted($file)) {
$files[] = $this->toSplFileInfo($file);
}
}
} elseif (is_file($resource)) {
$files = $this->canBeExtracted($resource) ? array($this->toSplFileInfo($resource)) : array();
$files = $this->canBeExtracted($resource) ? [$this->toSplFileInfo($resource)] : [];
} else {
$files = $this->extractFromDirectory($resource);
}
@@ -74,7 +74,7 @@ abstract class AbstractFileExtractor
/**
* @param string|array $resource Files, a file or a directory
*
* @return array files to be extracted
* @return iterable files to be extracted
*/
abstract protected function extractFromDirectory($resource);
}

View File

@@ -25,13 +25,12 @@ class ChainExtractor implements ExtractorInterface
*
* @var ExtractorInterface[]
*/
private $extractors = array();
private $extractors = [];
/**
* Adds a loader to the translation extractor.
*
* @param string $format The format of the loader
* @param ExtractorInterface $extractor The loader
* @param string $format The format of the loader
*/
public function addExtractor($format, ExtractorInterface $extractor)
{

View File

@@ -24,8 +24,7 @@ interface ExtractorInterface
/**
* Extracts translation messages from files, a file or a directory to the catalogue.
*
* @param string|array $resource Files, a file or a directory
* @param MessageCatalogue $catalogue The catalogue
* @param string|iterable<string> $resource Files, a file or a directory
*/
public function extract($resource, MessageCatalogue $catalogue);

View File

@@ -21,9 +21,9 @@ use Symfony\Component\Translation\MessageCatalogue;
*/
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
{
const MESSAGE_TOKEN = 300;
const METHOD_ARGUMENTS_TOKEN = 1000;
const DOMAIN_TOKEN = 1001;
public const MESSAGE_TOKEN = 300;
public const METHOD_ARGUMENTS_TOKEN = 1000;
public const DOMAIN_TOKEN = 1001;
/**
* Prefix for new found message.
@@ -37,8 +37,8 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
*
* @var array
*/
protected $sequences = array(
array(
protected $sequences = [
[
'->',
'trans',
'(',
@@ -47,8 +47,8 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
self::METHOD_ARGUMENTS_TOKEN,
',',
self::DOMAIN_TOKEN,
),
array(
],
[
'->',
'transChoice',
'(',
@@ -59,20 +59,20 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
self::METHOD_ARGUMENTS_TOKEN,
',',
self::DOMAIN_TOKEN,
),
array(
],
[
'->',
'trans',
'(',
self::MESSAGE_TOKEN,
),
array(
],
[
'->',
'transChoice',
'(',
self::MESSAGE_TOKEN,
),
);
],
];
/**
* {@inheritdoc}
@@ -81,9 +81,8 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
{
$files = $this->extractFiles($resource);
foreach ($files as $file) {
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog);
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
gc_mem_caches();
}
}
@@ -101,7 +100,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
*
* @param mixed $token
*
* @return string
* @return string|null
*/
protected function normalizeToken($token)
{
@@ -119,7 +118,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
{
for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();
if (T_WHITESPACE !== $t[0]) {
if (\T_WHITESPACE !== $t[0]) {
break;
}
}
@@ -154,43 +153,71 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
{
$message = '';
$docToken = '';
$docPart = '';
for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();
if ('.' === $t) {
// Concatenate with next token
continue;
}
if (!isset($t[1])) {
break;
}
switch ($t[0]) {
case T_START_HEREDOC:
case \T_START_HEREDOC:
$docToken = $t[1];
break;
case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
$message .= $t[1];
case \T_ENCAPSED_AND_WHITESPACE:
case \T_CONSTANT_ENCAPSED_STRING:
if ('' === $docToken) {
$message .= PhpStringTokenParser::parse($t[1]);
} else {
$docPart = $t[1];
}
break;
case \T_END_HEREDOC:
if ($indentation = strspn($t[1], ' ')) {
$docPartWithLineBreaks = $docPart;
$docPart = '';
foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, \PREG_SPLIT_DELIM_CAPTURE) as $str) {
if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
$docPart .= $str;
} else {
$docPart .= substr($str, $indentation);
}
}
}
$message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
$docToken = '';
$docPart = '';
break;
case \T_WHITESPACE:
break;
case T_END_HEREDOC:
return PhpStringTokenParser::parseDocString($docToken, $message);
default:
break 2;
}
}
if ($message) {
$message = PhpStringTokenParser::parse($message);
}
return $message;
}
/**
* Extracts trans message from PHP tokens.
*
* @param array $tokens
* @param MessageCatalogue $catalog
* @param array $tokens
* @param string $filename
*/
protected function parseTokens($tokens, MessageCatalogue $catalog)
protected function parseTokens($tokens, MessageCatalogue $catalog/* , string $filename */)
{
if (\func_num_args() < 3 && __CLASS__ !== static::class && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) {
@trigger_error(sprintf('The "%s()" method will have a new "string $filename" argument in version 5.0, not defining it is deprecated since Symfony 4.3.', __METHOD__), \E_USER_DEPRECATED);
}
$filename = 2 < \func_num_args() ? func_get_arg(2) : '';
$tokenIterator = new \ArrayIterator($tokens);
for ($key = 0; $key < $tokenIterator->count(); ++$key) {
@@ -214,7 +241,10 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
} elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
$this->skipMethodArgument($tokenIterator);
} elseif (self::DOMAIN_TOKEN === $item) {
$domain = $this->getValue($tokenIterator);
$domainToken = $this->getValue($tokenIterator);
if ('' !== $domainToken) {
$domain = $domainToken;
}
break;
} else {
@@ -224,6 +254,10 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
if ($message) {
$catalog->set($message, $this->prefix.$message, $domain);
$metadata = $catalog->getMetadata($message, $domain) ?? [];
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
$metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
$catalog->setMetadata($message, $metadata, $domain);
break;
}
}
@@ -239,13 +273,11 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
*/
protected function canBeExtracted($file)
{
return $this->isFile($file) && 'php' === pathinfo($file, PATHINFO_EXTENSION);
return $this->isFile($file) && 'php' === pathinfo($file, \PATHINFO_EXTENSION);
}
/**
* @param string|array $directory
*
* @return array
* {@inheritdoc}
*/
protected function extractFromDirectory($directory)
{

View File

@@ -49,7 +49,7 @@ namespace Symfony\Component\Translation\Extractor;
class PhpStringTokenParser
{
protected static $replacements = array(
protected static $replacements = [
'\\' => '\\',
'$' => '$',
'n' => "\n",
@@ -58,7 +58,7 @@ class PhpStringTokenParser
'f' => "\f",
'v' => "\v",
'e' => "\x1B",
);
];
/**
* Parses a string token.
@@ -76,8 +76,8 @@ class PhpStringTokenParser
if ('\'' === $str[$bLength]) {
return str_replace(
array('\\\\', '\\\''),
array('\\', '\''),
['\\\\', '\\\''],
['\\', '\''],
substr($str, $bLength + 1, -1)
);
} else {
@@ -101,12 +101,12 @@ class PhpStringTokenParser
return preg_replace_callback(
'~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~',
array(__CLASS__, 'parseCallback'),
[__CLASS__, 'parseCallback'],
$str
);
}
private static function parseCallback($matches)
private static function parseCallback(array $matches): string
{
$str = $matches[1];
@@ -133,7 +133,7 @@ class PhpStringTokenParser
$str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
// nowdoc string
if (false !== strpos($startToken, '\'')) {
if (str_contains($startToken, '\'')) {
return $str;
}

View File

@@ -28,5 +28,5 @@ interface ChoiceMessageFormatterInterface
*
* @return string
*/
public function choiceFormat($message, $number, $locale, array $parameters = array());
public function choiceFormat($message, $number, $locale, array $parameters = []);
}

View File

@@ -21,13 +21,18 @@ use Symfony\Component\Translation\Exception\LogicException;
class IntlFormatter implements IntlFormatterInterface
{
private $hasMessageFormatter;
private $cache = array();
private $cache = [];
/**
* {@inheritdoc}
*/
public function formatIntl(string $message, string $locale, array $parameters = array()): string
public function formatIntl(string $message, string $locale, array $parameters = []): string
{
// MessageFormatter constructor throws an exception if the message is empty
if ('' === $message) {
return '';
}
if (!$formatter = $this->cache[$locale][$message] ?? null) {
if (!($this->hasMessageFormatter ?? $this->hasMessageFormatter = class_exists(\MessageFormatter::class))) {
throw new LogicException('Cannot parse message translation: please install the "intl" PHP extension or the "symfony/polyfill-intl-messageformatter" package.');
@@ -35,19 +40,19 @@ class IntlFormatter implements IntlFormatterInterface
try {
$this->cache[$locale][$message] = $formatter = new \MessageFormatter($locale, $message);
} catch (\IntlException $e) {
throw new InvalidArgumentException(sprintf('Invalid message format (error #%d): %s.', intl_get_error_code(), intl_get_error_message()), 0, $e);
throw new InvalidArgumentException(sprintf('Invalid message format (error #%d): ', intl_get_error_code()).intl_get_error_message(), 0, $e);
}
}
foreach ($parameters as $key => $value) {
if (\in_array($key[0] ?? null, array('%', '{'), true)) {
if (\in_array($key[0] ?? null, ['%', '{'], true)) {
unset($parameters[$key]);
$parameters[trim($key, '%{ }')] = $value;
}
}
if (false === $message = $formatter->format($parameters)) {
throw new InvalidArgumentException(sprintf('Unable to format message (error #%s): %s.', $formatter->getErrorCode(), $formatter->getErrorMessage()));
throw new InvalidArgumentException(sprintf('Unable to format message (error #%s): ', $formatter->getErrorCode()).$formatter->getErrorMessage());
}
return $message;

View File

@@ -23,5 +23,5 @@ interface IntlFormatterInterface
*
* @see http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
*/
public function formatIntl(string $message, string $locale, array $parameters = array()): string;
public function formatIntl(string $message, string $locale, array $parameters = []): string;
}

View File

@@ -16,6 +16,9 @@ use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
// Help opcache.preload discover always-needed symbols
class_exists(IntlFormatter::class);
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
@@ -32,7 +35,7 @@ class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterf
if ($translator instanceof MessageSelector) {
$translator = new IdentityTranslator($translator);
} elseif (null !== $translator && !$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) {
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be an instance of "%s", "%s" given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
$this->translator = $translator ?? new IdentityTranslator();
@@ -42,7 +45,7 @@ class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterf
/**
* {@inheritdoc}
*/
public function format($message, $locale, array $parameters = array())
public function format($message, $locale, array $parameters = [])
{
if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($message, $parameters, null, $locale);
@@ -54,7 +57,7 @@ class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterf
/**
* {@inheritdoc}
*/
public function formatIntl(string $message, string $locale, array $parameters = array()): string
public function formatIntl(string $message, string $locale, array $parameters = []): string
{
return $this->intlFormatter->formatIntl($message, $locale, $parameters);
}
@@ -64,16 +67,16 @@ class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterf
*
* @deprecated since Symfony 4.2, use format() with a %count% parameter instead
*/
public function choiceFormat($message, $number, $locale, array $parameters = array())
public function choiceFormat($message, $number, $locale, array $parameters = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the format() one instead with a %count% parameter.', __METHOD__), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the format() one instead with a %%count%% parameter.', __METHOD__), \E_USER_DEPRECATED);
$parameters = array('%count%' => $number) + $parameters;
$parameters = ['%count%' => $number] + $parameters;
if ($this->translator instanceof TranslatorInterface) {
return $this->format($message, $locale, $parameters);
}
return $this->format($this->translator->transChoice($message, $number, array(), null, $locale), $locale, $parameters);
return $this->format($this->translator->transChoice($message, $number, [], null, $locale), $locale, $parameters);
}
}

View File

@@ -26,5 +26,5 @@ interface MessageFormatterInterface
*
* @return string
*/
public function format($message, $locale, array $parameters = array());
public function format($message, $locale, array $parameters = []);
}

View File

@@ -22,39 +22,55 @@ use Symfony\Contracts\Translation\TranslatorTrait;
*/
class IdentityTranslator implements LegacyTranslatorInterface, TranslatorInterface
{
use TranslatorTrait;
use TranslatorTrait {
trans as private doTrans;
setLocale as private doSetLocale;
}
private $selector;
/**
* @param MessageSelector|null $selector The message selector for pluralization
*/
public function __construct(MessageSelector $selector = null)
{
$this->selector = $selector;
if (__CLASS__ !== \get_class($this)) {
@trigger_error(sprintf('Calling "%s()" is deprecated since Symfony 4.2.'), E_USER_DEPRECATED);
if (__CLASS__ !== static::class) {
@trigger_error(sprintf('Calling "%s()" is deprecated since Symfony 4.2.', __METHOD__), \E_USER_DEPRECATED);
}
}
/**
* {@inheritdoc}
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
return $this->doTrans($id, $parameters, $domain, $locale);
}
/**
* {@inheritdoc}
*/
public function setLocale($locale)
{
$this->doSetLocale($locale);
}
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%count%" parameter.', __METHOD__), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), \E_USER_DEPRECATED);
if ($this->selector) {
return strtr($this->selector->choose((string) $id, $number, $locale ?: $this->getLocale()), $parameters);
}
return $this->trans($id, array('%count%' => $number) + $parameters, $domain, $locale);
return $this->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
}
private function getPluralizationRule(int $number, string $locale): int
private function getPluralizationRule(float $number, string $locale): int
{
return PluralizationRules::get($number, $locale, false);
}

View File

@@ -11,7 +11,7 @@
namespace Symfony\Component\Translation;
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use IdentityTranslator instead.', Interval::class), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use IdentityTranslator instead.', Interval::class), \E_USER_DEPRECATED);
use Symfony\Component\Translation\Exception\InvalidArgumentException;
@@ -99,7 +99,7 @@ class Interval
EOF;
}
private static function convertNumber($number)
private static function convertNumber(string $number): float
{
if ('-Inf' === $number) {
return log(0);

View File

@@ -1,4 +1,4 @@
Copyright (c) 2004-2018 Fabien Potencier
Copyright (c) 2004-2022 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -25,7 +25,7 @@ class ArrayLoader implements LoaderInterface
*/
public function load($resource, $locale, $domain = 'messages')
{
$this->flatten($resource);
$resource = $this->flatten($resource);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($resource, $domain);
@@ -36,31 +36,23 @@ class ArrayLoader implements LoaderInterface
* Flattens an nested array of translations.
*
* The scheme used is:
* 'key' => array('key2' => array('key3' => 'value'))
* 'key' => ['key2' => ['key3' => 'value']]
* Becomes:
* 'key.key2.key3' => 'value'
*
* This function takes an array by reference and will modify it
*
* @param array &$messages The array that will be flattened
* @param array $subnode Current subnode being parsed, used internally for recursive calls
* @param string $path Current path being parsed, used internally for recursive calls
*/
private function flatten(array &$messages, array $subnode = null, $path = null)
private function flatten(array $messages): array
{
if (null === $subnode) {
$subnode = &$messages;
}
foreach ($subnode as $key => $value) {
$result = [];
foreach ($messages as $key => $value) {
if (\is_array($value)) {
$nodePath = $path ? $path.'.'.$key : $key;
$this->flatten($messages, $value, $nodePath);
if (null === $path) {
unset($messages[$key]);
foreach ($this->flatten($value) as $k => $v) {
$result[$key.'.'.$k] = $v;
}
} elseif (null !== $path) {
$messages[$path.'.'.$key] = $value;
} else {
$result[$key] = $value;
}
}
return $result;
}
}

View File

@@ -29,7 +29,7 @@ class CsvFileLoader extends FileLoader
*/
protected function loadResource($resource)
{
$messages = array();
$messages = [];
try {
$file = new \SplFileObject($resource, 'rb');
@@ -41,6 +41,10 @@ class CsvFileLoader extends FileLoader
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
foreach ($file as $data) {
if (false === $data) {
continue;
}
if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === \count($data)) {
$messages[$data[0]] = $data[1];
}

View File

@@ -37,7 +37,7 @@ abstract class FileLoader extends ArrayLoader
// empty resource
if (null === $messages) {
$messages = array();
$messages = [];
}
// not an array
@@ -47,7 +47,7 @@ abstract class FileLoader extends ArrayLoader
$catalogue = parent::load($messages, $locale, $domain);
if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
if (class_exists(FileResource::class)) {
$catalogue->addResource(new FileResource($resource));
}

View File

@@ -43,7 +43,7 @@ class IcuDatFileLoader extends IcuResFileLoader
}
if (!$rb) {
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
throw new InvalidResourceException(sprintf('Cannot load resource "%s".', $resource));
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
@@ -52,7 +52,7 @@ class IcuDatFileLoader extends IcuResFileLoader
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
if (class_exists(FileResource::class)) {
$catalogue->addResource(new FileResource($resource.'.dat'));
}

View File

@@ -43,7 +43,7 @@ class IcuResFileLoader implements LoaderInterface
}
if (!$rb) {
throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
throw new InvalidResourceException(sprintf('Cannot load resource "%s".', $resource));
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
}
@@ -52,7 +52,7 @@ class IcuResFileLoader implements LoaderInterface
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);
if (class_exists('Symfony\Component\Config\Resource\DirectoryResource')) {
if (class_exists(DirectoryResource::class)) {
$catalogue->addResource(new DirectoryResource($resource));
}
@@ -75,7 +75,7 @@ class IcuResFileLoader implements LoaderInterface
*
* @return array the flattened ResourceBundle
*/
protected function flatten(\ResourceBundle $rb, array &$messages = array(), $path = null)
protected function flatten(\ResourceBundle $rb, array &$messages = [], $path = null)
{
foreach ($rb as $key => $value) {
$nodePath = $path ? $path.'.'.$key : $key;

View File

@@ -25,12 +25,12 @@ class JsonFileLoader extends FileLoader
*/
protected function loadResource($resource)
{
$messages = array();
$messages = [];
if ($data = file_get_contents($resource)) {
$messages = json_decode($data, true);
if (0 < $errorCode = json_last_error()) {
throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)));
throw new InvalidResourceException('Error parsing JSON: '.$this->getJSONErrorMessage($errorCode));
}
}
@@ -39,23 +39,19 @@ class JsonFileLoader extends FileLoader
/**
* Translates JSON_ERROR_* constant into meaningful message.
*
* @param int $errorCode Error code returned by json_last_error() call
*
* @return string Message string
*/
private function getJSONErrorMessage($errorCode)
private function getJSONErrorMessage(int $errorCode): string
{
switch ($errorCode) {
case JSON_ERROR_DEPTH:
case \JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded';
case JSON_ERROR_STATE_MISMATCH:
case \JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch';
case JSON_ERROR_CTRL_CHAR:
case \JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
case JSON_ERROR_SYNTAX:
case \JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON';
case JSON_ERROR_UTF8:
case \JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
default:
return 'Unknown error';

View File

@@ -19,21 +19,21 @@ use Symfony\Component\Translation\Exception\InvalidResourceException;
class MoFileLoader extends FileLoader
{
/**
* Magic used for validating the format of a MO file as well as
* Magic used for validating the format of an MO file as well as
* detecting if the machine used to create that file was little endian.
*/
const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
public const MO_LITTLE_ENDIAN_MAGIC = 0x950412DE;
/**
* Magic used for validating the format of a MO file as well as
* Magic used for validating the format of an MO file as well as
* detecting if the machine used to create that file was big endian.
*/
const MO_BIG_ENDIAN_MAGIC = 0xde120495;
public const MO_BIG_ENDIAN_MAGIC = 0xDE120495;
/**
* The size of the header of a MO file in bytes.
* The size of the header of an MO file in bytes.
*/
const MO_HEADER_SIZE = 28;
public const MO_HEADER_SIZE = 28;
/**
* Parses machine object (MO) format, independent of the machine's endian it
@@ -71,7 +71,7 @@ class MoFileLoader extends FileLoader
// offsetHashes
$this->readLong($stream, $isBigEndian);
$messages = array();
$messages = [];
for ($i = 0; $i < $count; ++$i) {
$pluralId = null;
@@ -89,8 +89,8 @@ class MoFileLoader extends FileLoader
fseek($stream, $offset);
$singularId = fread($stream, $length);
if (false !== strpos($singularId, "\000")) {
list($singularId, $pluralId) = explode("\000", $singularId);
if (str_contains($singularId, "\000")) {
[$singularId, $pluralId] = explode("\000", $singularId);
}
fseek($stream, $offsetTranslated + $i * 8);
@@ -104,24 +104,19 @@ class MoFileLoader extends FileLoader
fseek($stream, $offset);
$translated = fread($stream, $length);
if (false !== strpos($translated, "\000")) {
if (str_contains($translated, "\000")) {
$translated = explode("\000", $translated);
}
$ids = array('singular' => $singularId, 'plural' => $pluralId);
$ids = ['singular' => $singularId, 'plural' => $pluralId];
$item = compact('ids', 'translated');
if (\is_array($item['translated'])) {
$messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
if (!empty($item['ids']['singular'])) {
$id = $item['ids']['singular'];
if (isset($item['ids']['plural'])) {
$plurals = array();
foreach ($item['translated'] as $plural => $translated) {
$plurals[] = sprintf('{%d} %s', $plural, $translated);
}
$messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
$id .= '|'.$item['ids']['plural'];
}
} elseif (!empty($item['ids']['singular'])) {
$messages[$item['ids']['singular']] = stripcslashes($item['translated']);
$messages[$id] = stripcslashes(implode('|', (array) $item['translated']));
}
}

View File

@@ -18,11 +18,25 @@ namespace Symfony\Component\Translation\Loader;
*/
class PhpFileLoader extends FileLoader
{
private static $cache = [];
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
{
return require $resource;
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))) {
self::$cache = null;
}
if (null === self::$cache) {
return require $resource;
}
if (isset(self::$cache[$resource])) {
return self::$cache[$resource];
}
return self::$cache[$resource] = require $resource;
}
}

View File

@@ -12,7 +12,7 @@
namespace Symfony\Component\Translation\Loader;
/**
* @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
* @copyright Copyright (c) 2010, Union of RAD https://github.com/UnionOfRAD/lithium
* @copyright Copyright (c) 2012, Clemens Tolboom
*/
class PoFileLoader extends FileLoader
@@ -20,7 +20,7 @@ class PoFileLoader extends FileLoader
/**
* Parses portable object (PO) format.
*
* From http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
* From https://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
* we should be able to parse files having:
*
* white-space
@@ -64,14 +64,14 @@ class PoFileLoader extends FileLoader
{
$stream = fopen($resource, 'r');
$defaults = array(
'ids' => array(),
$defaults = [
'ids' => [],
'translated' => null,
);
];
$messages = array();
$messages = [];
$item = $defaults;
$flags = array();
$flags = [];
while ($line = fgets($stream)) {
$line = trim($line);
@@ -82,7 +82,7 @@ class PoFileLoader extends FileLoader
$this->addMessage($messages, $item);
}
$item = $defaults;
$flags = array();
$flags = [];
} elseif ('#,' === substr($line, 0, 2)) {
$flags = array_map('trim', explode(',', substr($line, 2)));
} elseif ('msgid "' === substr($line, 0, 7)) {
@@ -126,23 +126,24 @@ class PoFileLoader extends FileLoader
*/
private function addMessage(array &$messages, array $item)
{
if (\is_array($item['translated'])) {
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]);
if (!empty($item['ids']['singular'])) {
$id = stripcslashes($item['ids']['singular']);
if (isset($item['ids']['plural'])) {
$plurals = $item['translated'];
// PO are by definition indexed so sort by index.
ksort($plurals);
// Make sure every index is filled.
end($plurals);
$count = key($plurals);
// Fill missing spots with '-'.
$empties = array_fill(0, $count + 1, '-');
$plurals += $empties;
ksort($plurals);
$messages[stripcslashes($item['ids']['plural'])] = stripcslashes(implode('|', $plurals));
$id .= '|'.stripcslashes($item['ids']['plural']);
}
} elseif (!empty($item['ids']['singular'])) {
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);
$translated = (array) $item['translated'];
// PO are by definition indexed so sort by index.
ksort($translated);
// Make sure every index is filled.
end($translated);
$count = key($translated);
// Fill missing spots with '-'.
$empties = array_fill(0, $count + 1, '-');
$translated += $empties;
ksort($translated);
$messages[$id] = stripcslashes(implode('|', $translated));
}
}
}

View File

@@ -65,7 +65,7 @@ class QtFileLoader implements LoaderInterface
$translation = $translation->nextSibling;
}
if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
if (class_exists(FileResource::class)) {
$catalogue->addResource(new FileResource($resource));
}
}

View File

@@ -41,24 +41,24 @@ class XliffFileLoader implements LoaderInterface
$catalogue = new MessageCatalogue($locale);
$this->extract($resource, $catalogue, $domain);
if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
if (class_exists(FileResource::class)) {
$catalogue->addResource(new FileResource($resource));
}
return $catalogue;
}
private function extract($resource, MessageCatalogue $catalogue, $domain)
private function extract($resource, MessageCatalogue $catalogue, string $domain)
{
try {
$dom = XmlUtils::loadFile($resource);
} catch (\InvalidArgumentException $e) {
throw new InvalidResourceException(sprintf('Unable to load "%s": %s', $resource, $e->getMessage()), $e->getCode(), $e);
throw new InvalidResourceException(sprintf('Unable to load "%s": ', $resource).$e->getMessage(), $e->getCode(), $e);
}
$xliffVersion = XliffUtils::getVersionNumber($dom);
if ($errors = XliffUtils::validateSchema($dom)) {
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: %s', $xliffVersion, XliffUtils::getErrorsAsString($errors)));
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: ', $resource).XliffUtils::getErrorsAsString($errors));
}
if ('1.2' === $xliffVersion) {
@@ -72,55 +72,64 @@ class XliffFileLoader implements LoaderInterface
/**
* Extract messages and metadata from DOMDocument into a MessageCatalogue.
*
* @param \DOMDocument $dom Source to extract messages and metadata
* @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata
* @param string $domain The domain
*/
private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
{
$xml = simplexml_import_dom($dom);
$encoding = strtoupper($dom->encoding);
$encoding = $dom->encoding ? strtoupper($dom->encoding) : null;
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
$attributes = $translation->attributes();
$namespace = 'urn:oasis:names:tc:xliff:document:1.2';
$xml->registerXPathNamespace('xliff', $namespace);
if (!(isset($attributes['resname']) || isset($translation->source))) {
continue;
}
foreach ($xml->xpath('//xliff:file') as $file) {
$fileAttributes = $file->attributes();
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
// 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) (isset($translation->target) ? $translation->target : $translation->source), $encoding);
$file->registerXPathNamespace('xliff', $namespace);
$catalogue->set((string) $source, $target, $domain);
foreach ($file->xpath('.//xliff:trans-unit') as $translation) {
$attributes = $translation->attributes();
$metadata = array();
if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
$metadata['notes'] = $notes;
}
if (isset($translation->target) && $translation->target->attributes()) {
$metadata['target-attributes'] = array();
foreach ($translation->target->attributes() as $key => $value) {
$metadata['target-attributes'][$key] = (string) $value;
if (!(isset($attributes['resname']) || isset($translation->source))) {
continue;
}
}
if (isset($attributes['id'])) {
$metadata['id'] = (string) $attributes['id'];
}
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
// 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->setMetadata((string) $source, $metadata, $domain);
$catalogue->set((string) $source, $target, $domain);
$metadata = [
'source' => (string) $translation->source,
'file' => [
'original' => (string) $fileAttributes['original'],
],
];
if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
$metadata['notes'] = $notes;
}
if (isset($translation->target) && $translation->target->attributes()) {
$metadata['target-attributes'] = [];
foreach ($translation->target->attributes() as $key => $value) {
$metadata['target-attributes'][$key] = (string) $value;
}
}
if (isset($attributes['id'])) {
$metadata['id'] = (string) $attributes['id'];
}
$catalogue->setMetadata((string) $source, $metadata, $domain);
}
}
}
private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
{
$xml = simplexml_import_dom($dom);
$encoding = strtoupper($dom->encoding);
$encoding = $dom->encoding ? strtoupper($dom->encoding) : null;
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
@@ -130,22 +139,22 @@ class XliffFileLoader implements LoaderInterface
// 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) (isset($segment->target) ? $segment->target : $source), $encoding);
$target = $this->utf8ToCharset((string) ($segment->target ?? $source), $encoding);
$catalogue->set((string) $source, $target, $domain);
$metadata = array();
$metadata = [];
if (isset($segment->target) && $segment->target->attributes()) {
$metadata['target-attributes'] = array();
$metadata['target-attributes'] = [];
foreach ($segment->target->attributes() as $key => $value) {
$metadata['target-attributes'][$key] = (string) $value;
}
}
if (isset($unit->notes)) {
$metadata['notes'] = array();
$metadata['notes'] = [];
foreach ($unit->notes->note as $noteNode) {
$note = array();
$note = [];
foreach ($noteNode->attributes() as $key => $value) {
$note[$key] = (string) $value;
}
@@ -173,7 +182,7 @@ class XliffFileLoader implements LoaderInterface
private function parseNotesMetadata(\SimpleXMLElement $noteElement = null, string $encoding = null): array
{
$notes = array();
$notes = [];
if (null === $noteElement) {
return $notes;
@@ -182,7 +191,7 @@ class XliffFileLoader implements LoaderInterface
/** @var \SimpleXMLElement $xmlNote */
foreach ($noteElement as $xmlNote) {
$noteAttributes = $xmlNote->attributes();
$note = array('content' => $this->utf8ToCharset((string) $xmlNote, $encoding));
$note = ['content' => $this->utf8ToCharset((string) $xmlNote, $encoding)];
if (isset($noteAttributes['priority'])) {
$note['priority'] = (int) $noteAttributes['priority'];
}

View File

@@ -32,7 +32,7 @@ class YamlFileLoader extends FileLoader
protected function loadResource($resource)
{
if (null === $this->yamlParser) {
if (!class_exists('Symfony\Component\Yaml\Parser')) {
if (!class_exists(\Symfony\Component\Yaml\Parser::class)) {
throw new LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
}
@@ -42,9 +42,13 @@ class YamlFileLoader extends FileLoader
try {
$messages = $this->yamlParser->parseFile($resource, Yaml::PARSE_CONSTANT);
} catch (ParseException $e) {
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
throw new InvalidResourceException(sprintf('The file "%s" does not contain valid YAML: ', $resource).$e->getMessage(), 0, $e);
}
return $messages;
if (null !== $messages && !\is_array($messages)) {
throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource));
}
return $messages ?: [];
}
}

View File

@@ -14,6 +14,7 @@ namespace Symfony\Component\Translation;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
@@ -30,15 +31,14 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
/**
* @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
* @param LoggerInterface $logger
*/
public function __construct($translator, LoggerInterface $logger)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be an instance of "%s", "%s" given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
if (!$translator instanceof TranslatorBagInterface) {
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', \get_class($translator)));
if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator)));
}
$this->translator = $translator;
@@ -48,7 +48,7 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
/**
* {@inheritdoc}
*/
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
$trans = $this->translator->trans($id, $parameters, $domain, $locale);
$this->log($id, $domain, $locale);
@@ -61,12 +61,12 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
*
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%count%" parameter.', __METHOD__), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), \E_USER_DEPRECATED);
if ($this->translator instanceof TranslatorInterface) {
$trans = $this->translator->trans($id, array('%count%' => $number) + $parameters, $domain, $locale);
$trans = $this->translator->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
} else {
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
@@ -81,7 +81,13 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
*/
public function setLocale($locale)
{
$prev = $this->translator->getLocale();
$this->translator->setLocale($locale);
if ($prev === $locale) {
return;
}
$this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale));
}
/**
@@ -111,7 +117,7 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
return $this->translator->getFallbackLocales();
}
return array();
return [];
}
/**
@@ -124,12 +130,8 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
/**
* Logs for missing translations.
*
* @param string $id
* @param string|null $domain
* @param string|null $locale
*/
private function log($id, $domain, $locale)
private function log(?string $id, ?string $domain, ?string $locale)
{
if (null === $domain) {
$domain = 'messages';
@@ -142,9 +144,9 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
}
if ($catalogue->has($id, $domain)) {
$this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
$this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
} else {
$this->logger->warning('Translation not found.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
$this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
}
}
}

View File

@@ -19,19 +19,22 @@ use Symfony\Component\Translation\Exception\LogicException;
*/
class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
{
private $messages = array();
private $metadata = array();
private $resources = array();
private $messages = [];
private $metadata = [];
private $resources = [];
private $locale;
private $fallbackCatalogue;
private $parent;
/**
* @param string $locale The locale
* @param array $messages An array of messages classified by domain
* @param array $messages An array of messages classified by domain
*/
public function __construct(?string $locale, array $messages = array())
public function __construct(?string $locale, array $messages = [])
{
if (null === $locale) {
@trigger_error(sprintf('Passing "null" to the first argument of the "%s" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), \E_USER_DEPRECATED);
}
$this->locale = $locale;
$this->messages = $messages;
}
@@ -49,12 +52,11 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
*/
public function getDomains()
{
$domains = array();
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
$domains = [];
foreach ($this->messages as $domain => $messages) {
if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
$domain = substr($domain, 0, $i);
if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
$domain = substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
}
$domains[$domain] = $domain;
}
@@ -68,18 +70,22 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
public function all($domain = null)
{
if (null !== $domain) {
return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? array()) + ($this->messages[$domain] ?? array());
// skip messages merge if intl-icu requested explicitly
if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
return $this->messages[$domain] ?? [];
}
return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
}
$allMessages = array();
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
$allMessages = [];
foreach ($this->messages as $domain => $messages) {
if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
$domain = substr($domain, 0, $i);
$allMessages[$domain] = $messages + ($allMessages[$domain] ?? array());
if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
$domain = substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
$allMessages[$domain] = $messages + ($allMessages[$domain] ?? []);
} else {
$allMessages[$domain] = ($allMessages[$domain] ?? array()) + $messages;
$allMessages[$domain] = ($allMessages[$domain] ?? []) + $messages;
}
}
@@ -91,7 +97,7 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
*/
public function set($id, $translation, $domain = 'messages')
{
$this->add(array($id => $translation), $domain);
$this->add([$id => $translation], $domain);
}
/**
@@ -153,10 +159,14 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
*/
public function add($messages, $domain = 'messages')
{
if (!isset($this->messages[$domain])) {
$this->messages[$domain] = $messages;
} else {
$this->messages[$domain] = array_replace($this->messages[$domain], $messages);
$altDomain = str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain.self::INTL_DOMAIN_SUFFIX;
foreach ($messages as $id => $message) {
unset($this->messages[$altDomain][$id]);
$this->messages[$domain][$id] = $message;
}
if ([] === ($this->messages[$altDomain] ?? null)) {
unset($this->messages[$altDomain]);
}
}
@@ -166,7 +176,7 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
public function addCatalogue(MessageCatalogueInterface $catalogue)
{
if ($catalogue->getLocale() !== $this->locale) {
throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"', $catalogue->getLocale(), $this->locale));
throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale));
}
foreach ($catalogue->all() as $domain => $messages) {
@@ -261,6 +271,8 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
return $this->metadata[$domain][$key];
}
}
return null;
}
/**
@@ -277,7 +289,7 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
public function deleteMetadata($key = '', $domain = 'messages')
{
if ('' == $domain) {
$this->metadata = array();
$this->metadata = [];
} elseif ('' == $key) {
unset($this->metadata[$domain]);
} else {

View File

@@ -20,7 +20,7 @@ use Symfony\Component\Config\Resource\ResourceInterface;
*/
interface MessageCatalogueInterface
{
const INTL_DOMAIN_SUFFIX = '+intl-icu';
public const INTL_DOMAIN_SUFFIX = '+intl-icu';
/**
* Gets the catalogue locale.

View File

@@ -11,7 +11,7 @@
namespace Symfony\Component\Translation;
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use IdentityTranslator instead.', MessageSelector::class), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use IdentityTranslator instead.', MessageSelector::class), \E_USER_DEPRECATED);
use Symfony\Component\Translation\Exception\InvalidArgumentException;
@@ -53,15 +53,15 @@ class MessageSelector
*/
public function choose($message, $number, $locale)
{
$parts = array();
$parts = [];
if (preg_match('/^\|++$/', $message)) {
$parts = explode('|', $message);
} elseif (preg_match_all('/(?:\|\||[^\|])++/', $message, $matches)) {
$parts = $matches[0];
}
$explicitRules = array();
$standardRules = array();
$explicitRules = [];
$standardRules = [];
foreach ($parts as $part) {
$part = trim(str_replace('||', '|', $part));

View File

@@ -20,20 +20,22 @@ namespace Symfony\Component\Translation;
*/
class PluralizationRules
{
private static $rules = array();
private static $rules = [];
/**
* Returns the plural position to use for the given locale and number.
*
* @param int $number The number
* @param float $number The number
* @param string $locale The locale
*
* @return int The plural position
*/
public static function get($number, $locale/*, bool $triggerDeprecation = true*/)
public static function get($number, $locale/* , bool $triggerDeprecation = true */)
{
if (3 > \func_num_args() || \func_get_arg(2)) {
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
$number = abs($number);
if (3 > \func_num_args() || func_get_arg(2)) {
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), \E_USER_DEPRECATED);
}
if ('pt_BR' === $locale) {
@@ -41,12 +43,12 @@ class PluralizationRules
$locale = 'xbr';
}
if (\strlen($locale) > 3) {
if ('en_US_POSIX' !== $locale && \strlen($locale) > 3) {
$locale = substr($locale, 0, -\strlen(strrchr($locale, '_')));
}
if (isset(self::$rules[$locale])) {
$return = \call_user_func(self::$rules[$locale], $number);
$return = self::$rules[$locale]($number);
if (!\is_int($return) || $return < 0) {
return 0;
@@ -86,6 +88,7 @@ class PluralizationRules
case 'de':
case 'el':
case 'en':
case 'en_US_POSIX':
case 'eo':
case 'es':
case 'et':
@@ -144,7 +147,7 @@ class PluralizationRules
case 'xbr':
case 'ti':
case 'wa':
return ((0 == $number) || (1 == $number)) ? 0 : 1;
return ($number < 2) ? 0 : 1;
case 'be':
case 'bs':
@@ -202,7 +205,7 @@ class PluralizationRules
*/
public static function set(callable $rule, $locale)
{
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), \E_USER_DEPRECATED);
if ('pt_BR' === $locale) {
// temporary set a locale for brazilian

View File

@@ -3,11 +3,31 @@ Translation Component
The Translation component provides tools to internationalize your application.
Getting Started
---------------
```
$ composer require symfony/translation
```
```php
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\ArrayLoader;
$translator = new Translator('fr_FR');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', [
'Hello World!' => 'Bonjour !',
], 'fr_FR');
echo $translator->trans('Hello World!'); // outputs « Bonjour ! »
```
Resources
---------
* [Documentation](https://symfony.com/doc/current/components/translation/index.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
* [Documentation](https://symfony.com/doc/current/translation.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)

View File

@@ -27,13 +27,12 @@ class TranslationReader implements TranslationReaderInterface
*
* @var array
*/
private $loaders = array();
private $loaders = [];
/**
* Adds a loader to the translation extractor.
*
* @param string $format The format of the loader
* @param LoaderInterface $loader
* @param string $format The format of the loader
*/
public function addLoader($format, LoaderInterface $loader)
{

View File

@@ -23,8 +23,7 @@ interface TranslationReaderInterface
/**
* Reads translation messages from a directory to the catalogue.
*
* @param string $directory
* @param MessageCatalogue $catalogue
* @param string $directory
*/
public function read($directory, MessageCatalogue $catalogue);
}

View File

@@ -0,0 +1,278 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
if ('cli' !== \PHP_SAPI) {
throw new Exception('This script must be run from the command line.');
}
$usageInstructions = <<<END
Usage instructions
-------------------------------------------------------------------------------
$ cd symfony-code-root-directory/
# show the translation status of all locales
$ php translation-status.php
# only show the translation status of incomplete or erroneous locales
$ php translation-status.php --incomplete
# show the translation status of all locales, all their missing translations and mismatches between trans-unit id and source
$ php translation-status.php -v
# show the status of a single locale
$ php translation-status.php fr
# show the status of a single locale, missing translations and mismatches between trans-unit id and source
$ php translation-status.php fr -v
END;
$config = [
// if TRUE, the full list of missing translations is displayed
'verbose_output' => false,
// NULL = analyze all locales
'locale_to_analyze' => null,
// append --incomplete to only show incomplete languages
'include_completed_languages' => true,
// the reference files all the other translations are compared to
'original_files' => [
'src/Symfony/Component/Form/Resources/translations/validators.en.xlf',
'src/Symfony/Component/Security/Core/Resources/translations/security.en.xlf',
'src/Symfony/Component/Validator/Resources/translations/validators.en.xlf',
],
];
$argc = $_SERVER['argc'];
$argv = $_SERVER['argv'];
if ($argc > 4) {
echo str_replace('translation-status.php', $argv[0], $usageInstructions);
exit(1);
}
foreach (array_slice($argv, 1) as $argumentOrOption) {
if ('--incomplete' === $argumentOrOption) {
$config['include_completed_languages'] = false;
continue;
}
if (0 === strpos($argumentOrOption, '-')) {
$config['verbose_output'] = true;
} else {
$config['locale_to_analyze'] = $argumentOrOption;
}
}
foreach ($config['original_files'] as $originalFilePath) {
if (!file_exists($originalFilePath)) {
echo sprintf('The following file does not exist. Make sure that you execute this command at the root dir of the Symfony code repository.%s %s', \PHP_EOL, $originalFilePath);
exit(1);
}
}
$totalMissingTranslations = 0;
$totalTranslationMismatches = 0;
foreach ($config['original_files'] as $originalFilePath) {
$translationFilePaths = findTranslationFiles($originalFilePath, $config['locale_to_analyze']);
$translationStatus = calculateTranslationStatus($originalFilePath, $translationFilePaths);
$totalMissingTranslations += array_sum(array_map(function ($translation) {
return count($translation['missingKeys']);
}, array_values($translationStatus)));
$totalTranslationMismatches += array_sum(array_map(function ($translation) {
return count($translation['mismatches']);
}, array_values($translationStatus)));
printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output'], $config['include_completed_languages']);
}
exit($totalTranslationMismatches > 0 ? 1 : 0);
function findTranslationFiles($originalFilePath, $localeToAnalyze)
{
$translations = [];
$translationsDir = dirname($originalFilePath);
$originalFileName = basename($originalFilePath);
$translationFileNamePattern = str_replace('.en.', '.*.', $originalFileName);
$translationFiles = glob($translationsDir.'/'.$translationFileNamePattern, \GLOB_NOSORT);
sort($translationFiles);
foreach ($translationFiles as $filePath) {
$locale = extractLocaleFromFilePath($filePath);
if (null !== $localeToAnalyze && $locale !== $localeToAnalyze) {
continue;
}
$translations[$locale] = $filePath;
}
return $translations;
}
function calculateTranslationStatus($originalFilePath, $translationFilePaths)
{
$translationStatus = [];
$allTranslationKeys = extractTranslationKeys($originalFilePath);
foreach ($translationFilePaths as $locale => $translationPath) {
$translatedKeys = extractTranslationKeys($translationPath);
$missingKeys = array_diff_key($allTranslationKeys, $translatedKeys);
$mismatches = findTransUnitMismatches($allTranslationKeys, $translatedKeys);
$translationStatus[$locale] = [
'total' => count($allTranslationKeys),
'translated' => count($translatedKeys),
'missingKeys' => $missingKeys,
'mismatches' => $mismatches,
];
$translationStatus[$locale]['is_completed'] = isTranslationCompleted($translationStatus[$locale]);
}
return $translationStatus;
}
function isTranslationCompleted(array $translationStatus): bool
{
return $translationStatus['total'] === $translationStatus['translated'] && 0 === count($translationStatus['mismatches']);
}
function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput, $includeCompletedLanguages)
{
printTitle($originalFilePath);
printTable($translationStatus, $verboseOutput, $includeCompletedLanguages);
echo \PHP_EOL.\PHP_EOL;
}
function extractLocaleFromFilePath($filePath)
{
$parts = explode('.', $filePath);
return $parts[count($parts) - 2];
}
function extractTranslationKeys($filePath)
{
$translationKeys = [];
$contents = new \SimpleXMLElement(file_get_contents($filePath));
foreach ($contents->file->body->{'trans-unit'} as $translationKey) {
$translationId = (string) $translationKey['id'];
$translationKey = (string) $translationKey->source;
$translationKeys[$translationId] = $translationKey;
}
return $translationKeys;
}
/**
* Check whether the trans-unit id and source match with the base translation.
*/
function findTransUnitMismatches(array $baseTranslationKeys, array $translatedKeys): array
{
$mismatches = [];
foreach ($baseTranslationKeys as $translationId => $translationKey) {
if (!isset($translatedKeys[$translationId])) {
continue;
}
if ($translatedKeys[$translationId] !== $translationKey) {
$mismatches[$translationId] = [
'found' => $translatedKeys[$translationId],
'expected' => $translationKey,
];
}
}
return $mismatches;
}
function printTitle($title)
{
echo $title.\PHP_EOL;
echo str_repeat('=', strlen($title)).\PHP_EOL.\PHP_EOL;
}
function printTable($translations, $verboseOutput, bool $includeCompletedLanguages)
{
if (0 === count($translations)) {
echo 'No translations found';
return;
}
$longestLocaleNameLength = max(array_map('strlen', array_keys($translations)));
foreach ($translations as $locale => $translation) {
if (!$includeCompletedLanguages && $translation['is_completed']) {
continue;
}
if ($translation['translated'] > $translation['total']) {
textColorRed();
} elseif (count($translation['mismatches']) > 0) {
textColorRed();
} elseif ($translation['is_completed']) {
textColorGreen();
}
echo sprintf(
'| Locale: %-'.$longestLocaleNameLength.'s | Translated: %2d/%2d | Mismatches: %d |',
$locale,
$translation['translated'],
$translation['total'],
count($translation['mismatches'])
).\PHP_EOL;
textColorNormal();
$shouldBeClosed = false;
if (true === $verboseOutput && count($translation['missingKeys']) > 0) {
echo '| Missing Translations:'.\PHP_EOL;
foreach ($translation['missingKeys'] as $id => $content) {
echo sprintf('| (id=%s) %s', $id, $content).\PHP_EOL;
}
$shouldBeClosed = true;
}
if (true === $verboseOutput && count($translation['mismatches']) > 0) {
echo '| Mismatches between trans-unit id and source:'.\PHP_EOL;
foreach ($translation['mismatches'] as $id => $content) {
echo sprintf('| (id=%s) Expected: %s', $id, $content['expected']).\PHP_EOL;
echo sprintf('| Found: %s', $content['found']).\PHP_EOL;
}
$shouldBeClosed = true;
}
if ($shouldBeClosed) {
echo str_repeat('-', 80).\PHP_EOL;
}
}
}
function textColorGreen()
{
echo "\033[32m";
}
function textColorRed()
{
echo "\033[31m";
}
function textColorNormal()
{
echo "\033[0m";
}

View File

@@ -7,12 +7,11 @@
"en_AT": "en_150",
"en_AU": "en_001",
"en_BB": "en_001",
"en_BE": "en_001",
"en_BE": "en_150",
"en_BM": "en_001",
"en_BS": "en_001",
"en_BW": "en_001",
"en_BZ": "en_001",
"en_CA": "en_001",
"en_CC": "en_001",
"en_CH": "en_150",
"en_CK": "en_001",
@@ -55,6 +54,7 @@
"en_MS": "en_001",
"en_MT": "en_001",
"en_MU": "en_001",
"en_MV": "en_001",
"en_MW": "en_001",
"en_MY": "en_001",
"en_NA": "en_001",
@@ -65,7 +65,6 @@
"en_NU": "en_001",
"en_NZ": "en_001",
"en_PG": "en_001",
"en_PH": "en_001",
"en_PK": "en_001",
"en_PN": "en_001",
"en_PW": "en_001",
@@ -117,6 +116,11 @@
"es_US": "es_419",
"es_UY": "es_419",
"es_VE": "es_419",
"ff_Adlm": "root",
"hi_Latn": "en_IN",
"ks_Deva": "root",
"nb": "no",
"nn": "no",
"pa_Arab": "root",
"pt_AO": "pt_PT",
"pt_CH": "pt_PT",
@@ -128,6 +132,7 @@
"pt_MZ": "pt_PT",
"pt_ST": "pt_PT",
"pt_TL": "pt_PT",
"sd_Deva": "root",
"sr_Latn": "root",
"uz_Arab": "root",
"uz_Cyrl": "root",

View File

@@ -1,74 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Catalogue;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageCatalogueInterface;
abstract class AbstractOperationTest extends TestCase
{
public function testGetEmptyDomains()
{
$this->assertEquals(
array(),
$this->createOperation(
new MessageCatalogue('en'),
new MessageCatalogue('en')
)->getDomains()
);
}
public function testGetMergedDomains()
{
$this->assertEquals(
array('a', 'b', 'c'),
$this->createOperation(
new MessageCatalogue('en', array('a' => array(), 'b' => array())),
new MessageCatalogue('en', array('b' => array(), 'c' => array()))
)->getDomains()
);
}
public function testGetMessagesFromUnknownDomain()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$this->createOperation(
new MessageCatalogue('en'),
new MessageCatalogue('en')
)->getMessages('domain');
}
public function testGetEmptyMessages()
{
$this->assertEquals(
array(),
$this->createOperation(
new MessageCatalogue('en', array('a' => array())),
new MessageCatalogue('en')
)->getMessages('a')
);
}
public function testGetEmptyResult()
{
$this->assertEquals(
new MessageCatalogue('en'),
$this->createOperation(
new MessageCatalogue('en'),
new MessageCatalogue('en')
)->getResult()
);
}
abstract protected function createOperation(MessageCatalogueInterface $source, MessageCatalogueInterface $target);
}

View File

@@ -1,97 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Catalogue;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageCatalogueInterface;
class MergeOperationTest extends AbstractOperationTest
{
public function testGetMessagesFromSingleDomain()
{
$operation = $this->createOperation(
new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b'))),
new MessageCatalogue('en', array('messages' => array('a' => 'new_a', 'c' => 'new_c')))
);
$this->assertEquals(
array('a' => 'old_a', 'b' => 'old_b', 'c' => 'new_c'),
$operation->getMessages('messages')
);
$this->assertEquals(
array('c' => 'new_c'),
$operation->getNewMessages('messages')
);
$this->assertEquals(
array(),
$operation->getObsoleteMessages('messages')
);
}
public function testGetResultFromSingleDomain()
{
$this->assertEquals(
new MessageCatalogue('en', array(
'messages' => array('a' => 'old_a', 'b' => 'old_b', 'c' => 'new_c'),
)),
$this->createOperation(
new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b'))),
new MessageCatalogue('en', array('messages' => array('a' => 'new_a', 'c' => 'new_c')))
)->getResult()
);
}
public function testGetResultFromIntlDomain()
{
$this->assertEquals(
new MessageCatalogue('en', array(
'messages' => array('a' => 'old_a', 'b' => 'old_b'),
'messages+intl-icu' => array('d' => 'old_d', 'c' => 'new_c'),
)),
$this->createOperation(
new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b'), 'messages+intl-icu' => array('d' => 'old_d'))),
new MessageCatalogue('en', array('messages+intl-icu' => array('a' => 'new_a', 'c' => 'new_c')))
)->getResult()
);
}
public function testGetResultWithMetadata()
{
$leftCatalogue = new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b')));
$leftCatalogue->setMetadata('a', 'foo', 'messages');
$leftCatalogue->setMetadata('b', 'bar', 'messages');
$rightCatalogue = new MessageCatalogue('en', array('messages' => array('b' => 'new_b', 'c' => 'new_c')));
$rightCatalogue->setMetadata('b', 'baz', 'messages');
$rightCatalogue->setMetadata('c', 'qux', 'messages');
$mergedCatalogue = new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b', 'c' => 'new_c')));
$mergedCatalogue->setMetadata('a', 'foo', 'messages');
$mergedCatalogue->setMetadata('b', 'bar', 'messages');
$mergedCatalogue->setMetadata('c', 'qux', 'messages');
$this->assertEquals(
$mergedCatalogue,
$this->createOperation(
$leftCatalogue,
$rightCatalogue
)->getResult()
);
}
protected function createOperation(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
{
return new MergeOperation($source, $target);
}
}

View File

@@ -1,96 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Catalogue;
use Symfony\Component\Translation\Catalogue\TargetOperation;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageCatalogueInterface;
class TargetOperationTest extends AbstractOperationTest
{
public function testGetMessagesFromSingleDomain()
{
$operation = $this->createOperation(
new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b'))),
new MessageCatalogue('en', array('messages' => array('a' => 'new_a', 'c' => 'new_c')))
);
$this->assertEquals(
array('a' => 'old_a', 'c' => 'new_c'),
$operation->getMessages('messages')
);
$this->assertEquals(
array('c' => 'new_c'),
$operation->getNewMessages('messages')
);
$this->assertEquals(
array('b' => 'old_b'),
$operation->getObsoleteMessages('messages')
);
}
public function testGetResultFromSingleDomain()
{
$this->assertEquals(
new MessageCatalogue('en', array(
'messages' => array('a' => 'old_a', 'c' => 'new_c'),
)),
$this->createOperation(
new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b'))),
new MessageCatalogue('en', array('messages' => array('a' => 'new_a', 'c' => 'new_c')))
)->getResult()
);
}
public function testGetResultFromIntlDomain()
{
$this->assertEquals(
new MessageCatalogue('en', array(
'messages' => array('a' => 'old_a'),
'messages+intl-icu' => array('c' => 'new_c'),
)),
$this->createOperation(
new MessageCatalogue('en', array('messages' => array('a' => 'old_a'), 'messages+intl-icu' => array('b' => 'old_b'))),
new MessageCatalogue('en', array('messages' => array('a' => 'new_a'), 'messages+intl-icu' => array('c' => 'new_c')))
)->getResult()
);
}
public function testGetResultWithMetadata()
{
$leftCatalogue = new MessageCatalogue('en', array('messages' => array('a' => 'old_a', 'b' => 'old_b')));
$leftCatalogue->setMetadata('a', 'foo', 'messages');
$leftCatalogue->setMetadata('b', 'bar', 'messages');
$rightCatalogue = new MessageCatalogue('en', array('messages' => array('b' => 'new_b', 'c' => 'new_c')));
$rightCatalogue->setMetadata('b', 'baz', 'messages');
$rightCatalogue->setMetadata('c', 'qux', 'messages');
$diffCatalogue = new MessageCatalogue('en', array('messages' => array('b' => 'old_b', 'c' => 'new_c')));
$diffCatalogue->setMetadata('b', 'bar', 'messages');
$diffCatalogue->setMetadata('c', 'qux', 'messages');
$this->assertEquals(
$diffCatalogue,
$this->createOperation(
$leftCatalogue,
$rightCatalogue
)->getResult()
);
}
protected function createOperation(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
{
return new TargetOperation($source, $target);
}
}

View File

@@ -1,207 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Command;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Translation\Command\XliffLintCommand;
/**
* Tests the XliffLintCommand.
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class XliffLintCommandTest extends TestCase
{
private $files;
public function testLintCorrectFile()
{
$tester = $this->createCommandTester();
$filename = $this->createFile();
$tester->execute(
array('filename' => $filename),
array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)
);
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
$this->assertContains('OK', trim($tester->getDisplay()));
}
public function testLintCorrectFiles()
{
$tester = $this->createCommandTester();
$filename1 = $this->createFile();
$filename2 = $this->createFile();
$tester->execute(
array('filename' => array($filename1, $filename2)),
array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)
);
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
$this->assertContains('OK', trim($tester->getDisplay()));
}
/**
* @dataProvider provideStrictFilenames
*/
public function testStrictFilenames($requireStrictFileNames, $fileNamePattern, $targetLanguage, $mustFail)
{
$tester = $this->createCommandTester($requireStrictFileNames);
$filename = $this->createFile('note', $targetLanguage, $fileNamePattern);
$tester->execute(
array('filename' => $filename),
array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)
);
$this->assertEquals($mustFail ? 1 : 0, $tester->getStatusCode());
$this->assertContains($mustFail ? '[WARNING] 0 XLIFF files have valid syntax and 1 contain errors.' : '[OK] All 1 XLIFF files contain valid syntax.', $tester->getDisplay());
}
public function testLintIncorrectXmlSyntax()
{
$tester = $this->createCommandTester();
$filename = $this->createFile('note <target>');
$tester->execute(array('filename' => $filename), array('decorated' => false));
$this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
$this->assertContains('Opening and ending tag mismatch: target line 6 and source', trim($tester->getDisplay()));
}
public function testLintIncorrectTargetLanguage()
{
$tester = $this->createCommandTester();
$filename = $this->createFile('note', 'es');
$tester->execute(array('filename' => $filename), array('decorated' => false));
$this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
$this->assertContains('There is a mismatch between the language included in the file name ("messages.en.xlf") and the "es" value used in the "target-language" attribute of the file.', trim($tester->getDisplay()));
}
/**
* @expectedException \RuntimeException
*/
public function testLintFileNotReadable()
{
$tester = $this->createCommandTester();
$filename = $this->createFile();
unlink($filename);
$tester->execute(array('filename' => $filename), array('decorated' => false));
}
public function testGetHelp()
{
$command = new XliffLintCommand();
$expected = <<<EOF
The <info>%command.name%</info> command lints a XLIFF file and outputs to STDOUT
the first encountered syntax error.
You can validates XLIFF contents passed from STDIN:
<info>cat filename | php %command.full_name%</info>
You can also validate the syntax of a file:
<info>php %command.full_name% filename</info>
Or of a whole directory:
<info>php %command.full_name% dirname</info>
<info>php %command.full_name% dirname --format=json</info>
EOF;
$this->assertEquals($expected, $command->getHelp());
}
/**
* @return string Path to the new file
*/
private function createFile($sourceContent = 'note', $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf')
{
$xliffContent = <<<XLIFF
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="$targetLanguage" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="note">
<source>$sourceContent</source>
<target>NOTE</target>
</trans-unit>
</body>
</file>
</xliff>
XLIFF;
$filename = sprintf('%s/translation-xliff-lint-test/%s', sys_get_temp_dir(), str_replace('%locale%', 'en', $fileNamePattern));
file_put_contents($filename, $xliffContent);
$this->files[] = $filename;
return $filename;
}
/**
* @return CommandTester
*/
private function createCommandTester($requireStrictFileNames = true, $application = null)
{
if (!$application) {
$application = new Application();
$application->add(new XliffLintCommand(null, null, null, $requireStrictFileNames));
}
$command = $application->find('lint:xliff');
if ($application) {
$command->setApplication($application);
}
return new CommandTester($command);
}
protected function setUp()
{
$this->files = array();
@mkdir(sys_get_temp_dir().'/translation-xliff-lint-test');
}
protected function tearDown()
{
foreach ($this->files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
rmdir(sys_get_temp_dir().'/translation-xliff-lint-test');
}
public function provideStrictFilenames()
{
yield array(false, 'messages.%locale%.xlf', 'en', false);
yield array(false, 'messages.%locale%.xlf', 'es', true);
yield array(false, '%locale%.messages.xlf', 'en', false);
yield array(false, '%locale%.messages.xlf', 'es', true);
yield array(true, 'messages.%locale%.xlf', 'en', false);
yield array(true, 'messages.%locale%.xlf', 'es', true);
yield array(true, '%locale%.messages.xlf', 'en', true);
yield array(true, '%locale%.messages.xlf', 'es', true);
}
}

View File

@@ -1,150 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\DataCollector;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
use Symfony\Component\Translation\DataCollectorTranslator;
class TranslationDataCollectorTest extends TestCase
{
protected function setUp()
{
if (!class_exists('Symfony\Component\HttpKernel\DataCollector\DataCollector')) {
$this->markTestSkipped('The "DataCollector" is not available');
}
}
public function testCollectEmptyMessages()
{
$translator = $this->getTranslator();
$translator->expects($this->any())->method('getCollectedMessages')->will($this->returnValue(array()));
$dataCollector = new TranslationDataCollector($translator);
$dataCollector->lateCollect();
$this->assertEquals(0, $dataCollector->getCountMissings());
$this->assertEquals(0, $dataCollector->getCountFallbacks());
$this->assertEquals(0, $dataCollector->getCountDefines());
$this->assertEquals(array(), $dataCollector->getMessages()->getValue());
}
public function testCollect()
{
$collectedMessages = array(
array(
'id' => 'foo',
'translation' => 'foo (en)',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 3),
'transChoiceNumber' => 3,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 3),
'transChoiceNumber' => 3,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 4, '%foo%' => 'bar'),
'transChoiceNumber' => 4,
),
);
$expectedMessages = array(
array(
'id' => 'foo',
'translation' => 'foo (en)',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'count' => 1,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'count' => 1,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'count' => 3,
'parameters' => array(
array('%count%' => 3),
array('%count%' => 3),
array('%count%' => 4, '%foo%' => 'bar'),
),
'transChoiceNumber' => 3,
),
);
$translator = $this->getTranslator();
$translator->expects($this->any())->method('getCollectedMessages')->will($this->returnValue($collectedMessages));
$dataCollector = new TranslationDataCollector($translator);
$dataCollector->lateCollect();
$this->assertEquals(1, $dataCollector->getCountMissings());
$this->assertEquals(1, $dataCollector->getCountFallbacks());
$this->assertEquals(1, $dataCollector->getCountDefines());
$this->assertEquals($expectedMessages, array_values($dataCollector->getMessages()->getValue(true)));
}
private function getTranslator()
{
$translator = $this
->getMockBuilder('Symfony\Component\Translation\DataCollectorTranslator')
->disableOriginalConstructor()
->getMock()
;
return $translator;
}
}

View File

@@ -1,116 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
class DataCollectorTranslatorTest extends TestCase
{
public function testCollectMessages()
{
$collector = $this->createCollector();
$collector->setFallbackLocales(array('fr', 'ru'));
$collector->trans('foo');
$collector->trans('bar');
$collector->trans('choice', array('%count%' => 0));
$collector->trans('bar_ru');
$collector->trans('bar_ru', array('foo' => 'bar'));
$expectedMessages = array();
$expectedMessages[] = array(
'id' => 'foo',
'translation' => 'foo (en)',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 0),
'transChoiceNumber' => 0,
);
$expectedMessages[] = array(
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array('foo' => 'bar'),
'transChoiceNumber' => null,
);
$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
}
/**
* @group legacy
*/
public function testCollectMessagesTransChoice()
{
$collector = $this->createCollector();
$collector->setFallbackLocales(array('fr', 'ru'));
$collector->transChoice('choice', 0);
$expectedMessages = array();
$expectedMessages[] = array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 0),
'transChoiceNumber' => 0,
);
$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
}
private function createCollector()
{
$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('foo' => 'foo (en)'), 'en');
$translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
$translator->addResource('array', array('bar_ru' => 'bar (ru)'), 'ru');
return new DataCollectorTranslator($translator);
}
}

View File

@@ -1,48 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
class TranslationDumperPassTest extends TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$writerDefinition = $container->register('translation.writer');
$container->register('foo.id')
->addTag('translation.dumper', array('alias' => 'bar.alias'));
$translationDumperPass = new TranslationDumperPass();
$translationDumperPass->process($container);
$this->assertEquals(array(array('addDumper', array('bar.alias', new Reference('foo.id')))), $writerDefinition->getMethodCalls());
}
public function testProcessNoDefinitionFound()
{
$container = new ContainerBuilder();
$definitionsBefore = \count($container->getDefinitions());
$aliasesBefore = \count($container->getAliases());
$translationDumperPass = new TranslationDumperPass();
$translationDumperPass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
}

View File

@@ -1,66 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
class TranslationExtractorPassTest extends TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$extractorDefinition = $container->register('translation.extractor');
$container->register('foo.id')
->addTag('translation.extractor', array('alias' => 'bar.alias'));
$translationDumperPass = new TranslationExtractorPass();
$translationDumperPass->process($container);
$this->assertEquals(array(array('addExtractor', array('bar.alias', new Reference('foo.id')))), $extractorDefinition->getMethodCalls());
}
public function testProcessNoDefinitionFound()
{
$container = new ContainerBuilder();
$definitionsBefore = \count($container->getDefinitions());
$aliasesBefore = \count($container->getAliases());
$translationDumperPass = new TranslationExtractorPass();
$translationDumperPass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The alias for the tag "translation.extractor" of service "foo.id" must be set.
*/
public function testProcessMissingAlias()
{
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock();
$container = new ContainerBuilder();
$container->register('translation.extractor');
$container->register('foo.id')
->addTag('translation.extractor', array());
$definition->expects($this->never())->method('addMethodCall');
$translationDumperPass = new TranslationExtractorPass();
$translationDumperPass->process($container);
}
}

View File

@@ -1,57 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
class TranslationPassTest extends TestCase
{
public function testValidCollector()
{
$loader = (new Definition())
->addTag('translation.loader', array('alias' => 'xliff', 'legacy-alias' => 'xlf'));
$reader = new Definition();
$translator = (new Definition())
->setArguments(array(null, null, null, null));
$container = new ContainerBuilder();
$container->setDefinition('translator.default', $translator);
$container->setDefinition('translation.reader', $reader);
$container->setDefinition('translation.xliff_loader', $loader);
$pass = new TranslatorPass('translator.default', 'translation.reader');
$pass->process($container);
$expectedReader = (new Definition())
->addMethodCall('addLoader', array('xliff', new Reference('translation.xliff_loader')))
->addMethodCall('addLoader', array('xlf', new Reference('translation.xliff_loader')))
;
$this->assertEquals($expectedReader, $reader);
$expectedLoader = (new Definition())
->addTag('translation.loader', array('alias' => 'xliff', 'legacy-alias' => 'xlf'))
;
$this->assertEquals($expectedLoader, $loader);
$this->assertSame(array('translation.xliff_loader' => array('xliff', 'xlf')), $translator->getArgument(3));
$expected = array('translation.xliff_loader' => new ServiceClosureArgument(new Reference('translation.xliff_loader')));
$this->assertEquals($expected, $container->getDefinition((string) $translator->getArgument(0))->getArgument(0));
}
}

View File

@@ -1,30 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\CsvFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class CsvFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar', 'bar' => 'foo
foo', 'foo;foo' => 'bar'));
$dumper = new CsvFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/valid.csv', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@@ -1,90 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\FileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class FileDumperTest extends TestCase
{
public function testDump()
{
$tempDir = sys_get_temp_dir();
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new ConcreteFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertFileExists($tempDir.'/messages.en.concrete');
@unlink($tempDir.'/messages.en.concrete');
}
public function testDumpIntl()
{
$tempDir = sys_get_temp_dir();
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'), 'd1');
$catalogue->add(array('bar' => 'foo'), 'd1+intl-icu');
$catalogue->add(array('bar' => 'foo'), 'd2+intl-icu');
$dumper = new ConcreteFileDumper();
@unlink($tempDir.'/d2.en.concrete');
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertStringEqualsFile($tempDir.'/d1.en.concrete', 'foo=bar');
@unlink($tempDir.'/d1.en.concrete');
$this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo');
@unlink($tempDir.'/d1+intl-icu.en.concrete');
$this->assertFileNotExists($tempDir.'/d2.en.concrete');
$this->assertStringEqualsFile($tempDir.'/d2+intl-icu.en.concrete', 'bar=foo');
@unlink($tempDir.'/d2+intl-icu.en.concrete');
}
public function testDumpCreatesNestedDirectoriesAndFile()
{
$tempDir = sys_get_temp_dir();
$translationsDir = $tempDir.'/test/translations';
$file = $translationsDir.'/messages.en.concrete';
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new ConcreteFileDumper();
$dumper->setRelativePathTemplate('test/translations/%domain%.%locale%.%extension%');
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertFileExists($file);
@unlink($file);
@rmdir($translationsDir);
}
}
class ConcreteFileDumper extends FileDumper
{
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
return http_build_query($messages->all($domain), '', '&');
}
protected function getExtension()
{
return 'concrete';
}
}

View File

@@ -1,29 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\IcuResFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class IcuResFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new IcuResFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resourcebundle/res/en.res', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@@ -1,29 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\IniFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class IniFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new IniFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.ini', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@@ -1,39 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\JsonFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class JsonFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new JsonFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.json', $dumper->formatCatalogue($catalogue, 'messages'));
}
public function testDumpWithCustomEncoding()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => '"bar"'));
$dumper = new JsonFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.dump.json', $dumper->formatCatalogue($catalogue, 'messages', array('json_encoding' => JSON_HEX_QUOT)));
}
}

View File

@@ -1,29 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\MoFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class MoFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new MoFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.mo', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@@ -1,29 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\PhpFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class PhpFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new PhpFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.php', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@@ -1,29 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\PoFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class PoFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new PoFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.po', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@@ -1,29 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\QtFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class QtFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'), 'resources');
$dumper = new QtFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.ts', $dumper->formatCatalogue($catalogue, 'resources'));
}
}

View File

@@ -1,115 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\XliffFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class XliffFileDumperTest extends TestCase
{
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
'foo' => 'bar',
'key' => '',
'key.with.cdata' => '<source> & <target>',
));
$catalogue->setMetadata('foo', array('notes' => array(array('priority' => 1, 'from' => 'bar', 'content' => 'baz'))));
$catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux'))));
$dumper = new XliffFileDumper();
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-clean.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR'))
);
}
public function testFormatCatalogueXliff2()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
'foo' => 'bar',
'key' => '',
'key.with.cdata' => '<source> & <target>',
));
$catalogue->setMetadata('key', array('target-attributes' => array('order' => 1)));
$dumper = new XliffFileDumper();
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-2.0-clean.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR', 'xliff_version' => '2.0'))
);
}
public function testFormatCatalogueWithCustomToolInfo()
{
$options = array(
'default_locale' => 'en_US',
'tool_info' => array('tool-id' => 'foo', 'tool-name' => 'foo', 'tool-version' => '0.0', 'tool-company' => 'Foo'),
);
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array('foo' => 'bar'));
$dumper = new XliffFileDumper();
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-tool-info.xlf',
$dumper->formatCatalogue($catalogue, 'messages', $options)
);
}
public function testFormatCatalogueWithTargetAttributesMetadata()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
'foo' => 'bar',
));
$catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
$dumper = new XliffFileDumper();
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-target-attributes.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR'))
);
}
public function testFormatCatalogueWithNotesMetadata()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
'foo' => 'bar',
'baz' => 'biz',
));
$catalogue->setMetadata('foo', array('notes' => array(
array('category' => 'state', 'content' => 'new'),
array('category' => 'approved', 'content' => 'true'),
array('category' => 'section', 'content' => 'user login', 'priority' => '1'),
)));
$catalogue->setMetadata('baz', array('notes' => array(
array('id' => 'x', 'content' => 'x_content'),
array('appliesTo' => 'target', 'category' => 'quality', 'content' => 'Fuzzy'),
)));
$dumper = new XliffFileDumper();
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-notes-meta.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR', 'xliff_version' => '2.0'))
);
}
}

View File

@@ -1,47 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\YamlFileDumper;
use Symfony\Component\Translation\MessageCatalogue;
class YamlFileDumperTest extends TestCase
{
public function testTreeFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(
array(
'foo.bar1' => 'value1',
'foo.bar2' => 'value2',
));
$dumper = new YamlFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/messages.yml', $dumper->formatCatalogue($catalogue, 'messages', array('as_tree' => true, 'inline' => 999)));
}
public function testLinearFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(
array(
'foo.bar1' => 'value1',
'foo.bar2' => 'value2',
));
$dumper = new YamlFileDumper();
$this->assertStringEqualsFile(__DIR__.'/../fixtures/messages_linear.yml', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@@ -1,95 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Extractor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Extractor\PhpExtractor;
use Symfony\Component\Translation\MessageCatalogue;
class PhpExtractorTest extends TestCase
{
/**
* @dataProvider resourcesProvider
*
* @param array|string $resource
*/
public function testExtraction($resource)
{
// Arrange
$extractor = new PhpExtractor();
$extractor->setPrefix('prefix');
$catalogue = new MessageCatalogue('en');
// Act
$extractor->extract($resource, $catalogue);
$expectedHeredoc = <<<EOF
heredoc key with whitespace and escaped \$\n sequences
EOF;
$expectedNowdoc = <<<'EOF'
nowdoc key with whitespace and nonescaped \$\n sequences
EOF;
// Assert
$expectedCatalogue = array(
'messages' => array(
'single-quoted key' => 'prefixsingle-quoted key',
'double-quoted key' => 'prefixdouble-quoted key',
'heredoc key' => 'prefixheredoc key',
'nowdoc key' => 'prefixnowdoc key',
"double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
$expectedHeredoc => 'prefix'.$expectedHeredoc,
$expectedNowdoc => 'prefix'.$expectedNowdoc,
'{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples' => 'prefix{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
),
'not_messages' => array(
'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
'other-domain-test-params-long-array' => 'prefixother-domain-test-params-long-array',
'other-domain-test-trans-choice-short-array-%count%' => 'prefixother-domain-test-trans-choice-short-array-%count%',
'other-domain-test-trans-choice-long-array-%count%' => 'prefixother-domain-test-trans-choice-long-array-%count%',
'typecast' => 'prefixtypecast',
'msg1' => 'prefixmsg1',
'msg2' => 'prefixmsg2',
),
);
$actualCatalogue = $catalogue->all();
$this->assertEquals($expectedCatalogue, $actualCatalogue);
}
public function resourcesProvider()
{
$directory = __DIR__.'/../fixtures/extractor/';
$splFiles = array();
foreach (new \DirectoryIterator($directory) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
if ('translation.html.php' === $fileInfo->getBasename()) {
$phpFile = $fileInfo->getPathname();
}
$splFiles[] = $fileInfo->getFileInfo();
}
return array(
array($directory),
array($phpFile),
array(glob($directory.'*')),
array($splFiles),
array(new \ArrayObject(glob($directory.'*'))),
array(new \ArrayObject($splFiles)),
);
}
}

View File

@@ -1,96 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Formatter;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\Formatter\IntlFormatter;
use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
/**
* @requires extension intl
*/
class IntlFormatterTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider provideDataForFormat
*/
public function testFormat($expected, $message, $arguments)
{
$this->assertEquals($expected, trim((new IntlFormatter())->formatIntl($message, 'en', $arguments)));
}
public function testInvalidFormat()
{
$this->expectException(InvalidArgumentException::class);
(new IntlFormatter())->formatIntl('{foo', 'en', array(2));
}
public function testFormatWithNamedArguments()
{
if (version_compare(INTL_ICU_VERSION, '4.8', '<')) {
$this->markTestSkipped('Format with named arguments can only be run with ICU 4.8 or higher and PHP >= 5.5');
}
$chooseMessage = <<<'_MSG_'
{gender_of_host, select,
female {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to her party.}
=2 {{host} invites {guest} and one other person to her party.}
other {{host} invites {guest} as one of the # people invited to her party.}}}
male {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to his party.}
=2 {{host} invites {guest} and one other person to his party.}
other {{host} invites {guest} as one of the # people invited to his party.}}}
other {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to their party.}
=2 {{host} invites {guest} and one other person to their party.}
other {{host} invites {guest} as one of the # people invited to their party.}}}}
_MSG_;
$message = (new IntlFormatter())->formatIntl($chooseMessage, 'en', array(
'gender_of_host' => 'male',
'num_guests' => 10,
'host' => 'Fabien',
'guest' => 'Guilherme',
));
$this->assertEquals('Fabien invites Guilherme as one of the 9 people invited to his party.', $message);
}
public function provideDataForFormat()
{
return array(
array(
'There is one apple',
'There is one apple',
array(),
),
array(
'4,560 monkeys on 123 trees make 37.073 monkeys per tree',
'{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree',
array(4560, 123, 4560 / 123),
),
);
}
public function testPercentsAndBracketsAreTrimmed()
{
$formatter = new IntlFormatter();
$this->assertInstanceof(IntlFormatterInterface::class, $formatter);
$this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', array('name' => 'Fab')));
$this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', array('%name%' => 'Fab')));
$this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', array('{{ name }}' => 'Fab')));
}
}

View File

@@ -1,83 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Formatter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Formatter\MessageFormatter;
class MessageFormatterTest extends TestCase
{
/**
* @dataProvider getTransMessages
*/
public function testFormat($expected, $message, $parameters = array())
{
$this->assertEquals($expected, $this->getMessageFormatter()->format($message, 'en', $parameters));
}
/**
* @dataProvider getTransChoiceMessages
* @group legacy
*/
public function testFormatPlural($expected, $message, $number, $parameters)
{
$this->assertEquals($expected, $this->getMessageFormatter()->choiceFormat($message, $number, 'fr', $parameters));
}
public function getTransMessages()
{
return array(
array(
'There is one apple',
'There is one apple',
),
array(
'There are 5 apples',
'There are %count% apples',
array('%count%' => 5),
),
array(
'There are 5 apples',
'There are {{count}} apples',
array('{{count}}' => 5),
),
);
}
public function getTransChoiceMessages()
{
return array(
array('Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0)),
array('Il y a 1 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 1, array('%count%' => 1)),
array('Il y a 10 pommes', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 10, array('%count%' => 10)),
array('Il y a 0 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 0, array('%count%' => 0)),
array('Il y a 1 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 1, array('%count%' => 1)),
array('Il y a 10 pommes', 'Il y a %count% pomme|Il y a %count% pommes', 10, array('%count%' => 10)),
array('Il y a 0 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0)),
array('Il y a 1 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1)),
array('Il y a 10 pommes', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10)),
array('Il n\'y a aucune pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0)),
array('Il y a 1 pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1)),
array('Il y a 10 pommes', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10)),
array('Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0)),
);
}
private function getMessageFormatter()
{
return new MessageFormatter();
}
}

View File

@@ -1,23 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Contracts\Tests\Translation\TranslatorTest;
class IdentityTranslatorTest extends TranslatorTest
{
public function getTranslator()
{
return new IdentityTranslator();
}
}

View File

@@ -1,52 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Interval;
/**
* @group legacy
*/
class IntervalTest extends TestCase
{
/**
* @dataProvider getTests
*/
public function testTest($expected, $number, $interval)
{
$this->assertEquals($expected, Interval::test($number, $interval));
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
*/
public function testTestException()
{
Interval::test(1, 'foobar');
}
public function getTests()
{
return array(
array(true, 3, '{1,2, 3 ,4}'),
array(false, 10, '{1,2, 3 ,4}'),
array(false, 3, '[1,2]'),
array(true, 1, '[1,2]'),
array(true, 2, '[1,2]'),
array(false, 1, ']1,2['),
array(false, 2, ']1,2['),
array(true, log(0), '[-Inf,2['),
array(true, -log(0), '[-2,+Inf]'),
);
}
}

View File

@@ -1,61 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\CsvFileLoader;
class CsvFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new CsvFileLoader();
$resource = __DIR__.'/../fixtures/resources.csv';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadDoesNothingIfEmpty()
{
$loader = new CsvFileLoader();
$resource = __DIR__.'/../fixtures/empty.csv';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array(), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new CsvFileLoader();
$resource = __DIR__.'/../fixtures/not-exists.csv';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadNonLocalResource()
{
$loader = new CsvFileLoader();
$resource = 'http://example.com/resources.csv';
$loader->load($resource, 'en', 'domain1');
}
}

View File

@@ -1,64 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\IcuDatFileLoader;
/**
* @requires extension intl
*/
class IcuDatFileLoaderTest extends LocalizedTestCase
{
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadInvalidResource()
{
$loader = new IcuDatFileLoader();
$loader->load(__DIR__.'/../fixtures/resourcebundle/corrupted/resources', 'es', 'domain2');
}
public function testDatEnglishLoad()
{
// bundled resource is build using pkgdata command which at least in ICU 4.2 comes in extremely! buggy form
// you must specify an temporary build directory which is not the same as current directory and
// MUST reside on the same partition. pkgdata -p resources -T /srv -d.packagelist.txt
$loader = new IcuDatFileLoader();
$resource = __DIR__.'/../fixtures/resourcebundle/dat/resources';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('symfony' => 'Symfony 2 is great'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource.'.dat')), $catalogue->getResources());
}
public function testDatFrenchLoad()
{
$loader = new IcuDatFileLoader();
$resource = __DIR__.'/../fixtures/resourcebundle/dat/resources';
$catalogue = $loader->load($resource, 'fr', 'domain1');
$this->assertEquals(array('symfony' => 'Symfony 2 est génial'), $catalogue->all('domain1'));
$this->assertEquals('fr', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource.'.dat')), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new IcuDatFileLoader();
$loader->load(__DIR__.'/../fixtures/non-existing.txt', 'en', 'domain1');
}
}

View File

@@ -1,51 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Translation\Loader\IcuResFileLoader;
/**
* @requires extension intl
*/
class IcuResFileLoaderTest extends LocalizedTestCase
{
public function testLoad()
{
// resource is build using genrb command
$loader = new IcuResFileLoader();
$resource = __DIR__.'/../fixtures/resourcebundle/res';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new DirectoryResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new IcuResFileLoader();
$loader->load(__DIR__.'/../fixtures/non-existing.txt', 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadInvalidResource()
{
$loader = new IcuResFileLoader();
$loader->load(__DIR__.'/../fixtures/resourcebundle/corrupted', 'en', 'domain1');
}
}

View File

@@ -1,51 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\IniFileLoader;
class IniFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new IniFileLoader();
$resource = __DIR__.'/../fixtures/resources.ini';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadDoesNothingIfEmpty()
{
$loader = new IniFileLoader();
$resource = __DIR__.'/../fixtures/empty.ini';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array(), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new IniFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.ini';
$loader->load($resource, 'en', 'domain1');
}
}

View File

@@ -1,62 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\JsonFileLoader;
class JsonFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new JsonFileLoader();
$resource = __DIR__.'/../fixtures/resources.json';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadDoesNothingIfEmpty()
{
$loader = new JsonFileLoader();
$resource = __DIR__.'/../fixtures/empty.json';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array(), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new JsonFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.json';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
* @expectedExceptionMessage Error parsing JSON - Syntax error, malformed JSON
*/
public function testParseException()
{
$loader = new JsonFileLoader();
$resource = __DIR__.'/../fixtures/malformed.json';
$loader->load($resource, 'en', 'domain1');
}
}

View File

@@ -1,24 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
abstract class LocalizedTestCase extends TestCase
{
protected function setUp()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('Extension intl is required.');
}
}
}

View File

@@ -1,72 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\MoFileLoader;
class MoFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/resources.mo';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadPlurals()
{
$loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/plurals.mo';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar', 'foos' => '{0} bar|{1} bars'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.mo';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadInvalidResource()
{
$loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/empty.mo';
$loader->load($resource, 'en', 'domain1');
}
public function testLoadEmptyTranslation()
{
$loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/empty-translation.mo';
$catalogue = $loader->load($resource, 'en', 'message');
$this->assertEquals(array(), $catalogue->all('message'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
}

View File

@@ -1,50 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\PhpFileLoader;
class PhpFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new PhpFileLoader();
$resource = __DIR__.'/../fixtures/resources.php';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new PhpFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.php';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadThrowsAnExceptionIfFileNotLocal()
{
$loader = new PhpFileLoader();
$resource = 'http://example.com/resources.php';
$loader->load($resource, 'en', 'domain1');
}
}

View File

@@ -1,109 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\PoFileLoader;
class PoFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/resources.po';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadPlurals()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/plurals.po';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar', 'foos' => 'bar|bars'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadDoesNothingIfEmpty()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/empty.po';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array(), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.po';
$loader->load($resource, 'en', 'domain1');
}
public function testLoadEmptyTranslation()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/empty-translation.po';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => ''), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testEscapedId()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/escaped-id.po';
$catalogue = $loader->load($resource, 'en', 'domain1');
$messages = $catalogue->all('domain1');
$this->assertArrayHasKey('escaped "foo"', $messages);
$this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
}
public function testEscapedIdPlurals()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/escaped-id-plurals.po';
$catalogue = $loader->load($resource, 'en', 'domain1');
$messages = $catalogue->all('domain1');
$this->assertArrayHasKey('escaped "foo"', $messages);
$this->assertArrayHasKey('escaped "foos"', $messages);
$this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
$this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foos"']);
}
public function testSkipFuzzyTranslations()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/fuzzy-translations.po';
$catalogue = $loader->load($resource, 'en', 'domain1');
$messages = $catalogue->all('domain1');
$this->assertArrayHasKey('foo1', $messages);
$this->assertArrayNotHasKey('foo2', $messages);
$this->assertArrayHasKey('foo3', $messages);
}
}

View File

@@ -1,75 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\QtFileLoader;
class QtFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new QtFileLoader();
$resource = __DIR__.'/../fixtures/resources.ts';
$catalogue = $loader->load($resource, 'en', 'resources');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('resources'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new QtFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.ts';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadNonLocalResource()
{
$loader = new QtFileLoader();
$resource = 'http://domain1.com/resources.ts';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadInvalidResource()
{
$loader = new QtFileLoader();
$resource = __DIR__.'/../fixtures/invalid-xml-resources.xlf';
$loader->load($resource, 'en', 'domain1');
}
public function testLoadEmptyResource()
{
$loader = new QtFileLoader();
$resource = __DIR__.'/../fixtures/empty.xlf';
if (method_exists($this, 'expectException')) {
$this->expectException('Symfony\Component\Translation\Exception\InvalidResourceException');
$this->expectExceptionMessage(sprintf('Unable to load "%s".', $resource));
} else {
$this->setExpectedException('Symfony\Component\Translation\Exception\InvalidResourceException', sprintf('Unable to load "%s".', $resource));
}
$loader->load($resource, 'en', 'domain1');
}
}

View File

@@ -1,260 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\XliffFileLoader;
class XliffFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
$this->assertSame(array(), libxml_get_errors());
$this->assertContainsOnly('string', $catalogue->all('domain1'));
}
public function testLoadWithInternalErrorsEnabled()
{
$internalErrors = libxml_use_internal_errors(true);
$this->assertSame(array(), libxml_get_errors());
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
$this->assertSame(array(), libxml_get_errors());
libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
}
public function testLoadWithExternalEntitiesDisabled()
{
$disableEntities = libxml_disable_entity_loader(true);
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');
libxml_disable_entity_loader($disableEntities);
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadWithResname()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo', 'qux' => 'qux source'), $catalogue->all('domain1'));
}
public function testIncompleteResource()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar', 'extra' => 'extra', 'key' => '', 'test' => 'with'), $catalogue->all('domain1'));
}
public function testEncoding()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1');
$this->assertEquals(utf8_decode('föö'), $catalogue->get('bar', 'domain1'));
$this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
$this->assertEquals(array('notes' => array(array('content' => utf8_decode('bäz'))), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
}
public function testTargetAttributesAreStoredCorrectly()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/with-attributes.xlf', 'en', 'domain1');
$metadata = $catalogue->getMetadata('foo', 'domain1');
$this->assertEquals('translated', $metadata['target-attributes']['state']);
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadInvalidResource()
{
$loader = new XliffFileLoader();
$loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadResourceDoesNotValidate()
{
$loader = new XliffFileLoader();
$loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.xlf';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadThrowsAnExceptionIfFileNotLocal()
{
$loader = new XliffFileLoader();
$resource = 'http://example.com/resources.xlf';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
* @expectedExceptionMessage Document types are not allowed.
*/
public function testDocTypeIsNotAllowed()
{
$loader = new XliffFileLoader();
$loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
}
public function testParseEmptyFile()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/empty.xlf';
if (method_exists($this, 'expectException')) {
$this->expectException('Symfony\Component\Translation\Exception\InvalidResourceException');
$this->expectExceptionMessage(sprintf('Unable to load "%s":', $resource));
} else {
$this->setExpectedException('Symfony\Component\Translation\Exception\InvalidResourceException', sprintf('Unable to load "%s":', $resource));
}
$loader->load($resource, 'en', 'domain1');
}
public function testLoadNotes()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/withnote.xlf', 'en', 'domain1');
$this->assertEquals(array('notes' => array(array('priority' => 1, 'content' => 'foo')), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
// message without target
$this->assertEquals(array('notes' => array(array('content' => 'bar', 'from' => 'foo')), 'id' => '2'), $catalogue->getMetadata('extra', 'domain1'));
// message with empty target
$this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux')), 'id' => '123'), $catalogue->getMetadata('key', 'domain1'));
}
public function testLoadVersion2()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources-2.0.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
$this->assertSame(array(), libxml_get_errors());
$domains = $catalogue->all();
$this->assertCount(3, $domains['domain1']);
$this->assertContainsOnly('string', $catalogue->all('domain1'));
// target attributes
$this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1'));
}
public function testLoadVersion2WithNoteMeta()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources-notes-meta.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
$this->assertSame(array(), libxml_get_errors());
// test for "foo" metadata
$this->assertTrue($catalogue->defines('foo', 'domain1'));
$metadata = $catalogue->getMetadata('foo', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(3, $metadata['notes']);
$this->assertEquals('state', $metadata['notes'][0]['category']);
$this->assertEquals('new', $metadata['notes'][0]['content']);
$this->assertEquals('approved', $metadata['notes'][1]['category']);
$this->assertEquals('true', $metadata['notes'][1]['content']);
$this->assertEquals('section', $metadata['notes'][2]['category']);
$this->assertEquals('1', $metadata['notes'][2]['priority']);
$this->assertEquals('user login', $metadata['notes'][2]['content']);
// test for "baz" metadata
$this->assertTrue($catalogue->defines('baz', 'domain1'));
$metadata = $catalogue->getMetadata('baz', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(2, $metadata['notes']);
$this->assertEquals('x', $metadata['notes'][0]['id']);
$this->assertEquals('x_content', $metadata['notes'][0]['content']);
$this->assertEquals('target', $metadata['notes'][1]['appliesTo']);
$this->assertEquals('quality', $metadata['notes'][1]['category']);
$this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
}
public function testLoadVersion2WithMultiSegmentUnit()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources-2.0-multi-segment-unit.xlf';
$catalog = $loader->load($resource, 'en', 'domain1');
$this->assertSame('en', $catalog->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalog->getResources());
$this->assertFalse(libxml_get_last_error());
// test for "foo" metadata
$this->assertTrue($catalog->defines('foo', 'domain1'));
$metadata = $catalog->getMetadata('foo', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(1, $metadata['notes']);
$this->assertSame('processed', $metadata['notes'][0]['category']);
$this->assertSame('true', $metadata['notes'][0]['content']);
// test for "bar" metadata
$this->assertTrue($catalog->defines('bar', 'domain1'));
$metadata = $catalog->getMetadata('bar', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(1, $metadata['notes']);
$this->assertSame('processed', $metadata['notes'][0]['category']);
$this->assertSame('true', $metadata['notes'][0]['content']);
}
}

View File

@@ -1,71 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\YamlFileLoader;
class YamlFileLoaderTest extends TestCase
{
public function testLoad()
{
$loader = new YamlFileLoader();
$resource = __DIR__.'/../fixtures/resources.yml';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadDoesNothingIfEmpty()
{
$loader = new YamlFileLoader();
$resource = __DIR__.'/../fixtures/empty.yml';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array(), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
*/
public function testLoadNonExistingResource()
{
$loader = new YamlFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.yml';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadThrowsAnExceptionIfFileNotLocal()
{
$loader = new YamlFileLoader();
$resource = 'http://example.com/resources.yml';
$loader->load($resource, 'en', 'domain1');
}
/**
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
*/
public function testLoadThrowsAnExceptionIfNotAnArray()
{
$loader = new YamlFileLoader();
$resource = __DIR__.'/../fixtures/non-valid.yml';
$loader->load($resource, 'en', 'domain1');
}
}

View File

@@ -1,68 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\LoggingTranslator;
use Symfony\Component\Translation\Translator;
class LoggingTranslatorTest extends TestCase
{
public function testTransWithNoTranslationIsLogged()
{
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger->expects($this->exactly(1))
->method('warning')
->with('Translation not found.')
;
$translator = new Translator('ar');
$loggableTranslator = new LoggingTranslator($translator, $logger);
$loggableTranslator->trans('bar');
}
/**
* @group legacy
*/
public function testTransChoiceFallbackIsLogged()
{
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger->expects($this->once())
->method('debug')
->with('Translation use fallback catalogue.')
;
$translator = new Translator('ar');
$translator->setFallbackLocales(array('en'));
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
$loggableTranslator = new LoggingTranslator($translator, $logger);
$loggableTranslator->transChoice('some_message2', 10, array('%count%' => 10));
}
/**
* @group legacy
*/
public function testTransChoiceWithNoTranslationIsLogged()
{
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$logger->expects($this->exactly(1))
->method('warning')
->with('Translation not found.')
;
$translator = new Translator('ar');
$loggableTranslator = new LoggingTranslator($translator, $logger);
$loggableTranslator->transChoice('some_message2', 10, array('%count%' => 10));
}
}

View File

@@ -1,243 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\MessageCatalogue;
class MessageCatalogueTest extends TestCase
{
public function testGetLocale()
{
$catalogue = new MessageCatalogue('en');
$this->assertEquals('en', $catalogue->getLocale());
}
public function testGetDomains()
{
$catalogue = new MessageCatalogue('en', array('domain1' => array(), 'domain2' => array(), 'domain2+intl-icu' => array(), 'domain3+intl-icu' => array()));
$this->assertEquals(array('domain1', 'domain2', 'domain3'), $catalogue->getDomains());
}
public function testAll()
{
$catalogue = new MessageCatalogue('en', $messages = array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
$this->assertEquals(array('foo' => 'foo'), $catalogue->all('domain1'));
$this->assertEquals(array(), $catalogue->all('domain88'));
$this->assertEquals($messages, $catalogue->all());
$messages = array('domain1+intl-icu' => array('foo' => 'bar')) + $messages + array(
'domain2+intl-icu' => array('bar' => 'foo'),
'domain3+intl-icu' => array('biz' => 'biz'),
);
$catalogue = new MessageCatalogue('en', $messages);
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals(array('bar' => 'foo'), $catalogue->all('domain2'));
$this->assertEquals(array('biz' => 'biz'), $catalogue->all('domain3'));
$messages = array(
'domain1' => array('foo' => 'bar'),
'domain2' => array('bar' => 'foo'),
'domain3' => array('biz' => 'biz'),
);
$this->assertEquals($messages, $catalogue->all());
}
public function testHas()
{
$catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2+intl-icu' => array('bar' => 'bar')));
$this->assertTrue($catalogue->has('foo', 'domain1'));
$this->assertTrue($catalogue->has('bar', 'domain2'));
$this->assertFalse($catalogue->has('bar', 'domain1'));
$this->assertFalse($catalogue->has('foo', 'domain88'));
}
public function testGetSet()
{
$catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar'), 'domain2+intl-icu' => array('bar' => 'foo')));
$catalogue->set('foo1', 'foo1', 'domain1');
$this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
$this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
$this->assertEquals('foo', $catalogue->get('bar', 'domain2'));
}
public function testAdd()
{
$catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
$catalogue->add(array('foo1' => 'foo1'), 'domain1');
$this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
$this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
$catalogue->add(array('foo' => 'bar'), 'domain1');
$this->assertEquals('bar', $catalogue->get('foo', 'domain1'));
$this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
$catalogue->add(array('foo' => 'bar'), 'domain88');
$this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
}
public function testReplace()
{
$catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain1+intl-icu' => array('bar' => 'bar')));
$catalogue->replace($messages = array('foo1' => 'foo1'), 'domain1');
$this->assertEquals($messages, $catalogue->all('domain1'));
}
public function testAddCatalogue()
{
$r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
$r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
$r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
$r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
$catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo')));
$catalogue->addResource($r);
$catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo1' => 'foo1'), 'domain2+intl-icu' => array('bar' => 'bar')));
$catalogue1->addResource($r1);
$catalogue->addCatalogue($catalogue1);
$this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
$this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
$this->assertEquals('bar', $catalogue->get('bar', 'domain2'));
$this->assertEquals('bar', $catalogue->get('bar', 'domain2+intl-icu'));
$this->assertEquals(array($r, $r1), $catalogue->getResources());
}
public function testAddFallbackCatalogue()
{
$r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
$r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
$r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
$r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
$r2 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
$r2->expects($this->any())->method('__toString')->will($this->returnValue('r2'));
$catalogue = new MessageCatalogue('fr_FR', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
$catalogue->addResource($r);
$catalogue1 = new MessageCatalogue('fr', array('domain1' => array('foo' => 'bar', 'foo1' => 'foo1')));
$catalogue1->addResource($r1);
$catalogue2 = new MessageCatalogue('en');
$catalogue2->addResource($r2);
$catalogue->addFallbackCatalogue($catalogue1);
$catalogue1->addFallbackCatalogue($catalogue2);
$this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
$this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
$this->assertEquals(array($r, $r1, $r2), $catalogue->getResources());
}
/**
* @expectedException \Symfony\Component\Translation\Exception\LogicException
*/
public function testAddFallbackCatalogueWithParentCircularReference()
{
$main = new MessageCatalogue('en_US');
$fallback = new MessageCatalogue('fr_FR');
$fallback->addFallbackCatalogue($main);
$main->addFallbackCatalogue($fallback);
}
/**
* @expectedException \Symfony\Component\Translation\Exception\LogicException
*/
public function testAddFallbackCatalogueWithFallbackCircularReference()
{
$fr = new MessageCatalogue('fr');
$en = new MessageCatalogue('en');
$es = new MessageCatalogue('es');
$fr->addFallbackCatalogue($en);
$es->addFallbackCatalogue($en);
$en->addFallbackCatalogue($fr);
}
/**
* @expectedException \Symfony\Component\Translation\Exception\LogicException
*/
public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
{
$catalogue = new MessageCatalogue('en');
$catalogue->addCatalogue(new MessageCatalogue('fr', array()));
}
public function testGetAddResource()
{
$catalogue = new MessageCatalogue('en');
$r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
$r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
$catalogue->addResource($r);
$catalogue->addResource($r);
$r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
$r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
$catalogue->addResource($r1);
$this->assertEquals(array($r, $r1), $catalogue->getResources());
}
public function testMetadataDelete()
{
$catalogue = new MessageCatalogue('en');
$this->assertEquals(array(), $catalogue->getMetadata('', ''), 'Metadata is empty');
$catalogue->deleteMetadata('key', 'messages');
$catalogue->deleteMetadata('', 'messages');
$catalogue->deleteMetadata();
}
public function testMetadataSetGetDelete()
{
$catalogue = new MessageCatalogue('en');
$catalogue->setMetadata('key', 'value');
$this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
$catalogue->setMetadata('key2', array());
$this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
$catalogue->deleteMetadata('key2', 'messages');
$this->assertNull($catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
$catalogue->deleteMetadata('key2', 'domain');
$this->assertNull($catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
}
public function testMetadataMerge()
{
$cat1 = new MessageCatalogue('en');
$cat1->setMetadata('a', 'b');
$this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
$cat2 = new MessageCatalogue('en');
$cat2->setMetadata('b', 'c', 'domain');
$this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
$cat1->addCatalogue($cat2);
$this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
}
}

View File

@@ -1,140 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\MessageSelector;
/**
* @group legacy
*/
class MessageSelectorTest extends TestCase
{
/**
* @dataProvider getChooseTests
*/
public function testChoose($expected, $id, $number)
{
$selector = new MessageSelector();
$this->assertEquals($expected, $selector->choose($id, $number, 'en'));
}
public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
{
$selector = new MessageSelector();
$this->assertEquals('There are two apples', $selector->choose('There are two apples', 2, 'en'));
}
/**
* @dataProvider getNonMatchingMessages
* @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
*/
public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
{
$selector = new MessageSelector();
$selector->choose($id, $number, 'en');
}
public function getNonMatchingMessages()
{
return array(
array('{0} There are no apples|{1} There is one apple', 2),
array('{1} There is one apple|]1,Inf] There are %count% apples', 0),
array('{1} There is one apple|]2,Inf] There are %count% apples', 2),
array('{0} There are no apples|There is one apple', 2),
);
}
public function getChooseTests()
{
return array(
array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
array('There are no apples', '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1),
array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10),
array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
array('There are %count% apples', 'There is one apple|There are %count% apples', 0),
array('There is one apple', 'There is one apple|There are %count% apples', 1),
array('There are %count% apples', 'There is one apple|There are %count% apples', 10),
array('There are %count% apples', 'one: There is one apple|more: There are %count% apples', 0),
array('There is one apple', 'one: There is one apple|more: There are %count% apples', 1),
array('There are %count% apples', 'one: There is one apple|more: There are %count% apples', 10),
array('There are no apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 0),
array('There is one apple', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 1),
array('There are %count% apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 10),
array('', '{0}|{1} There is one apple|]1,Inf] There are %count% apples', 0),
array('', '{0} There are no apples|{1}|]1,Inf] There are %count% apples', 1),
// Indexed only tests which are Gettext PoFile* compatible strings.
array('There are %count% apples', 'There is one apple|There are %count% apples', 0),
array('There is one apple', 'There is one apple|There are %count% apples', 1),
array('There are %count% apples', 'There is one apple|There are %count% apples', 2),
// Tests for float numbers
array('There is almost one apple', '{0} There are no apples|]0,1[ There is almost one apple|{1} There is one apple|[1,Inf] There is more than one apple', 0.7),
array('There is one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1),
array('There is more than one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1.7),
array('There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),
array('There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0.0),
array('There are no apples', '{0.0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),
// Test texts with new-lines
// with double-quotes and \n in id & double-quotes and actual newlines in text
array("This is a text with a\n new-line in it. Selector = 0.", '{0}This is a text with a
new-line in it. Selector = 0.|{1}This is a text with a
new-line in it. Selector = 1.|[1,Inf]This is a text with a
new-line in it. Selector > 1.', 0),
// with double-quotes and \n in id and single-quotes and actual newlines in text
array("This is a text with a\n new-line in it. Selector = 1.", '{0}This is a text with a
new-line in it. Selector = 0.|{1}This is a text with a
new-line in it. Selector = 1.|[1,Inf]This is a text with a
new-line in it. Selector > 1.', 1),
array("This is a text with a\n new-line in it. Selector > 1.", '{0}This is a text with a
new-line in it. Selector = 0.|{1}This is a text with a
new-line in it. Selector = 1.|[1,Inf]This is a text with a
new-line in it. Selector > 1.', 5),
// with double-quotes and id split accros lines
array('This is a text with a
new-line in it. Selector = 1.', '{0}This is a text with a
new-line in it. Selector = 0.|{1}This is a text with a
new-line in it. Selector = 1.|[1,Inf]This is a text with a
new-line in it. Selector > 1.', 1),
// with single-quotes and id split accros lines
array('This is a text with a
new-line in it. Selector > 1.', '{0}This is a text with a
new-line in it. Selector = 0.|{1}This is a text with a
new-line in it. Selector = 1.|[1,Inf]This is a text with a
new-line in it. Selector > 1.', 5),
// with single-quotes and \n in text
array('This is a text with a\nnew-line in it. Selector = 0.', '{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.', 0),
// with double-quotes and id split accros lines
array("This is a text with a\nnew-line in it. Selector = 1.", "{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.", 1),
// esacape pipe
array('This is a text with | in it. Selector = 0.', '{0}This is a text with || in it. Selector = 0.|{1}This is a text with || in it. Selector = 1.', 0),
// Empty plural set (2 plural forms) from a .PO file
array('', '|', 1),
// Empty plural set (3 plural forms) from a .PO file
array('', '||', 1),
);
}
}

View File

@@ -1,124 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\PluralizationRules;
/**
* Test should cover all languages mentioned on http://translate.sourceforge.net/wiki/l10n/pluralforms
* and Plural forms mentioned on http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms.
*
* See also https://developer.mozilla.org/en/Localization_and_Plurals which mentions 15 rules having a maximum of 6 forms.
* The mozilla code is also interesting to check for.
*
* As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
*
* The goal to cover all languages is to far fetched so this test case is smaller.
*
* @author Clemens Tolboom clemens@build2be.nl
*
* @group legacy
*/
class PluralizationRulesTest extends TestCase
{
/**
* We test failed langcode here.
*
* TODO: The languages mentioned in the data provide need to get fixed somehow within PluralizationRules.
*
* @dataProvider failingLangcodes
*/
public function testFailedLangcodes($nplural, $langCodes)
{
$matrix = $this->generateTestData($langCodes);
$this->validateMatrix($nplural, $matrix, false);
}
/**
* @dataProvider successLangcodes
*/
public function testLangcodes($nplural, $langCodes)
{
$matrix = $this->generateTestData($langCodes);
$this->validateMatrix($nplural, $matrix);
}
/**
* This array should contain all currently known langcodes.
*
* As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
*
* @return array
*/
public function successLangcodes()
{
return array(
array('1', array('ay', 'bo', 'cgg', 'dz', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky')),
array('2', array('nl', 'fr', 'en', 'de', 'de_GE', 'hy', 'hy_AM')),
array('3', array('be', 'bs', 'cs', 'hr')),
array('4', array('cy', 'mt', 'sl')),
array('6', array('ar')),
);
}
/**
* This array should be at least empty within the near future.
*
* This both depends on a complete list trying to add above as understanding
* the plural rules of the current failing languages.
*
* @return array with nplural together with langcodes
*/
public function failingLangcodes()
{
return array(
array('1', array('fa')),
array('2', array('jbo')),
array('3', array('cbs')),
array('4', array('gd', 'kw')),
array('5', array('ga')),
);
}
/**
* We validate only on the plural coverage. Thus the real rules is not tested.
*
* @param string $nplural Plural expected
* @param array $matrix Containing langcodes and their plural index values
* @param bool $expectSuccess
*/
protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
{
foreach ($matrix as $langCode => $data) {
$indexes = array_flip($data);
if ($expectSuccess) {
$this->assertEquals($nplural, \count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
} else {
$this->assertNotEquals((int) $nplural, \count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
}
}
}
protected function generateTestData($langCodes)
{
$matrix = array();
foreach ($langCodes as $langCode) {
for ($count = 0; $count < 200; ++$count) {
$plural = PluralizationRules::get($count, $langCode);
$matrix[$langCode][$count] = $plural;
}
}
return $matrix;
}
}

Some files were not shown because too many files have changed in this diff Show More