General Translation  
Next.jsHelpers

getLocale()

API Reference for the getLocale server-side method

Overview

The getLocale() function retrieves the user's current locale during server-side rendering. The returned locale is formatted as a string, e.g., 'en-US'.

getLocale() is a server-side method and can only be used on server-side components.

For client-side usage, see useLocale().

Reference

Returns

A promise that resolves to a string representing the user's current locale, e.g., 'en-US'.


Fallback behavior

When an unsupported locale is requested, a fallback locale will be selected.

For instance, in the event of an unsupported locale, if (1) the user has configured multiple preferred locales in their browser settings, and (2) one of these locales are supported by your application, then the locale will fallback to the best language.

Additionally, if no possible fallback locales are available, but two locales share the same language (e.g., en-US and en-GB), then the locale will fallback to the supported locale that shares the same language.

If neither condition can be met, then the the default locale will be used.

See initGT() for information on configuring supported locales.


Examples

Basic Usage

Retrieve the user's locale during server-side rendering.

GetUserLocale.jsx
import getLocale from 'gt-next/server';
 
export default async function GetUserLocale() {
    const locale = await getLocale(); 
    return <p>User locale: {locale}</p>;
}

Notes

  • The getLocale() function is asynchronous and must be awaited to retrieve the locale.
  • It is specifically designed for server-side use. See useLocale() for client-side components.
  • The returned locale adheres to the locale strings format.

Next Steps

On this page