Initialize the catalogs for the locale(s) and domain(s)

@param  string | array $locale
@param  string | array $domain
@throws NoResources
176221
protected function initCatalogs($locale, $domain) {
		if (empty($this->resources)) {
			throw new NoResources('Resources hasn\'t been provided');
		}
		
		// Add the preinit data to the request if need
		if ($this->preinitNeed) {
			list($locale, $domain) = $this->handlePreinit($locale, $domain);
		}
		
		// Mark as initialized anyway
		$this->markAsInitialized($locale, $domain);
		
		// Try from cache
		if ($this->cache !== null) {
			$catalogsCached = $this->cache->getCatalogs($locale, $domain);
			
			if ($catalogsCached !== null) {
				$this->catalogs = array_replace_recursive($this->catalogs, $catalogsCached);
				
				// Request to the resources is still need ?
				$requestDiff = $this->calculateRequestDiff($locale, $domain, $catalogsCached);
				
				if ($requestDiff === null) {
					return;
				}
				
				list($locale, $domain) = $requestDiff;
			}
		}
		
		// Try from resources
		$catalogs = $this->getCatalogsFromResources($locale, $domain);
		
		if (! empty($catalogs)) {
			$this->catalogs = array_replace_recursive($this->catalogs, $catalogs);
			
			// Store in the cache just obtained data
			if ($this->cache !== null) {
				$this->cache->setCatalogs($catalogs);
			}
		}
	}
288874
public function getCatalogs($locale = null, $domain = null) {
		$cache = $this->getCacheFrontend();
		
		if (is_array($domain)) {
			$catalogs = $cache->getMany($domain);
			
			if (empty($catalogs)) {
				return;
			}
			
			return call_user_func_array('array_replace_recursive', $catalogs);
		}
		
		return $cache->get($domain);
	}
434376
protected function importBundlesTranslationFiles(array $locales, array $domains, $global = false)
    {
        $bundles = $this->getApplication()->getKernel()->getBundles();

        foreach ($bundles as $bundle) {
            $this->importBundleTranslationFiles($bundle, $locales, $domains, $global);
        }
    }
1235323
protected function getCatalogContent($locale, $domain) {
		$path = $this->getCatalogPath($locale, $domain);
		
		if (! is_file($path)) {
			return false;
		}
		
		if (! is_readable($path)) {
			throw new ReadError('Catalog file: "' . $path . '" exists but can\'t be read');
		}
		
		$result = file_get_contents($path);
		
		if ($result === false) {
			throw new ReadError('Error has occurred while reading the file: "' . $path . '"');
		}
		
		$this->lastFilePath = $path;
		
		return $result;
	}
621877
public function removeCatalogs($locale = null, $domain = null) {
		$cache = $this->getCacheFrontend();
		
		if (is_array($domain)) {
			return $cache->removeMany($domain);
		}
		
		return $cache->remove($domain);
	}
428229
protected function initCatalogsForManyLocales($locales, $domain) {
		foreach ($locales as $locale) {
			$this->initCatalogsForSingleLocale($locale, $domain);
		}
	}
197785
private function loadCountriesForLocale($locale)
    {
        //load from Symfony Intl component
        $regionBundle = Intl::getRegionBundle();
        $this->displayCountries[$locale] = $regionBundle->getCountryNames($locale);
    }
14505
public static function contentToCatalogue($content, $locale, $domain)
    {
        $loader = new XliffLoader();
        $catalogue = new MessageCatalogue($locale);
        $loader->extractFromContent($content, $catalogue, $domain);

        return $catalogue;
    }
722132
public function removeTranslation($locale, $domain, $key)
    {
        $unit = $this->findByDomainAndKey($domain, $key);
        $unit->deleteTranslation($locale);
        $this->persist($unit);
    }
1170004
public function setLocale($locale)
    {
        //Check if we have information for this locale
        $file = __DIR__ . '/i18n/' . $locale . '.json';
        if (file_exists($file)) {
            //Read the locale information from the file
            $meta = json_decode(file_get_contents($file), true);
            if (is_array($meta)) {
                $this->locale = $meta;

                return true;
            } else {
                throw new LocaleParseErrorException('Locale "' . $locale . '" could not be parsed correctly');
            }
        } else {
            throw new LocaleNotSupportedException('Locale not supported by this library');
        }
    }
207366
protected function initManyLocales($locale, $domain) {
		$source = $this->getCatalogContent($locale, $domain);
		
		if ($source !== false) {
			$locales = parse_ini_string($source, true);
			
			if ($locales === false) {
				throw new ParseError('Error has occurred while parsing INI file for the domain: "' . $domain . '"');
			}
			
			foreach ($locales as $locale => $catalog) {
				$this->catalogs = array_replace_recursive($this->catalogs, [
					$locale => [
						$domain => $catalog
					]
				]);
			}
		}

		if (! isset($this->initializedCatalogs[$locale])) {
			$this->initializedCatalogs[$locale] = [];
		}

		foreach (array_keys($locales) as $locale) {
			$this->initializedCatalogs[$locale][$domain] = true;
		}
	}
51182
private function log($id, $domain, $locale)
    {
        if (null === $domain) {
            $domain = 'messages';
        }

        $id = (string) $id;
        $catalogue = $this->translator->getCatalogue($locale);
        if ($catalogue->defines($id, $domain)) {
            return;
        }

        if ($catalogue->has($id, $domain)) {
            $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
        } else {
            $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
        }
    }
956499
public function setLocale(string $locale)
    {
        if (!in_array($locale, $this->getAvailableLanguages())) {
            throw new LanguageNotFoundException("Language $locale not found.");
        }

        $this->translator->setLocale($locale);
    }
1202192
public function writeToStorage($domain, $translations)
    {
        foreach ($translations as $keys) {
            foreach ($keys as $key => $transMeta) {
                $search = $this->repository->createSearch();
                $search->addQuery(new MatchQuery('key', $key));
                $results = $this->repository->findDocuments($search);
                if (count($results)) {
                    continue;
                }

                $document = new Translation();
                $document->setDomain($domain);
                $document->setKey($key);
                $document->setPath($transMeta['path']);
                $document->setFormat($transMeta['format']);
                foreach ($transMeta['messages'] as $locale => $text) {
                    $message = new Message();
                    $message->setLocale($locale);
                    $message->setMessage($text);
                    $document->addMessage($message);
                }
                $this->manager->persist($document);
            }
        }

        $this->manager->commit();
    }
460503
public function getAllByLocaleAndDomain($locale, $domain)
    {
        $results = $this->createQueryBuilder()
            ->hydrate(false)
            ->field('domain')->equals($domain)
            ->field('translations.locale')->equals($locale)
            ->sort('key', 'asc')
            ->getQuery()
            ->execute();

        $values = array();
        foreach ($results as $item) {
            $i = 0;
            $index = null;
            while ($i < $item['translations'] && null === $index) {
                if ($item['translations'][$i]['locale'] == $locale) {
                    $index = $i;
                }
                $i++;
            }
            $item['translations'] = array($item['translations'][$i - 1]);
            $values[] = $item;
        }

        return $values;
    }
976403
protected function markAsInitializedLocale($locale, $domain) {
		if (! isset($this->initializedCatalogs[$locale])) {
			$this->initializedCatalogs[$locale] = [];
		}

		if (is_string($domain)) {
			$this->initializedCatalogs[$locale][$domain] = true;
			return;
		}

		if (is_array($domain)) {
			$this->initializedCatalogs[$locale] += array_combine($domain, array_fill_keys($domain, true));
			return;
		}
	}
1343882
public function setRegion($region, $locale = null)
    {
        $region = strtolower($region);
        $locales = $this->getSupportedLocales($region);

        if (in_array($region, $this->getSupportedRegions()) && $locales !== false) {
            $this->options->set('region', $region);
        } else {
            throw new \InvalidArgumentException(sprintf('The region `%s` is not supported.', $region));
        }

        if ($locale === null) {
            $this->options->set('locale', $locales[0]);
        } else {
            if ($locales !== false && in_array($locale, $locales)) {
                $this->options->set('locale', $locale);
            } else {
                throw new \InvalidArgumentException(sprintf('The locale `%s` for region `%s` is not supported.', $locale, $region));
            }
        }
    }