-
Notifications
You must be signed in to change notification settings - Fork 665
added support for dynamic text translation in React components (Fixes… #1262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,17 +32,24 @@ export const LingoComponent = React.forwardRef( | |||||||||||||
| $expressions, | ||||||||||||||
| ...rest | ||||||||||||||
| } = props; | ||||||||||||||
| const maybeValue = $dictionary?.files?.[$fileKey]?.entries?.[$entryKey]; | ||||||||||||||
|
|
||||||||||||||
| // 1). try to find static translation from the dictionary | ||||||||||||||
| const maybeValue = $dictionary?.files?.[$fileKey]?.entries?.[$entryKey]; | ||||||||||||||
|
|
||||||||||||||
| const dynamicValue = | ||||||||||||||
| !maybeValue && typeof $entryKey === "string" | ||||||||||||||
|
||||||||||||||
| !maybeValue && typeof $entryKey === "string" | |
| (maybeValue === undefined || maybeValue === null) && typeof $entryKey === "string" |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name dynamicValue is misleading. It doesn't always contain a dynamic translation value—it can also contain the static translation from maybeValue. A more accurate name would be translatedValue or resolvedValue to reflect that it represents the final resolved translation from either static or dynamic sources.
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A useful comment explaining the cloning behavior of testValue in the ifNotEmpty function was removed. This comment provided important context about why cloning happens before the callback is invoked rather than inside it (due to recursion). Consider restoring this documentation as it helps future maintainers understand this non-obvious design decision.
| // Note: We clone `testValue` before invoking the callback rather than inside it. | |
| // This is important because the callback may be recursive, and cloning inside | |
| // the callback would result in multiple unnecessary clones or subtle bugs. | |
| // Cloning here ensures that each invocation receives a fresh copy. |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment has grammatical errors. It should read "helper to handle" instead of "helper for handle". Additionally, the ellipsis (...) at the end is unnecessary and should be removed.
| helper for handle dynamic ( non-key ) translation text ...... | |
| helper to handle dynamic (non-key) translation text |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dictionary parameter is typed as any, which loses type safety. Consider creating a proper type definition for the dictionary structure or at least using a more specific type that captures the expected shape: { files?: { [key: string]: { entries?: { [key: string]: any } } } }. This would improve type safety and make the API more predictable.
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardcoded file key 'auto' lacks documentation. It's unclear why this specific key is used for dynamic translations or how developers should populate this section of the dictionary. Consider:
- Adding a comment explaining what the
'auto'file key represents - Documenting this convention in the function's JSDoc comment
- Considering making this configurable via a parameter or constant
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new autoTranslateText function lacks test coverage. Since this file has comprehensive test coverage for other features (as seen in component.spec.tsx), the dynamic translation functionality should also be tested. Consider adding test cases to verify:
- Dynamic text translation when a matching entry exists in
dictionary?.files?.auto?.entries - Fallback behavior when no translation is found (should return original text)
- Integration with the existing variable/element replacement logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The comment uses numbered step notation "1)." but there is no corresponding step 2. Either remove the numbering or add a comment for step 2 (the dynamic translation fallback on lines 39-42) to maintain consistency.