API 参考
Vue 的扩展
localePath()
- 参数:
- route (类型:
string
|Location
) - locale (类型:
string
,默认: 当前语言环境)
- route (类型:
- 返回:
string
返回传入 route
的本地化路径。如果未指定 locale
,则使用当前语言环境。
另见 基础用法 - nuxt-link。
switchLocalePath()
- 参数:
- locale: (类型:
string
)
- locale: (类型:
- 返回:
string
返回指定 locale
的当前路由路径。
另见 基础用法 - nuxt-link。
有关 Location 的类型定义请查看。
getRouteBaseName()
- 参数:
- route (类型:
Route
,默认: 当前路由)
- route (类型:
- 返回:
string
返回当前(如果未提供参数)或传入 route
的基础名称。基础名称是没有语言环境后缀和其他由 @nuxtjs/i18n
添加的元数据的路由名称。
localeRoute()
- 参数:
- route (类型:
RawLocation
) - locale (类型:
string
,默认: 当前语言环境)
- route (类型:
- 返回:
Route
|undefined
返回传入 route
参数的本地化路由。如果未指定 locale
,则使用当前语言环境。
另见 基础用法 - nuxt-link。
localeLocation()
- 参数:
- route (类型:
RawLocation
) - locale (类型:
string
,默认: 当前语言环境)
- route (类型:
- 返回:
Location
|undefined
返回传入 route
参数的本地化位置。如果未指定 locale
,则使用当前语言环境。
另见 基础用法 - nuxt-link。
$nuxtI18nHead()
- 参数:
- options: (类型:
NuxtI18nHeadOptions
)
- options: (类型:
- 返回:
MetaInfo
options
对象接受以下可选属性:
addDirAttribute
(类型:boolean
) - 向 HTML 元素添加dir
属性。默认:false
addSeoAttributes
(类型:boolean | SeoAttributesOptions
) - 添加各种 SEO 属性。默认:false
另见 SEO。
VueI18n 的扩展
getLocaleCookie()
- 参数:
- 无参数
- 返回:
string | undefined
从存储的语言环境 Cookie 中返回语言代码。
setLocaleCookie()
- 参数:
- locale (类型:
string
)
- locale (类型:
- 返回:
undefined
使用指定的语言代码更新存储的语言环境 Cookie。如果想切换语言环境,建议使用 setLocale
。
setLocale()
- 参数:
- locale (类型:
string
)
- locale (类型:
- 返回:
Promise<undefined>
将应用的语言环境切换到指定的语言代码。如果启用了 useCookie
选项,语言环境 Cookie 将更新为新值。如果启用了前缀(即 strategy
不是 no_prefix
),将导航到新语言的路由。
getBrowserLocale()
- 参数:
- 无参数
- 返回:
string | undefined
返回过滤后的浏览器语言代码。
finalizePendingLocaleChange()
- 参数:
- 无参数
- 返回:
Promise<undefined>
切换到本将于导航时设置的待定语言环境,但由于选项 skipSettingLocaleOnNavigate
被阻止而未设置。有关更多信息,请参见 等待页面过渡。
waitForPendingLocaleChange()
- 参数:
- 无参数
- 返回:
Promise<undefined>
返回一个 Promise,一旦设置了待定语言环境,将被解析。
defaultDirection
- 类型:
Directions
根据选项指定的默认方向。
defaultLocale
- 类型:
string
根据选项指定的默认语言环境。
localeCodes
- 类型:
Array<string>
已注册语言环境的语言代码列表。
locales
- 类型:
Array<string | LocaleObject>
根据选项定义的语言环境列表。
localeProperties
- 类型:
LocaleObject
当前语言环境属性的对象。
differentDomains
- 类型:
boolean
是否启用了 differentDomains
选项。
onBeforeLanguageSwitch
- 类型:
Function
参见 回调
onLanguageSwitched
- 类型:
Function
参见 回调
Nuxt 上下文的扩展
以下 API 在 context
和 context.app
上都可用。
i18n
- 类型:
VueI18n
另见 Nuxt 上下文。
可以从 asyncData
和任何可用 context
的位置访问。
示例用法:
export default Vue.extend({
asyncData({ i18n }) {
const locale = i18n.locale
return {
locale
}
}
})
getRouteBaseName()
localePath()
localeRoute()
localeLocation()
switchLocalePath()
有关这些内容的更多信息,请参见 Vue 的扩展 部分。
Vuex 的扩展
$i18n
- 类型:
VueI18n
可以在存储的 actions 和 mutations 中作为 this.$i18n
访问。
示例用法:
export const actions = {
nuxtServerInit({ commit }) {
commit('LOCALE', this.$i18n.locale)
}
}
getRouteBaseName()
localePath()
localeRoute()
localeLocation()
switchLocalePath()
有关这些内容的更多信息,请参见 Vue 的扩展 部分。