i18n

设置支持的语言

site.config.ts
ts
import { defineSiteConfig } from 'valaxy'

export default defineSiteConfig({
  languages: ['zh-CN', 'en'],
})

在配置中使用国际化

如果你想要为 siteConfig.title/siteConfig.description 添加国际化支持,可以在 siteConfig 中设定键值。

TIP

$t 是 Valaxy 提供的一个虚拟函数,它会添加特定的前缀 $locale: 以标记此处的文本需要国际化处理。 随后,Valaxy 会在页面中自动替换为对应语言的文本。 因此,它在页面上仍然是支持响应式的。

例如:

site.config.ts
ts
import { $t, defineSiteConfig } from 'valaxy'

export default defineSiteConfig({
  title: $t('siteConfig.title'),
  description: $t('siteConfig.description'),
})

然后在 locales 目录下创建对应的语言文件。

locales/zh-CN.yml
yaml
siteConfig:
  title: 你好,世界
locales/en.yml
yaml
siteConfig:
  title: Hello World

单页 i18n

i18n in One Page

TIP

Valaxy 提出了一种基于 CSS 面向博客的 i18n 解决方案。

你可以在同一个页面中快速编写中英文博客。

如果你想了解实现原理,可参考 i18n

Valaxy proposed a CSS-based i18n solution for blog.

You can quickly write English and Chinese blogs from the same page.

If you want to know how this works, see i18n.

效果如下(点击按钮切换):

The effect is as follows (click the button to switch).

另一种 i18n 方案。

更多内容:…

Another i18n method.

More info…

中文

English


书写方式如下:

Written like this:

md
::: zh-CN
另一种 i18n 方案。

更多内容:...
:::

::: en
Another i18n method.

More info...
:::

::: zh-CN
中文
:::

::: en
English
:::

标题 i18n

Title i18n

当然,Valaxy 同样支持标题的 i18n。原理同上。

你可以采用如下方式书写:

Of course, Valaxy supports i18n on titles. Works the same as above.

You can write internationalized titles like this:

md
### 你好,世界 {lang="zh-CN"}

### Hello World {lang="en"}

Frontmatter i18n

实现 titledescription 的国际化:

Internationalizing title and description:

md
---
title:
  en: Hello World
  zh-CN: 你好,世界
description:
  en: A simple i18n example
  zh-CN: 一个简单的 i18n 示例
---

分类/标签 i18n

Category/Tag i18n

TIP

为什么这里不是使用类似 title/description 的对象方式,而是使用特殊前缀 $locale:tag.notes 的方式?

因为分类和标签仍然需要一个唯一的键值,且它们通常是在多篇文章中复用的。 如果使用对象方式,你将不得不在每篇文章的 frontmatter 中重复定义分类和标签的多种语言。

posts/hello-world.md
md
---
categories:
  - $locale:category.test
tags:
  - $locale:tag.notes
---
locales/zh-CN.yml
yaml
category:
  test: 测试
tag:
  notes: 笔记
locales/en.yml
yaml
category:
  test: Test
tag:
  notes: Notes

Contributors