diff --git a/src/content/docs/zh-cn/reference/modules/astro-i18n.mdx b/src/content/docs/zh-cn/reference/modules/astro-i18n.mdx index 75c22224a129d..2ab68454b62a1 100644 --- a/src/content/docs/zh-cn/reference/modules/astro-i18n.mdx +++ b/src/content/docs/zh-cn/reference/modules/astro-i18n.mdx @@ -40,6 +40,10 @@ import { notFound, middleware, requestHasLocale, + normalizeTheLocale, + pathHasLocale, + toCodes, + toPaths } from 'astro:i18n'; ``` @@ -338,3 +342,116 @@ export const onRequest = defineMiddleware(async (context, next) => { return new Response("Not found", { status: 404 }); }) ``` + +### `normalizeTheLocale()` + +

+ +**类型:** `(locale: string) => string` +

+ +在返回小写版本之前,将给定语言环境中的下划线( `_` )替换为连字符( `-` )。 + +```astro title="src/pages/index.astro" +--- +import { normalizeTheLocale } from "astro:i18n"; + +normalizeTheLocale("it_VT") // 返回 `it-vt` +// 假设当前语言环境设置为 `"pt-PT"`: +normalizeTheLocale(Astro.currentLocale) // 返回 `pt-pt` +--- +``` + +### `pathHasLocale()` + +

+ +**类型:** `(path: string) => boolean`
+ +

+ +检查给定路径是否包含已配置的语言环境。 + +这在使用依赖于 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()` + +

+ +**类型:** `(locales: Locales) => string[]`
+ +

+ +检索为配置中定义的每个区域设置配置的区域代码。当某个区域设置关联多个代码时,只有第一个代码会被添加到数组中。 + +```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()` + +

+ +**类型:** `(locales: Locales) => string[]`
+ +

+ +检索配置中为每个已定义语言区域设置的路径。 + +```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"] +--- +```