Skip to content

Commit

Permalink
i18n(zh-cn): Update i18n.mdx (#2775)
Browse files Browse the repository at this point in the history
Co-authored-by: HiDeoo <[email protected]>
  • Loading branch information
liruifengv and HiDeoo authored Jan 8, 2025
1 parent aa69440 commit 87f0d5c
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions docs/src/content/docs/zh-cn/guides/i18n.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,17 @@ import UIStringsList from '~/components/ui-strings-list.astro';

<Steps>

1. 如果尚未配置,请在 `src/content/config.ts` 中配置 `i18n` 数据集合:
1. 如果尚未配置,请在 `src/content.config.ts` 中配置 `i18n` 数据集合:

```diff lang="js" ins=/, (i18nSchema)/
// src/content/config.ts
```diff lang="js" ins=/, (i18nLoader|i18nSchema)/
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { docsLoader, i18nLoader } from '@astrojs/starlight/loaders';
import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';

export const collections = {
docs: defineCollection({ schema: docsSchema() }),
+ i18n: defineCollection({ type: 'data', schema: i18nSchema() }),
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
+ i18n: defineCollection({ loader: i18nLoader(), schema: i18nSchema() }),
};
```

Expand Down Expand Up @@ -256,14 +257,15 @@ import UIStringsList from '~/components/ui-strings-list.astro';
在下面的示例中添加了一个新的可选的 `custom.label` 键:

```diff lang="js"
// src/content/config.ts
// src/content.config.ts
import { defineCollection, z } from 'astro:content';
import { docsLoader, i18nLoader } from '@astrojs/starlight/loaders';
import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';

export const collections = {
docs: defineCollection({ schema: docsSchema() }),
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
i18n: defineCollection({
type: 'data',
loader: i18nLoader(),
schema: i18nSchema({
+ extend: z.object({
+ 'custom.label': z.string().optional(),
Expand All @@ -273,22 +275,22 @@ export const collections = {
};
```

在 Astro 文档的[“定义集合模式”](https://docs.astro.build/zh-cn/guides/content-collections/#定义集合模式)中了解有关内容集合 schema 的更多信息。
在 Astro 文档的[“定义集合模式”](https://docs.astro.build/zh-cn/guides/content-collections/#定义集合模式schema)中了解有关内容集合 schema 的更多信息。

## 使用 UI 翻译

你可以使用由 [i18next](https://www.i18next.com/) 提供支持的统一 API 访问 Starlight 的 [内置 UI 字符串](/zh-cn/guides/i18n/#翻译-starlight-的-ui) 以及 [用户定义](/zh-cn/guides/i18n/#拓展翻译-schema)[插件提供](/zh-cn/reference/plugins/#injecttranslations) 的 UI 字符串。
其中包括了对 [插值](https://www.i18next.com/translation-function/interpolation)[多元化](https://www.i18next.com/translation-function/plurals) 等功能的支持。

在 Astro 组件中,此 API 作为 [全局 `Astro` 对象](https://docs.astro.build/zh-cn/reference/api-reference/#astrolocals) 的一部分提供,即 `Astro.locals.t`
在 Astro 组件中,此 API 作为 [全局 `Astro` 对象](https://docs.astro.build/zh-cn/reference/api-reference/#locals) 的一部分提供,即 `Astro.locals.t`

```astro title="example.astro"
<p dir={Astro.locals.t.dir()}>
{Astro.locals.t('404.text')}
</p>
```

你还可以在 [端点](https://docs.astro.build/zh-cn/guides/endpoints/) 中使用 API,其中的 `locals` 对象可作为 [端点上下文](https://docs.astro.build/zh-cn/reference/api-reference/#contextlocals) 的一部分使用:
你还可以在 [端点](https://docs.astro.build/zh-cn/guides/endpoints/) 中使用 API,其中的 `locals` 对象可作为 [端点上下文](https://docs.astro.build/zh-cn/reference/api-reference/#locals) 的一部分使用:

```ts title="src/pages/404.ts"
export const GET = (context) => {
Expand Down Expand Up @@ -389,7 +391,7 @@ const arabicDirection = Astro.locals.t.dir('ar');

## 访问当前语言环境

你可以使用 [`Astro.currentLocale`](https://docs.astro.build/zh-cn/reference/api-reference/#astrocurrentlocale)`.astro` 组件中读取当前的语言环境。
你可以使用 [`Astro.currentLocale`](https://docs.astro.build/zh-cn/reference/api-reference/#currentlocale)`.astro` 组件中读取当前的语言环境。

下面的示例读取当前语言环境,并使用它和 [`getRelativeLocaleUrl()`](https://docs.astro.build/zh-cn/reference/modules/astro-i18n/#getrelativelocaleurl) 助手函数以生成一个当前语言的关于页面的链接:

Expand Down

0 comments on commit 87f0d5c

Please sign in to comment.