Helpers
getDefaultLocale
getDefaultLocale 服务端方法的 API 参考
概览
getDefaultLocale 函数用于获取应用的默认 locale。
该 locale 在服务器启动时确定,并作为翻译的回退语言使用。
它返回一个locale 字符串,例如 'en-US'。
getDefaultLocale 是一个服务端方法,只能在服务端组件中使用。
配置请参见 withGTConfig。
如果在 withGTConfig 中未指定默认 locale,则默认为 'en-US'。
客户端请参见 useDefaultLocale。
参考
返回值
表示应用默认 locale 的字符串,例如 'en-US'。
示例
基本用法
获取应用的默认 locale,并在服务器端逻辑中使用它。
import getDefaultLocale from 'gt-next/server';
export default function GetDefaultLocale() {
const defaultLocale = getDefaultLocale();
return <p>默认语言区域:{defaultLocale}</p>;
}注意事项
getDefaultLocale返回在withGTConfig中设置的默认 locale。getDefaultLocale仅在 server-side 可用。- 返回的 locale 遵循语言环境代码字符串的格式。
后续步骤
本指南如何?