戻る

gt-node@0.3.0

Ernest McCarter avatarErnest McCarter
gt-nodev0.3.0nodelocalehelpersi18n

概要

gt-node で、ロケール用ユーティリティ関数 getLocale(), getDefaultLocale(), getLocales(), および 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); // '日本語'