Back

gt-node@0.3.0

Ernest McCarter avatarErnest McCarter
gt-nodev0.3.0nodelocalehelpersi18n

Overview

gt-node now exports locale utility functions: getLocale(), getDefaultLocale(), getLocales(), and getLocaleProperties().

These already existed in gt-next and gt-react. Adding them to gt-node brings server-side parity so you can access locale information without reaching into the i18n manager directly.

What's New

getLocale()

Returns the current request locale.

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()

Returns the default locale set in initializeGT.

import { getDefaultLocale } from 'gt-node';

console.log(getDefaultLocale()); // 'en-US'

getLocales()

Returns the list of supported locales.

import { getLocales } from 'gt-node';

console.log(getLocales()); // ['en-US', 'es', 'fr']

getLocaleProperties()

Returns metadata about a locale — name, native name, region, script, etc.

import { getLocaleProperties } from 'gt-node';

const props = getLocaleProperties('ja');
console.log(props.nativeName); // '日本語'