@@ -256,9 +256,15 @@ private function representationCodes(): array {
256256 }
257257 }
258258
259- // Fallback for partially initialized Locale instances that do not have a persisted id yet.
260- $ country = $ this ->getCountry () ?? new Country ($ this ->countryId );
261- $ language = $ this ->getLanguage () ?? new Language ($ this ->languageId );
259+ // Fallback for partially initialized Locale instances without triggering relation discovery.
260+ $ codes = $ this ->loadRepresentationCodesByRelationIds ();
261+
262+ if ($ codes ) {
263+ return $ codes ;
264+ }
265+
266+ $ country = new Country ($ this ->countryId );
267+ $ language = new Language ($ this ->languageId );
262268
263269 return [
264270 'language ' => (string )$ language ->code ,
@@ -295,6 +301,40 @@ private function loadRepresentationCodes(): ?array {
295301
296302 }
297303
304+ /**
305+ * Load language and country codes from relation ids when the locale has not been persisted yet.
306+ *
307+ * @return null|array{language: string, country: string}
308+ */
309+ private function loadRepresentationCodesByRelationIds (): ?array {
310+
311+ $ languageId = isset ($ this ->languageId ) ? (int )$ this ->languageId : 0 ;
312+ $ countryId = isset ($ this ->countryId ) ? (int )$ this ->countryId : 0 ;
313+
314+ if ($ languageId <= 0 or $ countryId <= 0 ) {
315+ return null ;
316+ }
317+
318+ $ query =
319+ 'SELECT l.`code` AS `language_code`, c.`code` AS `country_code`
320+ FROM `languages` AS l
321+ INNER JOIN `countries` AS c ON c.`id` = ?
322+ WHERE l.`id` = ?
323+ LIMIT 1 ' ;
324+
325+ $ row = Database::load ($ query , [$ countryId , $ languageId ], Database::OBJECT );
326+
327+ if (!$ row ) {
328+ return null ;
329+ }
330+
331+ return [
332+ 'language ' => (string )$ row ->language_code ,
333+ 'country ' => (string )$ row ->country_code ,
334+ ];
335+
336+ }
337+
298338 /**
299339 * Returns the Locale object by its representation.
300340 * @param string Locale representation (eg. en-GB).
0 commit comments