gt-node@0.3.0
概述
gt-node 现已导出区域设置实用函数:getLocale()、getDefaultLocale()、getLocales() 和 getLocaleProperties()。
这些函数原本已在 gt-next 和 gt-react 中提供。现在将它们加入 gt-node,实现了服务端功能一致,因此你无需直接访问 i18n 管理器即可获取区域设置信息。
最新更新
getLocale()
返回当前请求的区域设置。
import { withGT, getLocale } from 'gt-node';
app.use((req, res, next) => {
const locale = req.headers['accept-language']?.split(',')[0] || 'en-US';
withGT(locale, () => {
console.log(getLocale()); // 'es'
next();
});
});getDefaultLocale()
返回在 initializeGT 中设置的默认区域设置。
import { getDefaultLocale } from 'gt-node';
console.log(getDefaultLocale()); // 'en-US'getLocales()
返回支持的区域设置列表。
import { getLocales } from 'gt-node';
console.log(getLocales()); // ['en-US', 'es', 'fr']getLocaleProperties()
返回某个区域设置的元数据,如名称、本地名称、地区、书写系统等。
import { getLocaleProperties } from 'gt-node';
const props = getLocaleProperties('ja');
console.log(props.nativeName); // '日本語'