Skip to content

Conversation

kazupon
Copy link
Member

@kazupon kazupon commented Jun 17, 2025

This PR is bringed up from #2203

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling to prevent duplicate calls to useI18n within the same component, displaying a clear error message if this occurs.
  • Tests

    • Added a test case to ensure an error is thrown when useI18n is called multiple times in the same local scope.

Copy link

coderabbitai bot commented Jun 17, 2025

Walkthrough

This update introduces a new error code and message to handle duplicate calls to useI18n with local scope within the same component. The useI18n function now throws a specific error if invoked multiple times locally, and a corresponding test case verifies this behavior.

Changes

Files/Groups Change Summary
packages/vue-i18n-core/src/errors.ts Added error code DUPLICATE_USE_I18N_CALLING and its message to error handling definitions.
packages/vue-i18n-core/src/i18n.ts Modified useI18n to throw an error if called multiple times with local scope in the same component.
packages/vue-i18n-core/test/i18n.test.ts Added a test case to ensure duplicate local useI18n calls trigger the new error.

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
Loading

Possibly related PRs

Suggested labels

Type: Improvement

Poem

In the warren of code, a new rule’s in play,
No double dipping with useI18n, hooray!
If you try it twice in a single scope,
An error will catch you, so don’t even hope.
The bunny says, “One hop at a time,
Keeps your translations working just fine!” 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c0bc82 and f7247a7.

📒 Files selected for processing (1)
  • packages/vue-i18n-core/src/errors.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/vue-i18n-core/src/errors.ts
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: E2E test for rolldown build (windows-latest, 22)
  • GitHub Check: E2E test for rolldown build (ubuntu-latest, 22)
  • GitHub Check: E2E test for rolldown build (ubuntu-latest, 20)
  • GitHub Check: E2E test for rolldown build (macos-latest, 20)
  • GitHub Check: E2E test for rolldown build (macos-latest, 22)
  • GitHub Check: E2E test for rolldown build (windows-latest, 20)
  • GitHub Check: release
  • GitHub Check: Build (ubuntu-latest, 20)
  • GitHub Check: Build (windows-latest, 20)
  • GitHub Check: upload
  • GitHub Check: Build (macos-latest, 20)
  • GitHub Check: release
  • GitHub Check: Cloudflare Pages
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kazupon kazupon added the Type: Improvement Includes backwards-compatible fixes label Jun 17, 2025
Copy link

cloudflare-workers-and-pages bot commented Jun 17, 2025

Deploying vue-i18n-next with  Cloudflare Pages  Cloudflare Pages

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

View logs

Copy link

Size Report

Bundles

File Size Gzip Brotli
core.esm-browser.prod.js 38.05 kB 11.62 kB 10.39 kB
core.global.prod.js 31.11 kB 10.78 kB 9.67 kB
core.runtime.esm-browser.prod.js 23.77 kB 7.71 kB 6.90 kB
core.runtime.global.prod.js 18.08 kB 7.04 kB 6.32 kB
message-compiler.esm-browser.prod.js 19.29 kB 5.76 kB 5.14 kB
message-compiler.global.prod.js 17.32 kB 5.55 kB 4.98 kB
petite-vue-i18n-core.esm-browser.prod.js 20.70 kB (+0.03 kB) 6.98 kB (+0.02 kB) 6.27 kB (+0.02 kB)
petite-vue-i18n-core.global.prod.js 15.66 kB (+0.03 kB) 6.08 kB (+0.02 kB) 5.51 kB (+0.01 kB)
petite-vue-i18n.esm-browser.prod.js 37.01 kB (+0.03 kB) 11.36 kB (+0.02 kB) 10.19 kB (+0.02 kB)
petite-vue-i18n.global.prod.js 29.87 kB (+0.03 kB) 10.21 kB (+0.02 kB) 9.22 kB (+0.01 kB)
petite-vue-i18n.runtime.esm-browser.prod.js 22.58 kB (+0.03 kB) 7.35 kB (+0.02 kB) 6.64 kB (+0.02 kB)
petite-vue-i18n.runtime.global.prod.js 16.86 kB (+0.03 kB) 6.42 kB (+0.02 kB) 5.82 kB (+0.01 kB)
vue-i18n.esm-browser.prod.js 50.71 kB (+0.03 kB) 15.17 kB (+0.02 kB) 13.56 kB (+0.01 kB)
vue-i18n.global.prod.js 40.57 kB (+0.03 kB) 13.63 kB (+0.02 kB) 12.25 kB (+0.01 kB)
vue-i18n.runtime.esm-browser.prod.js 36.28 kB (+0.03 kB) 11.17 kB (+0.02 kB) 10.03 kB (+0.01 kB)
vue-i18n.runtime.global.prod.js 27.56 kB (+0.03 kB) 9.88 kB (+0.02 kB) 8.91 kB (+0.02 kB)

Usages

Name Size Gzip Brotli
packages/size-check-core (@intlify/core) 9.22 kB 3.82 kB 3.46 kB
packages/size-check-petite-vue-i18n (petite-vue-i18n) 77.63 kB 30.24 kB 27.28 kB
packages/size-check-vue-i18n (vue-i18n) 82.85 kB 31.74 kB 28.64 kB

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 (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 literal 33.
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 is 33, so the next contributor knows where to continue.


55-56: Polish the error message for clarity

Grammatically 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 & missing locale

  1. Inside App.setup() you re-declare const i18n = useI18n(...), shadowing the i18n constant defined at line 349.
    Prefer a distinct name (localI18n or similar) to avoid confusion.

  2. The template binds v-model="locale" but locale is not returned from setup(), which triggers a Vue runtime warning.
    Return locale (and maybe t) 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

📥 Commits

Reviewing files that changed from the base of the PR and between d4c6e3a and 9c0bc82.

📒 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:

  1. Bugs will only surface during development, not in prod.
  2. 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 emitting warn() 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)
+  }

Copy link

pkg-pr-new bot commented Jun 17, 2025

Open in StackBlitz

@intlify/core

npm i https://pkg.pr.new/@intlify/core@2204

@intlify/core-base

npm i https://pkg.pr.new/@intlify/core-base@2204

@intlify/devtools-types

npm i https://pkg.pr.new/@intlify/devtools-types@2204

@intlify/message-compiler

npm i https://pkg.pr.new/@intlify/message-compiler@2204

petite-vue-i18n

npm i https://pkg.pr.new/petite-vue-i18n@2204

@intlify/shared

npm i https://pkg.pr.new/@intlify/shared@2204

vue-i18n

npm i https://pkg.pr.new/vue-i18n@2204

@intlify/vue-i18n-core

npm i https://pkg.pr.new/@intlify/vue-i18n-core@2204

commit: f7247a7

@kazupon kazupon merged commit 9c7573e into master Jun 17, 2025
31 checks passed
@kazupon kazupon deleted the fix/error-on-duplicate-calling-on-local-scope-for-v12 branch June 17, 2025 01:53
@AbdallahAlhaddad
Copy link

This PR causes some inconvience as we sometimes want to run the useI18n composable more than once. see the following usecase.

// 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 solution

Could we handle this silently inside the useI18n Composable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Improvement Includes backwards-compatible fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants