Skip to content

Commit f505db9

Browse files
authored
fix(locales): use correct lang (#276)
1 parent fa1616a commit f505db9

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

docs/config/basics.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ module.exports = {
2222

2323
The `lang` attribute for the site. This will render as a `<html lang="en-US">` tag in the page HTML.
2424

25-
Note that the `lang` attribute will only be added when building the site via `vitepress build`. You will not see this rendered during `vitepress dev`.
26-
2725
```js
2826
module.exports = {
2927
lang: 'en-US'

src/client/app/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import 'vite/dynamic-import-polyfill'
2-
import { App, createApp as createClientApp, createSSRApp, h } from 'vue'
2+
import {
3+
App,
4+
createApp as createClientApp,
5+
createSSRApp,
6+
h,
7+
onMounted,
8+
watch
9+
} from 'vue'
310
import { inBrowser, pathToFile } from './utils'
411
import { Router, RouterSymbol, createRouter } from './router'
512
import { mixinGlobalComputed, mixinGlobalComponents } from './mixin'
@@ -15,6 +22,19 @@ const NotFound = Theme.NotFound || (() => '404 Not Found')
1522
const VitePressApp = {
1623
name: 'VitePressApp',
1724
setup() {
25+
const siteData = useSiteDataByRoute()
26+
27+
// change the language on the HTML element based on the current lang
28+
onMounted(() => {
29+
watch(
30+
() => siteData.value.lang,
31+
(lang: string) => {
32+
document.documentElement.lang = lang
33+
},
34+
{ immediate: true }
35+
)
36+
})
37+
1838
if (import.meta.env.PROD) {
1939
// in prod mode, enable intersectionObserver based pre-fetch
2040
usePrefetch()

src/client/app/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { reactive, inject, markRaw, nextTick, readonly } from 'vue'
22
import type { Component, InjectionKey } from 'vue'
33
import { PageData } from '../../../types/shared'
4+
import { inBrowser } from './utils'
45

56
export interface Route {
67
path: string
@@ -38,7 +39,6 @@ export function createRouter(
3839
fallbackComponent?: Component
3940
): Router {
4041
const route = reactive(getDefaultRoute())
41-
const inBrowser = typeof window !== 'undefined'
4242

4343
function go(href: string = inBrowser ? location.href : '/') {
4444
// ensure correct deep link so page refresh lands on correct files.

src/client/app/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const inBrowser = typeof window !== 'undefined'
1+
import { inBrowser } from '/@shared/config'
2+
3+
export { inBrowser }
24

35
/**
46
* Join two paths by resolving the slash collision.

src/shared/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SiteData } from '/@types/shared'
22

3-
const inBrowser = typeof window !== 'undefined'
3+
export const inBrowser = typeof window !== 'undefined'
44

55
function findMatchRoot(route: string, roots: string[]) {
66
// first match to the routes with the most deep level.

0 commit comments

Comments
 (0)