How to access setLocal() when using vue-i18n-routing

I have a vue app using vue-i18n-routing (https://github.com/intlify/routing) which is based on @nuxtjs/i18n (https://i18n.nuxtjs.org). I have it running and it handles the routing and language switching beautifully, except from one problem I’m having:

I can’t access setLocale(). Here’s the details on this from https://i18n.nuxtjs.org/docs/api/vue-i18n#setlocale:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>setLocale()
Arguments:
locale (type: string)
Returns: Promise<void>
Switches locale of the app to specified locale code. If useCookie option is enabled, locale cookie will be updated with new value. If prefixes are enabled (strategy other than no_prefix), will navigate to new locale's route.
</code>
<code>setLocale() Arguments: locale (type: string) Returns: Promise<void> Switches locale of the app to specified locale code. If useCookie option is enabled, locale cookie will be updated with new value. If prefixes are enabled (strategy other than no_prefix), will navigate to new locale's route. </code>
setLocale()

Arguments:
locale (type: string)
Returns: Promise<void>
Switches locale of the app to specified locale code. If useCookie option is enabled, locale cookie will be updated with new value. If prefixes are enabled (strategy other than no_prefix), will navigate to new locale's route.

I need to use this so that the url changes to reflect the chosen locale. However, I don’t know what or how to import in order to actually use this feature. I’ve read the docs, looked at examples, and searched for similar questions, but nothing has helped me. I suspect I’m overlooking something basic.

Here’s my router config:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { createRouter as _createRouter } from 'vue-i18n-routing'
import { createWebHistory } from 'vue-router'
import type { I18n } from 'vue-i18n'
export function createRouter(i18n: I18n) {
return _createRouter(i18n, {
version: 4,
detectBrowserLanguage: {
alwaysRedirect: false,
fallbackLocale: 'en',
redirectOn: 'no prefix',
},
strategy: 'prefix_and_default',
trailingSlash: true,
defaultLocale: 'en',
locales: [
{
code: 'en',
iso: 'en-US',
name: 'English'
},
{
code: 'fr',
iso: 'fr-FR',
name: 'French'
}
],
history: createWebHistory(import.meta.env.BASE_URL),
},
routes: [
/* not shown */
]
})
}
</code>
<code>import { createRouter as _createRouter } from 'vue-i18n-routing' import { createWebHistory } from 'vue-router' import type { I18n } from 'vue-i18n' export function createRouter(i18n: I18n) { return _createRouter(i18n, { version: 4, detectBrowserLanguage: { alwaysRedirect: false, fallbackLocale: 'en', redirectOn: 'no prefix', }, strategy: 'prefix_and_default', trailingSlash: true, defaultLocale: 'en', locales: [ { code: 'en', iso: 'en-US', name: 'English' }, { code: 'fr', iso: 'fr-FR', name: 'French' } ], history: createWebHistory(import.meta.env.BASE_URL), }, routes: [ /* not shown */ ] }) } </code>
import { createRouter as _createRouter } from 'vue-i18n-routing'
import { createWebHistory } from 'vue-router'

import type { I18n } from 'vue-i18n'

export function createRouter(i18n: I18n) {
  return _createRouter(i18n, {
    version: 4,
    detectBrowserLanguage: {
      alwaysRedirect: false,
      fallbackLocale: 'en',
      redirectOn: 'no prefix',
    },
    strategy: 'prefix_and_default',
    trailingSlash: true,
    defaultLocale: 'en',
    locales: [
      {
        code: 'en',
        iso: 'en-US',
        name: 'English'
      },
      {
        code: 'fr',
        iso: 'fr-FR',
        name: 'French'
      }
    ],
    history: createWebHistory(import.meta.env.BASE_URL),
    },
    routes: [
      /* not shown */
    ]
  })
}

And where I create i18n:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { createI18n } from 'vue-i18n'
import en from './locales/en.json'
import fr from './locales/fr.json'
const i18n = createI18n<true>({
legacy: true,
messages: {
en,
fr
}
})
export default i18n
</code>
<code>import { createI18n } from 'vue-i18n' import en from './locales/en.json' import fr from './locales/fr.json' const i18n = createI18n<true>({ legacy: true, messages: { en, fr } }) export default i18n </code>
import { createI18n } from 'vue-i18n'

import en from './locales/en.json'
import fr from './locales/fr.json'

const i18n = createI18n<true>({
  legacy: true,
  messages: {
    en,
    fr
  }
})

export default i18n

And my language switcher with both a working and a non-working example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <div v-if="$i18n.locale === 'en'">
<a @click="$i18n.locale = 'fr'">FR</a> // works, but lacks the url changing I need
</div>
<div v-if="$i18n.locale === 'fr'">
<a @click="$i18n.setLocale('en')">EN</a> // doesn't work at all (Property 'setLocale' may not exist on type 'VueI18n')
</div>
</code>
<code> <div v-if="$i18n.locale === 'en'"> <a @click="$i18n.locale = 'fr'">FR</a> // works, but lacks the url changing I need </div> <div v-if="$i18n.locale === 'fr'"> <a @click="$i18n.setLocale('en')">EN</a> // doesn't work at all (Property 'setLocale' may not exist on type 'VueI18n') </div> </code>
  <div v-if="$i18n.locale === 'en'">
    <a @click="$i18n.locale = 'fr'">FR</a> // works, but lacks the url changing I need
  </div>
  <div v-if="$i18n.locale === 'fr'">
    <a @click="$i18n.setLocale('en')">EN</a> // doesn't work at all (Property 'setLocale' may not exist on type 'VueI18n')
  </div>

Can someone help me get this setLocale() feature working?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật