-
-
Notifications
You must be signed in to change notification settings - Fork 376
fix: error on duplicate useI18n calling on local scope #2204
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
fix: error on duplicate useI18n calling on local scope #2204
Conversation
WalkthroughThis update introduces a new error code and message to handle duplicate calls to Changes
Sequence Diagram(s)sequenceDiagram
participant Component
participant useI18n
participant ErrorHandler
Component->>useI18n: Call useI18n (scope: local)
useI18n->>useI18n: Check for existing local instance
alt No existing instance
useI18n-->>Component: Return Composer instance
else Existing local instance
useI18n->>ErrorHandler: Throw DUPLICATE_USE_I18N_CALLING error
ErrorHandler-->>Component: Error thrown
end
Possibly related PRs
Suggested labels
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (13)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Deploying vue-i18n-next with
|
Latest commit: |
f7247a7
|
Status: | ✅ Deploy successful! |
Preview URL: | https://51cf877e.vue-i18n-next.pages.dev |
Branch Preview URL: | https://fix-error-on-duplicate-calli-dt9p.vue-i18n-next.pages.dev |
Size ReportBundles
Usages
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/vue-i18n-core/src/errors.ts (2)
25-27
: Check numbering strategy for newly-added error codes
DUPLICATE_USE_I18N_CALLING
is assigned the literal33
.
If external libraries or user-land extensions ever rely on a fixed numeric range (24-31 were already “taken”), hard-coding the next integer can create merge-conflicts later.
Consider introducing a small helper (e.g.nextErrorCode()
), or at least adding a short comment that the current max is33
, so the next contributor knows where to continue.
55-56
: Polish the error message for clarityGrammatically the sentence reads a bit awkward:
"Duplicate `useI18n` calling by local scope. Please don't call it on local scope"
Proposed wording (keeps the intent, matches existing tone):
-"Duplicate `useI18n` calling by local scope. Please don't call it on local scope" +"Duplicate local-scope `useI18n` call detected. Call `useI18n` only once per component."packages/vue-i18n-core/test/i18n.test.ts (1)
348-406
: Minor test-hygiene issues: shadowing & missinglocale
Inside
App.setup()
you re-declareconst i18n = useI18n(...)
, shadowing thei18n
constant defined at line 349.
Prefer a distinct name (localI18n
or similar) to avoid confusion.The template binds
v-model="locale"
butlocale
is not returned fromsetup()
, which triggers a Vue runtime warning.
Returnlocale
(and maybet
) to keep the template clean.-const App = defineComponent({ +const App = defineComponent({ setup() { - let message: string = '' - let t: any + let message = '' + let t: any + let locale: any try { - const i18n = useI18n({ ... }) - t = i18n.t + const localI18n = useI18n({ ... }) + t = localI18n.t + locale = localI18n.locale ... - return { t, message, error } + return { t, locale, message, error } },These tweaks make the test output noise-free and easier to read.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/vue-i18n-core/src/errors.ts
(2 hunks)packages/vue-i18n-core/src/i18n.ts
(1 hunks)packages/vue-i18n-core/test/i18n.test.ts
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/vue-i18n-core/src/i18n.ts (1)
packages/vue-i18n-core/src/errors.ts (2)
createI18nError
(32-41)I18nErrorCodes
(10-28)
packages/vue-i18n-core/test/i18n.test.ts (3)
packages/vue-i18n-core/src/errors.ts (2)
errorMessages
(43-57)I18nErrorCodes
(10-28)packages/vue-i18n-core/src/i18n.ts (2)
createI18n
(353-452)useI18n
(535-625)packages/vue-i18n-core/test/helper.ts (1)
mount
(84-202)
⏰ Context from checks skipped due to timeout of 90000ms (14)
- GitHub Check: E2E test for rolldown build (macos-latest, 22)
- GitHub Check: E2E test for rolldown build (macos-latest, 20)
- GitHub Check: E2E test for rolldown build (ubuntu-latest, 20)
- GitHub Check: E2E test for rolldown build (windows-latest, 20)
- GitHub Check: E2E test for rolldown build (windows-latest, 22)
- GitHub Check: E2E test for rolldown build (ubuntu-latest, 22)
- GitHub Check: upload
- GitHub Check: Unit test (windows-latest, 20)
- GitHub Check: Unit test (windows-latest, 22)
- GitHub Check: Build (macos-latest, 20)
- GitHub Check: release
- GitHub Check: Build (windows-latest, 20)
- GitHub Check: Build (ubuntu-latest, 20)
- GitHub Check: release
🔇 Additional comments (1)
packages/vue-i18n-core/src/i18n.ts (1)
613-617
: Runtime behaviour differs between dev & prod – deliberate?The duplicate-call guard is wrapped in
__DEV__
:if (__DEV__ && scope === 'local') { throw createI18nError(I18nErrorCodes.DUPLICATE_USE_I18N_CALLING) }In production builds the second
useI18n
silently returns the existing composer.
That means:
- Bugs will only surface during development, not in prod.
- A malicious/complacent library calling
useI18n()
twice could still work (or break) differently once bundled for production.If the goal is to always prevent the second call, drop the
__DEV__
guard; if the goal is to warn in prod, consider emittingwarn()
instead.- } else { - if (__DEV__ && scope === 'local') { - throw createI18nError(I18nErrorCodes.DUPLICATE_USE_I18N_CALLING) - } - } + } else { + // Always throw; swap to `warn` if a hard error is undesirable in prod + throw createI18nError(I18nErrorCodes.DUPLICATE_USE_I18N_CALLING) + }
@intlify/core
@intlify/core-base
@intlify/devtools-types
@intlify/message-compiler
petite-vue-i18n
@intlify/shared
vue-i18n
@intlify/vue-i18n-core
commit: |
This PR causes some inconvience as we sometimes want to run the // composableThatUsesI18n.ts
export function composableThatUsesI18n() {
const { locale } = useI18n()
// ...
} <script setup lang="ts">
// component.vue
const { t } = useI18n({ useScope: 'local' })
composableThatUsesI18n() // 👈 Triggers duplicate `useI18n` calling error
</script> Suggested solutionCould we handle this silently inside the useI18n Composable? |
This PR is bringed up from #2203
Summary by CodeRabbit
Bug Fixes
useI18n
within the same component, displaying a clear error message if this occurs.Tests
useI18n
is called multiple times in the same local scope.