Creation:2025-11-16Last update:2025-11-16
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
将 MCP 服务器添加到您的 AI 助手
通过将 Intlayer MCP 服务器集成到您的 AI 助手,您可以直接从 ChatGPT、DeepSeek、Cursor、VSCode 等获取所有文档。
查看 MCP 服务器文档版本历史
- 初始文档v7.1.02025/11/16
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本编辑此文档
如果您有改善此文档的想法,请随时通过在GitHub上提交拉取请求来贡献。
文档的 GitHub 链接Copy
复制文档 Markdown 到剪贴板
文档:intlayer 中的 getPrefix 函数
描述
getPrefix 函数根据路由模式配置确定给定 locale 的 URL 前缀。它将 locale 与默认 locale 进行比较,并返回一个包含三种不同前缀格式的对象,以实现灵活的 URL 构建。
主要特性:
- 接受一个 locale 作为第一个参数(必需)
- 可选的 options 对象,包含 defaultLocale 和 mode
- 返回一个包含 prefix 和 localePrefix 属性的对象
- 支持所有路由模式:prefix-no-default、prefix-all、no-prefix 和 search-params
- 轻量级工具,用于确定何时添加 locale 前缀
函数签名
getPrefix( locale: Locales, // 必需 options?: { // 可选 defaultLocale?: Locales; mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'; }): GetPrefixResulttype GetPrefixResult = { prefix: string; // 例如 'fr/' 或 '' localePrefix?: Locale; // 例如 'fr' 或 undefined}参数
locale: Locales
- 描述:用于生成前缀的语言环境。如果该值为假值(undefined、null、空字符串),函数将返回空字符串。
- 类型:Locales
- 必需:是
options?: object
- 描述:用于确定前缀的配置对象。
- 类型:object
必需:否(可选)
options.defaultLocale?: Locales
- 描述:应用程序的默认语言环境。如果未提供,则使用项目配置中设置的默认语言环境。
- 类型:Locales
- 默认值:项目配置
options.mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'
- 描述:用于处理本地化的 URL 路由模式。如果未提供,则使用项目配置中设置的模式。
- 类型:'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'
- 默认值:项目配置
- 模式:
- prefix-no-default:当 locale 与默认 locale 匹配时返回空字符串
- prefix-all:为所有 locale(包括默认)返回前缀
- no-prefix:返回空字符串(URL 中无前缀)
- search-params:返回空字符串(locale 作为查询参数)
返回值
- 类型:GetPrefixResult
- 描述:包含三种不同前缀格式的对象:
- prefix:带有尾部斜杠的路径前缀(例如,'fr/','')
- localePrefix:不带斜杠的语言标识符(例如,'fr',undefined)
示例用法
基本用法
import { getPrefix, Locales } from "intlayer";// 检查英语语言的前缀getPrefix(Locales.ENGLISH, { defaultLocale: Locales.ENGLISH, mode: "prefix-all",});// 返回: { prefix: 'en/', localePrefix: 'en' }// 检查法语语言的前缀getPrefix(Locales.FRENCH, { defaultLocale: Locales.ENGLISH, mode: "prefix-no-default",});// 返回: { prefix: 'fr/', localePrefix: 'fr' }不同的路由模式
import { getPrefix, Locales } from "intlayer";// prefix-all: 总是返回前缀getPrefix(Locales.ENGLISH, { mode: "prefix-all", defaultLocale: Locales.ENGLISH,});// 返回: { prefix: '/en', localePrefix: 'en' }// prefix-no-default: 当语言环境与默认相同时不返回前缀getPrefix(Locales.ENGLISH, { mode: "prefix-no-default", defaultLocale: Locales.ENGLISH,});// 返回: { prefix: '', localePrefix: undefined }javascript;// prefix-no-default: 当语言环境与默认语言不同,返回前缀getPrefix(Locales.FRENCH, { mode: "prefix-no-default", defaultLocale: Locales.ENGLISH,});// 返回: { prefix: 'fr/', localePrefix: 'fr' }// no-prefix 和 search-params: 从不返回前缀getPrefix(Locales.ENGLISH, { mode: "no-prefix" });// 返回: { prefix: '', localePrefix: undefined }getPrefix(Locales.ENGLISH, { mode: "search-params" });// 返回: { prefix: '', localePrefix: undefined }实际示例
import { getPrefix, Locales } from "intlayer";// 为特定语言环境构建带有适当前缀的 URLconst locale = Locales.FRENCH;const { prefix, localePrefix } = getPrefix(locale, { defaultLocale: Locales.ENGLISH, mode: "prefix-no-default",});typescript;// 使用 prefix 构建路径const url1 = `/${prefix}about`.replace(/\/+/g, "/");// 结果: "/fr/about"// 使用 localePrefix 进行语言标识console.log(`当前语言环境: ${localePrefix}`);// 输出: "当前语言环境: fr"相关函数
- getLocalizedUrl: 为特定语言环境生成本地化 URL
- getMultilingualUrls: 为所有配置的语言环境生成 URL
TypeScript
type GetPrefixResult = { prefix: string; // 带有尾部斜杠的路径前缀(例如 'fr/' 或 '') localePrefix?: Locale; // 不带斜杠的语言标识符(例如 'fr' 或 undefined)};function getPrefix( locale: Locales, options?: { defaultLocale?: Locales; mode?: "prefix-no-default" | "prefix-all" | "no-prefix" | "search-params"; }): GetPrefixResult;