Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@tomkalon
Copy link
Contributor

@tomkalon tomkalon commented Sep 11, 2025

Q A
Branch? 2.2
Bug fix? no
New feature? yes
BC breaks? no
Related tickets #17648
License MIT

This PR ensures that the default locale code (defaultLocaleCode) is always placed at the beginning of the returned array.
Other locales remain in the same order as provided by the repository.

Default locale code is defined in: config/parameters.yaml

Added UnitTest for TranslationLocaleProvider.

Summary by CodeRabbit

  • Refactor

    • Improved how available translation locales are assembled and ordered for more consistent results.
    • The configured default locale will now appear first when locales are listed, if present.
    • No changes to public APIs, settings, or user-facing configuration.
  • Documentation

    • Added an upgrade note describing the new default-locale ordering behavior.

@tomkalon tomkalon requested review from a team as code owners September 11, 2025 10:19
@coderabbitai
Copy link

coderabbitai bot commented Sep 11, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Refactors TranslationLocaleProvider::getDefinedLocalesCodes() to collect locale codes with a static, typed closure and move the configured default locale to the front of the returned array if present. Adds an upgrade note documenting this behavior. No public method signatures changed.

Changes

Cohort / File(s) Summary
Translation locale provider
src/Sylius/Component/Core/Provider/TranslationLocaleProvider.php
Change inner closure to static with an explicit return type; accumulate locale codes into a local $codes array; find and remove the configured default locale and unshift it to the front; return reordered $codes. No public API changes.
Upgrade notes
UPGRADE-2.1.md
Add a note for 2.1.6 describing that TranslationLocaleProvider will place the configured default_locale first in the returned locales array while preserving repository order for others.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Caller as Consumer
    participant Provider as TranslationLocaleProvider
    participant Config as config/parameters

    Caller->>Provider: getDefinedLocalesCodes()
    Provider->>Provider: collect locale entities\nvia static typed closure\nbuild local $codes array
    Provider->>Config: read configured default_locale
    alt default locale present in $codes
        Provider->>Provider: remove default from list
        Provider->>Provider: unshift default to front
    end
    Provider-->>Caller: return reordered $codes
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

A rabbit hops through arrays at night,
Pulls the default forward, snug and bright.
A tiny static closure, tidy and keen,
Locales aligned, the list is clean. 🐇

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title "[Feature] Prioritize default locale in TranslationLocaleProvider output" accurately and succinctly describes the primary change in this PR — ensuring the configured default locale is placed first in the array returned by TranslationLocaleProvider. It is specific, directly related to the code and UPGRADE note in the diff, and readable for a teammate scanning history. The optional "[Feature]" prefix is minor noise but does not make the title misleading.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Sep 11, 2025

❌ Preview Environment deleted from Bunnyshell

Available commands:

  • 🚀 /bns:deploy to redeploy the environment

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/Sylius/Component/Core/Provider/TranslationLocaleProvider.php (4)

32-37: Avoid casting null locale codes to empty strings.

If LocaleInterface::getCode() can be null, casting to string yields '', which may leak empty codes downstream. Filter null/empty codes explicitly.

Apply:

-        $codes = array_map(
-            static function (LocaleInterface $locale): string {
-                return (string) $locale->getCode();
-            },
-            $locales,
-        );
+        $codes = array_values(array_filter(
+            array_map(
+                static function (LocaleInterface $locale): ?string {
+                    return $locale->getCode();
+                },
+                $locales,
+            ),
+            static fn (?string $code): bool => $code !== null && $code != '',
+        ));

39-44: Optional: simplify reordering into a single expression.

A more declarative approach keeps the intent compact without changing behavior.

Apply:

-        $default = $this->defaultLocaleCode;
-        if (false !== ($key = array_search($default, $codes, true))) {
-            unset($codes[$key]);
-            array_unshift($codes, $default);
-        }
+        $default = $this->defaultLocaleCode;
+        if (in_array($default, $codes, true)) {
+            $codes = array_merge([$default], array_values(array_diff($codes, [$default])));
+        }

22-26: Mark promoted constructor properties as readonly.

This service is immutable; readonly communicates intent and prevents accidental mutation.

Apply:

-        private LocaleCollectionProviderInterface $localeRepository,
-        private string $defaultLocaleCode,
+        private readonly LocaleCollectionProviderInterface $localeRepository,
+        private readonly string $defaultLocaleCode,

28-29: Document the return shape as list.

Native types can’t express array element types; a brief PHPDoc improves static analysis.

Apply:

-    public function getDefinedLocalesCodes(): array
+    /** @return list<string> */
+    public function getDefinedLocalesCodes(): array
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c9c1a7 and 8b2d939.

📒 Files selected for processing (1)
  • src/Sylius/Component/Core/Provider/TranslationLocaleProvider.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{php,yaml,yml,xml,twig}

📄 CodeRabbit inference engine (AGENTS.md)

Use 4 spaces for indentation in PHP, YAML, XML, and Twig files

Files:

  • src/Sylius/Component/Core/Provider/TranslationLocaleProvider.php
**/*.php

📄 CodeRabbit inference engine (AGENTS.md)

**/*.php: Use modern PHP 8.2+ syntax and features
Declare strict_types=1 in all PHP files
Follow the Sylius Coding Standard
Do not use deprecated features from PHP, Symfony, or Sylius
Use final for all classes, except entities and repositories
Use readonly for immutable services and value objects
Add type declarations for all properties, arguments, and return values
Use camelCase for variables and method names
Use SCREAMING_SNAKE_CASE for constants
Use fast returns instead of unnecessary nesting
Use trailing commas in multi-line arrays and argument lists
Order array keys alphabetically where applicable
Use PHPDoc only when necessary (e.g., @var Collection)
Group class elements in order: constants, properties, constructor, public, protected, private methods
Group getter and setter methods for the same properties together
Suffix interfaces with Interface and traits with Trait
Use use statements for all non-global classes
Sort use imports alphabetically and group by type (classes, functions, constants)

Files:

  • src/Sylius/Component/Core/Provider/TranslationLocaleProvider.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Packages / Get matrix
  • GitHub Check: Frontend / Get matrix
  • GitHub Check: End-to-end tests (MariaDB) / Get matrix
  • GitHub Check: End-to-end tests (PostgreSQL) / Get matrix
  • GitHub Check: End-to-end tests (MySQL) / Get matrix
🔇 Additional comments (2)
src/Sylius/Component/Core/Provider/TranslationLocaleProvider.php (2)

32-37: LGTM: clean mapping to codes with a static, typed closure.

The mapping reads well and aligns with PHP 8.2+ style (static closure, trailing comma).


39-44: LGTM: stable reordering to prioritize default locale while preserving the rest.

Strict comparison in array_search and reindexing via array_unshift are correct here.

@tomkalon tomkalon force-pushed the bugfix/locales-sorting branch from 8b2d939 to f950067 Compare September 11, 2025 10:32
Rafikooo
Rafikooo previously approved these changes Sep 12, 2025
Copy link
Contributor

@Rafikooo Rafikooo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a good idea to include this behavioral change in the upgrade file.

@Rafikooo Rafikooo added the Feature New feature proposals. label Sep 12, 2025
@Rafikooo Rafikooo changed the title [BUGFIX] Prioritize default locale in TranslationLocaleProvider output [Feature] Prioritize default locale in TranslationLocaleProvider output Sep 12, 2025
@probot-autolabeler probot-autolabeler bot added the Maintenance CI configurations, READMEs, releases, etc. label Sep 15, 2025
@tomkalon tomkalon force-pushed the bugfix/locales-sorting branch from 1925c2b to 703aaf4 Compare September 15, 2025 09:19
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
UPGRADE-2.1.md (2)

5-7: Qualify behavior: only move default locale if present

Avoid overpromising “always”; the implementation moves it only when found. Suggested wording below.

-1. The `TranslationLocaleProvider` now ensures that the default locale (configured as `default_locale` in `config/parameters.yaml`)
-   is always placed at the beginning of the returned locales array.  
-   Other locales remain in the same order as returned by the repository.
+1. The `TranslationLocaleProvider` now places the default locale (configured as `default_locale` in `config/parameters.yaml`)
+   at the beginning of the returned locales array, if that locale is present among the available locales.
+   Other locales remain in the same order as returned by the repository.

3-3: Fix heading level per markdownlint (MD001)

Change H3 to H2 to avoid skipping a level after the H1 section title.

-### Translations
+## Translations
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f950067 and 703aaf4.

📒 Files selected for processing (1)
  • UPGRADE-2.1.md (1 hunks)
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
UPGRADE-2.1.md

3-3: Heading levels should only increment by one level at a time
Expected: h2; Actual: h3

(MD001, heading-increment)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (17)
  • GitHub Check: End-to-end tests (MariaDB) / Non-JS, PHP 8.4, Symfony ^7.2, MariaDB 11.4.7, State Machine Adapter symfony_workflow
  • GitHub Check: End-to-end tests (MariaDB) / Non-JS, PHP 8.2, Symfony ^6.4, MariaDB 10.11.13, State Machine Adapter winzou_state_machine
  • GitHub Check: End-to-end tests (MySQL) / JS with Panther, PHP 8.3, Symfony ^7.2 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Chromedriver, PHP 8.2, Symfony ^6.4 (test_cached), MySQL 8.0, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Panther, PHP 8.4, Symfony ^7.2 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Panther, PHP 8.2, Symfony ^6.4 (test_cached), MySQL 8.0, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Chromedriver, PHP 8.4, Symfony ^7.2 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / Non-JS, PHP 8.4, Symfony ^7.2 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / JS with Chromedriver, PHP 8.3, Symfony ^7.2 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / Non-JS, PHP 8.3, Symfony ^7.2 (test_cached), MySQL 8.4, Twig ^3.3
  • GitHub Check: End-to-end tests (MySQL) / Non-JS, PHP 8.2, Symfony ^6.4 (test_cached), MySQL 8.0, Twig ^3.3
  • GitHub Check: End-to-end tests (PostgreSQL) / Non-JS, PHP 8.4, Symfony ^7.2, PostgreSQL 17.5
  • GitHub Check: End-to-end tests (PostgreSQL) / Non-JS, PHP 8.2, Symfony ^6.4, PostgreSQL 15.13
  • GitHub Check: Packages / PHP 8.2, Symfony ^6.4
  • GitHub Check: Packages / PHP 8.3, Symfony ^7.2, ORM ^3.3
  • GitHub Check: Packages / PHP 8.4, Symfony ^7.2
  • GitHub Check: Frontend / NodeJS 24.x

@Rafikooo Rafikooo changed the base branch from 2.1 to 2.2 September 15, 2025 14:11
@Rafikooo Rafikooo removed the Maintenance CI configurations, READMEs, releases, etc. label Sep 17, 2025
@probot-autolabeler probot-autolabeler bot added the Maintenance CI configurations, READMEs, releases, etc. label Sep 17, 2025
@tomkalon tomkalon force-pushed the bugfix/locales-sorting branch from 52d34e2 to 860dd9a Compare September 25, 2025 10:27
@tomkalon tomkalon force-pushed the bugfix/locales-sorting branch from 860dd9a to d1f58dd Compare September 29, 2025 09:19
@Rafikooo Rafikooo merged commit dce9d20 into Sylius:2.2 Sep 29, 2025
29 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature New feature proposals. Maintenance CI configurations, READMEs, releases, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[2.0][Admin] In translations collection, translation with default/fallback locale should be shown first

2 participants