Skip to content

Commit

Permalink
Improve performance by reduce loadings of Translator (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Feb 24, 2021
1 parent 3578469 commit 9a1e139
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Coduo/PHPHumanizer/Translator/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,30 @@

final class Builder
{
/**
* @var array<string, Translator>
*/
private static array $translators = [];

public static function build(string $locale) : Translator
{
$translator = new Translator($locale);
$translator->addLoader('yml', new YamlFileLoader());
if (!isset(self::$translators[$locale])) {
$translator = new Translator($locale);
$translator->addLoader('yml', new YamlFileLoader());

$iterator = new \FilesystemIterator(__DIR__ . '/../Resources/translations');
$filter = new \RegexIterator($iterator, '/[aA-zZ]+\.([a-z]{2}|[a-z]{2}\_[A-Z]{2})\.yml$/');

$iterator = new \FilesystemIterator(__DIR__ . '/../Resources/translations');
$filter = new \RegexIterator($iterator, '/[aA-zZ]+\.([a-z]{2}|[a-z]{2}\_[A-Z]{2})\.yml$/');
foreach ($filter as $file) {
/* @var $file \SplFileInfo */
$resourceName = $file->getBasename('.yml');
[$fileDomain, $fileLocale] = \explode('.', $resourceName);
$translator->addResource('yml', $file->getPathname(), $fileLocale, $fileDomain);
}

foreach ($filter as $file) {
/* @var $file \SplFileInfo */
$resourceName = $file->getBasename('.yml');
[$fileDomain, $fileLocale] = \explode('.', $resourceName);
$translator->addResource('yml', $file->getPathname(), $fileLocale, $fileDomain);
self::$translators[$locale] = $translator;
}

return $translator;
return self::$translators[$locale];
}
}

0 comments on commit 9a1e139

Please sign in to comment.