You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4 KiB

3 years ago
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Command;
  11. use Symfony\Component\Translation\MessageCatalogue;
  12. use Symfony\Component\Translation\MessageCatalogueInterface;
  13. use Symfony\Component\Translation\TranslatorBag;
  14. /**
  15. * @internal
  16. */
  17. trait TranslationTrait
  18. {
  19. private function readLocalTranslations(array $locales, array $domains, array $transPaths): TranslatorBag
  20. {
  21. $bag = new TranslatorBag();
  22. foreach ($locales as $locale) {
  23. $catalogue = new MessageCatalogue($locale);
  24. foreach ($transPaths as $path) {
  25. $this->reader->read($path, $catalogue);
  26. }
  27. if ($domains) {
  28. foreach ($domains as $domain) {
  29. $catalogue = $this->filterCatalogue($catalogue, $domain);
  30. $bag->addCatalogue($catalogue);
  31. }
  32. } else {
  33. $bag->addCatalogue($catalogue);
  34. }
  35. }
  36. return $bag;
  37. }
  38. private function filterCatalogue(MessageCatalogue $catalogue, string $domain): MessageCatalogue
  39. {
  40. $filteredCatalogue = new MessageCatalogue($catalogue->getLocale());
  41. // extract intl-icu messages only
  42. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  43. if ($intlMessages = $catalogue->all($intlDomain)) {
  44. $filteredCatalogue->add($intlMessages, $intlDomain);
  45. }
  46. // extract all messages and subtract intl-icu messages
  47. if ($messages = array_diff($catalogue->all($domain), $intlMessages)) {
  48. $filteredCatalogue->add($messages, $domain);
  49. }
  50. foreach ($catalogue->getResources() as $resource) {
  51. $filteredCatalogue->addResource($resource);
  52. }
  53. if ($metadata = $catalogue->getMetadata('', $intlDomain)) {
  54. foreach ($metadata as $k => $v) {
  55. $filteredCatalogue->setMetadata($k, $v, $intlDomain);
  56. }
  57. }
  58. if ($metadata = $catalogue->getMetadata('', $domain)) {
  59. foreach ($metadata as $k => $v) {
  60. $filteredCatalogue->setMetadata($k, $v, $domain);
  61. }
  62. }
  63. return $filteredCatalogue;
  64. }
  65. }