Skip to main content

Configuration

NextGlobeGen can be configured through an i18n.config.ts file in the root of your project directory with a default export.

i18n.config.ts
import { Config } from "next-globe-gen";

const config: Config = {
/* config options here */
};

export default config;

The location of the configuration file can be altered by giving the path to the configuration file as an argument to the NextGlobeGen plugin in next.config.ts file.

next.config.ts
import type { NextConfig } from "next";
import { withNextGlobeGenPlugin } from "next-globe-gen/plugin";

const withNextGlobeGen = withNextGlobeGenPlugin("./configs/next-globe-gen.ts");

const nextConfig: NextConfig = {
/* Next.js config options here */
};

export default withNextGlobeGen(nextConfig);

Options

This section documents all the available configuration options.

locales (required)

An array of locales that the application supports. At least one locale needs to be provided.

const config: Config = {
locales: ["en", "fi"],
};

defaultLocale (required)

A locale that will be used as a default locale. Should be one of the provided locales.

const config: Config = {
locales: ["en", "fi"],
defaultLocale: "en",
};

Routes

Options under routes namespace affect how localized routes are generated.

prefixDefaultLocale

A boolean value indicating if the default locale version of the site should be served from locale-prefixed path or from the root.

const config: Config = {
routes: {
prefixDefaultLocale: false, // Default: true
},
};

originDir

A path to a directory where the origin routes are located. The NextGlobeGen pugin watches this directory for changes and regenerates the routes when necessary.

const config: Config = {
routes: {
originDir: "./_app", // Default: "./src/_app"
},
};

localizedDir

A path to a directory where the localized routes will be generated.

const config: Config = {
routes: {
localizedDir: "./app/(i18n)", // Default: "./src/app/(i18n)"
},
};

skipLanguageAlternatesMetadata

NextGlobeGen package injects language alternates to the metadata objects exported or returned by generateMetadata functions for all page routing files.

Option skipLanguageAlternatesMetadata is used to configure if the automatic alternate language URLs metadata generation should be skipped or not.

const config: Config = {
routes: {
skipLanguageAlternatesMetadata: true, // Default: false
},
};

Messages

Options under messages namespace affect how messages are loaded and handled in the application.

originDir

A path to a directory where the message files are located. The NextGlobeGen pugin watches this directory for changes and recompiles the messages when necessary.

const config: Config = {
messages: {
originDir: "./messages", // Default: "./src/messages"
},
};

getMessages

An async function which handles loading the messages. The getMessages function must return a possibly deeply nested object with message translations as values.

const config: Config = {
messages: {
async getMessages(locale) {
/* fetch messages for example from an external provider */
},
},
};
Default message loading

By default messages are loaded from <locale>.json and <locale>/<filename>.json files from the directory specified by the originDir option.

The messages from the <locale>/<filename>.json file are put under <filename> namespace. An expection to this rule are the messages from the <locale>/index.json file, which are not put under any namespace.

YAML Support

The package supports also messages in YAML files using .yaml or .yml extensions.

clientKeys

A regular expression or an array of regular expressions that filter out which messages are provided to the client. A message will be provided to the client if it's key matches to any of the given regular expressions.

const config: Config = {
messages: {
// Provide only messages in the `client` namespace to the client
clientKeys: /^client\./,
},
};
Message key

Key of the message is it's full object dot notation syntax as a string, possibly prefixed with a filename namespace. (i.e. filename.deeply.nested.object.key)

The clientKeys option allows full regex so excluding some of the messages is also possible using negative lookaheads.

By default all messages are sent to the client. If you want to exclude all messages from being provided to the client, use an empty array.

Reducing payload

This option is helpful if you would like to reduce the payload of the responses by sending fewer of the messages to the client.