戻る

gt-node@0.3.0

Ernest McCarter avatarErnest McCarter
gt-nodev0.3.0nodeロケールhelpersi18n

概要

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); // '日本語'