diff --git a/docs/guide/global-computed.md b/docs/guide/global-computed.md index d1c0d3f1..ea46cf4f 100644 --- a/docs/guide/global-computed.md +++ b/docs/guide/global-computed.md @@ -64,6 +64,14 @@ Reference of `$page.frontmatter`. } ``` +## $lang + +The language of the current page. Default: `en-US`. + +## $localePath + +The locale path prefix for the current page. Default: `/`. + ## $title Value of the `` label used for the current page. @@ -77,5 +85,5 @@ The content value of the `<meta name= "description" content= "...">` for the cur Helper method to generate correct path by prepending the `base` path configured in `.vitepress/config.js`. It's useful when you want to link to [public files with base path](./assets#public-files). ```html -<img :src="$withBase('/foo.png')" alt="foo"> +<img :src="$withBase('/foo.png')" alt="foo" /> ``` diff --git a/src/client/app/mixin.ts b/src/client/app/mixin.ts index e33a5b9e..86dfe665 100644 --- a/src/client/app/mixin.ts +++ b/src/client/app/mixin.ts @@ -56,6 +56,24 @@ export function mixinGlobalComputed( } }, + $lang: { + get() { + return siteByRoute.value.lang + } + }, + + $localePath: { + get() { + const { locales } = site.value + const { lang } = siteByRoute.value + return ( + (locales && + Object.keys(locales).find((lp) => locales[lp].lang === lang)) || + '/' + ) + } + }, + $withBase: { value(path: string) { return joinPath(site.value.base, path)