Merged in feat/syncDefaultMessage (pull request #3022)

Sync defaultMessage from lokalise

* Enhance translation sync functionality and tests

- Added logging for found component files during sync.
- Introduced tests for handling complex components with replacements.
- Updated regex in syncIntlFormatMessage to support optional second arguments.
- Removed unused test files.

* feat(syncDefaultMessage): add script for syncing default message with lokalise

* feat(syncDefaultMessage): add script for syncing default message with lokalise


Approved-by: Matilda Landström
This commit is contained in:
Joakim Jäderberg
2025-10-30 08:38:50 +00:00
parent 3962ecd858
commit bf6ed7778e
48 changed files with 316 additions and 197 deletions

View File

@@ -11,15 +11,17 @@ export function syncIntlFormatMessage({
for (const [messageId, messageValue] of entries) {
const escapedId = messageId.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// Find intl.formatMessage({...}) blocks that contain the specific id
// Find intl.formatMessage({...}) or intl.formatMessage({...}, secondArg) blocks that contain the specific id
const outerRegex = new RegExp(
`intl\\.formatMessage\\(\\s*\\{([^}]*?\\bid\\s*:\\s*['"]${escapedId}['"][^}]*?)\\}\\s*\\)`,
// group 1 = inner object content (without surrounding braces)
// group 2 = optional second argument (anything until the closing parenthesis, non-greedy)
`intl\\.formatMessage\\(\\s*\\{([^}]*?\\bid\\s*:\\s*['"]${escapedId}['"][^}]*?)\\}\\s*(?:,\\s*([^)]*?))?\\s*\\)`,
"gs"
);
fileContent = fileContent.replace(
outerRegex,
(fullMatch, innerObject) => {
(fullMatch: string, innerObject: string, secondArg?: string) => {
// Find defaultMessage: '...' or "..."
const dmRegex =
/defaultMessage\s*:\s*(['"])((?:\\.|[\s\S])*?)\1/;
@@ -38,7 +40,9 @@ export function syncIntlFormatMessage({
);
updated = true;
return `intl.formatMessage({${newInner}})`;
// Preserve secondArg if present
const secondArgPart = secondArg ? `, ${secondArg}` : "";
return `intl.formatMessage({${newInner}}${secondArgPart})`;
}
);
}