{"record":{"id":"8eecaece1e557da3","repo":"symfony/translation","slug":"cannot-add-a-catalogue-for-locale-s-as-the-current-locale","errorCode":null,"errorMessage":"Cannot add a catalogue for locale \"%s\" as the current locale for this catalogue is \"%s\".","messagePattern":"Cannot add a catalogue for locale \"(.+?)\" as the current locale for this catalogue is \"(.+?)\"\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"MessageCatalogue.php","lineNumber":144,"sourceCode":"    }\n\n    public function add(array $messages, string $domain = 'messages'): void\n    {\n        $altDomain = str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? substr($domain, 0, -\\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain.self::INTL_DOMAIN_SUFFIX;\n        foreach ($messages as $id => $message) {\n            unset($this->messages[$altDomain][$id]);\n            $this->messages[$domain][$id] = $message;\n        }\n\n        if ([] === ($this->messages[$altDomain] ?? null)) {\n            unset($this->messages[$altDomain]);\n        }\n    }\n\n    public function addCatalogue(MessageCatalogueInterface $catalogue): void\n    {\n        if ($catalogue->getLocale() !== $this->locale) {\n            throw new LogicException(\\sprintf('Cannot add a catalogue for locale \"%s\" as the current locale for this catalogue is \"%s\".', $catalogue->getLocale(), $this->locale));\n        }\n\n        foreach ($catalogue->all() as $domain => $messages) {\n            if ($intlMessages = $catalogue->all($domain.self::INTL_DOMAIN_SUFFIX)) {\n                $this->add($intlMessages, $domain.self::INTL_DOMAIN_SUFFIX);\n                $messages = array_diff_key($messages, $intlMessages);\n            }\n            $this->add($messages, $domain);\n        }\n\n        foreach ($catalogue->getResources() as $resource) {\n            $this->addResource($resource);\n        }\n\n        if ($catalogue instanceof MetadataAwareInterface) {\n            $metadata = $catalogue->getMetadata('', '');\n            $this->addMetadata($metadata);\n        }","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/symfony/translation/blob/ae9e8a51bc29780d63c7f9efd6005d7c875e100b/MessageCatalogue.php#L126-L162","documentation":"MessageCatalogue::addCatalogue merges another catalogue into this one, but only if both catalogues belong to the same locale. Throwing a LogicException when the incoming catalogue's locale differs prevents silently mixing translations of different languages into one catalogue.","triggerScenarios":"Calling $catalogue->addCatalogue($other) where $other->getLocale() !== $this->locale, e.g. adding a 'fr' catalogue into a 'en' catalogue.","commonSituations":"Building catalogues in a loop keyed by the wrong variable, merging cached catalogues from different locales, or constructing a catalogue with one locale and then loading a resource file for another.","solutions":["Ensure the catalogue you add was created with the same locale as the target catalogue.","Create a separate MessageCatalogue per locale and add each catalogue to its matching instance.","Check the locale argument passed to the MessageCatalogue constructor; it is often hardcoded or swapped.","If you need cross-locale behavior, use addFallbackCatalogue to link locales instead of merging."],"exampleFix":"// before\n$fr = new MessageCatalogue('fr', $messages);\n$enCatalogue->addCatalogue($fr);\n\n// after\n$en = new MessageCatalogue('en', $messages);\n$enCatalogue->addCatalogue($en);","handlingStrategy":"type-guard","validationCode":"if ($other->getLocale() !== $catalogue->getLocale()) {\n    throw new \\LogicException('Refusing to merge catalogues of different locales');\n}","typeGuard":"function canMerge(MessageCatalogue $into, MessageCatalogue $from): bool { return $into->getLocale() === $from->getLocale(); }","tryCatchPattern":"try {\n    $catalogue->addCatalogue($other);\n} catch (\\LogicException $e) {\n    // route $other to a catalogue of its own locale instead\n}","preventionTips":["Key catalogues by locale in a map and always merge into the matching entry.","Pass the same locale variable to the catalogue constructor and its resources.","Use addFallbackCatalogue for cross-locale relations, not addCatalogue."],"tags":["translation","catalogue","locale-mismatch","logic-error"],"backgroundTag":"invalid-state-transition","analyzedSha":"ae9e8a51bc29780d63c7f9efd6005d7c875e100b","analyzedAt":"2026-09-15T22:29:15.305Z","contentChangedAt":"2026-09-15T22:29:15.305Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}