返回

gt-node@0.3.0

Ernest McCarter avatarErnest McCarter
gt-nodev0.3.0nodelocalehelpersi18n

概述

gt-node 现在会导出区域设置工具函数:getLocale(), getDefaultLocale(), getLocales(), and getLocaleProperties()

这些函数此前已存在于 gt-nextgt-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); // '日本語'