コンテンツにスキップ

インターナショナル化

組み込みのi18n機能を使用するには、ディレクトリ構造を次のように作成する必要があります

docs/
├─ es/
│  ├─ foo.md
├─ fr/
│  ├─ foo.md
├─ foo.md

次に、docs/.vitepress/config.ts

ts
import { defineConfig } from 'vitepress'

export default defineConfig({
  // shared properties and other top-level stuff...

  locales: {
    root: {
      label: 'English',
      lang: 'en'
    },
    fr: {
      label: 'French',
      lang: 'fr', // optional, will be added  as `lang` attribute on `html` tag
      link: '/fr/guide' // default /fr/ -- shows on navbar translations menu, can be external

      // other locale specific properties...
    }
  }
})

次のプロパティは、各ロケール(ルートを含む)でオーバーライドできます

ts
interface LocaleSpecificConfig<ThemeConfig = any> {
  lang?: string
  dir?: string
  title?: string
  titleTemplate?: string | boolean
  description?: string
  head?: HeadConfig[] // will be merged with existing head entries, duplicate meta tags are automatically removed
  themeConfig?: ThemeConfig // will be shallow merged, common stuff can be put in top-level themeConfig entry
}

詳細は、DefaultTheme.Configインターフェースを参照し、デフォルトテーマのプレースホルダーテキストをカスタマイズします。ロケールレベルでthemeConfig.algoliaまたはthemeConfig.carbonAdsをオーバーライドしないでください。多言語検索の使用については、 Algoliaのドキュメントを参照してください。

プロのヒント: 構成ファイルはdocs/.vitepress/config/index.tsにも保存できます。構成ファイルをロケールごとに作成し、index.tsからそれらをマージしてエクスポートすることで、整理に役立つ場合があります。

ロケールごとの個別ディレクトリ

以下は完全に問題のない構造です

docs/
├─ en/
│  ├─ foo.md
├─ es/
│  ├─ foo.md
├─ fr/
   ├─ foo.md

ただし、VitePressはデフォルトで//en/にリダイレクトしません。そのためにはサーバーを設定する必要があります。たとえば、Netlifyでは、次のようにdocs/public/_redirectsファイルを追加できます

/*  /es/:splat  302  Language=es
/*  /fr/:splat  302  Language=fr
/*  /en/:splat  302

プロのヒント:上記の方法を使用する場合、nf_lang Cookieを使用してユーザーの言語選択を永続化できます

ts
// docs/.vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme'
import Layout from './Layout.vue'

export default {
  extends: DefaultTheme,
  Layout
}
vue
<!-- docs/.vitepress/theme/Layout.vue -->
<script setup lang="ts">
import DefaultTheme from 'vitepress/theme'
import { useData } from 'vitepress'
import { watchEffect } from 'vue'

const { lang } = useData()
watchEffect(() => {
  if (inBrowser) {
    document.cookie = `nf_lang=${lang.value}; expires=Mon, 1 Jan 2030 00:00:00 UTC; path=/`
  }
})
</script>

<template>
  <DefaultTheme.Layout />
</template>

RTLサポート(実験的)

RTLサポートの場合、configでdir: 'rtl'を指定し、https://github.com/MohammadYounes/rtlcsshttps://github.com/vkalinichev/postcss-rtlhttps://github.com/elchininet/postcss-rtlcssなどのRTLCSS PostCSSプラグインを使用します。CSSの特異性の問題が発生しないように、:where([dir="ltr"]):where([dir="rtl"])をプレフィックスとして使用するようにPostCSSプラグインを構成する必要があります。

MITライセンスに基づいてリリースされています。