Skip to content

[POC] feat(localization): add support for array translations and enhance msgList functionality#2689

Open
SebayK wants to merge 1 commit intoing-bank:masterfrom
SebayK:docs/extend-localize-mixin
Open

[POC] feat(localization): add support for array translations and enhance msgList functionality#2689
SebayK wants to merge 1 commit intoing-bank:masterfrom
SebayK:docs/extend-localize-mixin

Conversation

@SebayK
Copy link
Contributor

@SebayK SebayK commented Jan 27, 2026

Copilot AI review requested due to automatic review settings January 27, 2026 20:33
@changeset-bot
Copy link

changeset-bot bot commented Jan 27, 2026

⚠️ No Changeset found

Latest commit: 4f49eb4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for retrieving structured translation values (arrays/objects) and documents the existing “keys-as-array” fallback behavior in localize.msg.

Changes:

  • Add msgList() to LocalizeManager to return raw translation values (string/array/object).
  • Adjust internal message resolution to return non-string translation values and to treat falsy-but-valid values differently.
  • Extend docs and examples to cover key-array fallback and array translations, plus add demo translation data.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/ui/components/localize/src/LocalizeManager.js Adds msgList() and adjusts message lookup/formatting behavior to allow non-string translation values.
packages/ui/components/localize/test/LocalizeManager.test.js Adds unit tests covering msgList() for arrays/objects/strings, locale override, and key fallback.
docs/fundamentals/systems/localize/text.md Documents key-array fallback mechanism and introduces msgList() usage examples.
docs/fundamentals/systems/localize/assets/en.js Adds an example translated array (keys) used by docs/examples.
docs/fundamentals/systems/localize/assets/nl.js Adds the Dutch equivalent example translated array (keys) used by docs/examples.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -269,10 +269,28 @@ export class LocalizeManager extends EventTarget {
if (!message) {
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

msg() now calls _getMessageForKeys() which can return non-string values like 0 or false, but the current if (!message) check treats those as “not found” and returns an empty string. Use an explicit missing-value check (e.g. message === undefined || message === null || message === '') so valid falsy translations still format correctly.

Suggested change
if (!message) {
if (message === undefined || message === null || message === '') {

Copilot uses AI. Check for mistakes.
Comment on lines +633 to +634
(message, name) =>
typeof message === 'object' && !Array.isArray(message) ? message[name] : message,
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The path resolution in _getMessageForKey stops traversing once it hits an array (!Array.isArray(message)), which causes incorrect lookups: keys like ns:arr.0 can no longer resolve, and keys like ns:arr.nonexistent will incorrectly return the whole array instead of “not found”. Adjust the reducer to handle arrays (at least numeric indices) and to return undefined when a segment can’t be resolved.

Suggested change
(message, name) =>
typeof message === 'object' && !Array.isArray(message) ? message[name] : message,
(message, name) => {
if (message == null) {
return undefined;
}
if (Array.isArray(message)) {
const index = Number(name);
if (!Number.isInteger(index) || index < 0 || index >= message.length) {
return undefined;
}
return message[index];
}
if (typeof message === 'object') {
// For plain objects, return the property value if it exists; otherwise, undefined
return /** @type {any} */ (message)[name];
}
// Primitive value encountered before path is fully resolved
return undefined;
},

Copilot uses AI. Check for mistakes.
Comment on lines 20 to 22
import { localize, formatNumber, formatDate, LocalizeMixin } from '@lion/ui/localize.js';
import { getLocalizeManager } from '@lion/ui/localize-no-side-effects.js';
import '@lion/ui/define-helpers/sb-locale-switcher.js';
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

This doc snippet uses getLocalizeManager() even though it’s marked deprecated in the codebase (packages/ui/components/localize/src/getLocalizeManager.js). Consider using localize (or explicitly documenting why the deprecated API is preferred here) to avoid recommending deprecated usage.

Copilot uses AI. Check for mistakes.
Comment on lines +76 to +80
localizeManager.addData('en-GB', 'promo-banner', {
greeting: 'Special offer! Welcome, {name}!',
});

localizeManager.addData('en-GB', 'defaults', {
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

In this example, localizeManager is referenced without showing how it’s created in the snippet (e.g. via getLocalizeManager()), and later the snippet calls localize.msg(...) instead. For clarity and copy/paste correctness, define localizeManager in the snippet or consistently use localize for both addData and msg.

Suggested change
localizeManager.addData('en-GB', 'promo-banner', {
greeting: 'Special offer! Welcome, {name}!',
});
localizeManager.addData('en-GB', 'defaults', {
localize.addData('en-GB', 'promo-banner', {
greeting: 'Special offer! Welcome, {name}!',
});
localize.addData('en-GB', 'defaults', {

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants