Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions src/content/docs/zh-cn/reference/modules/astro-i18n.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ import {
notFound,
middleware,
requestHasLocale,
normalizeTheLocale,
pathHasLocale,
toCodes,
toPaths
} from 'astro:i18n';
```

Expand Down Expand Up @@ -338,3 +342,116 @@ export const onRequest = defineMiddleware(async (context, next) => {
return new Response("Not found", { status: 404 });
})
```

### `normalizeTheLocale()`

<p>

**类型:** `(locale: string) => string`
</p>

在返回小写版本之前,将给定语言环境中的下划线( `_` )替换为连字符( `-` )。

```astro title="src/pages/index.astro"
---
import { normalizeTheLocale } from "astro:i18n";

normalizeTheLocale("it_VT") // 返回 `it-vt`
// 假设当前语言环境设置为 `"pt-PT"`:
normalizeTheLocale(Astro.currentLocale) // 返回 `pt-pt`
---
```

### `pathHasLocale()`

<p>

**类型:** `(path: string) => boolean`<br />
<Since v="4.6.0" />
</p>

检查给定路径是否包含已配置的语言环境。

这在使用依赖于 URL 路径中区域设置的 i18n 工具函数之前有助于防止错误。

```js title="astro.config.mjs"
export default defineConfig({
i18n: {
locales: [
{ codes: ["it-VT", "it"], path: "italiano" },
"es"
]
}
})
```

```astro title="src/pages/index.astro"
---
import { pathHasLocale } from "astro:i18n";

pathHasLocale("italiano"); // 返回 `true`
pathHasLocale("es"); // 返回 `true`
pathHasLocale('/es/blog/'); // 返回 `true`
pathHasLocale("it-VT"); // 返回 `false`
---
```

### `toCodes()`

<p>

**类型:** `(locales: Locales) => string[]`<br />
<Since v="4.0.0" />
</p>

检索为配置中定义的每个区域设置配置的区域代码。当某个区域设置关联多个代码时,只有第一个代码会被添加到数组中。

```js title="astro.config.mjs"
export default defineConfig({
i18n: {
locales: [
{ codes: ["it-VT", "it"], path: "italiano" },
"es"
]
}
})
```

```astro title="src/pages/index.astro"
---
import { i18n } from "astro:config/client";
import { toCodes } from "astro:i18n";

toCodes(i18n!.locales); // ["it-VT", "es"]
---
```

### `toPaths()`

<p>

**类型:** `(locales: Locales) => string[]`<br />
<Since v="4.0.0" />
</p>

检索配置中为每个已定义语言区域设置的路径。

```js title="astro.config.mjs"
export default defineConfig({
i18n: {
locales: [
{ codes: ["it-VT", "it"], path: "italiano" },
"es"
]
}
})
```

```astro title="src/pages/index.astro"
---
import { i18n } from "astro:config/client";
import { toPaths } from "astro:i18n";

toPaths(i18n!.locales); // ["italiano", "es"]
---
```