Config

loadDictionary

API reference for the loadDictionary() function.

Overview

loadDictionary loads a translation JSON file for a given locale.

This function is intended for those who wish to use gt-next as a standalone i18n library.

It’s primarily used to migrate existing i18n projects to General Translation while keeping their existing translations. Follow this guide to set this up.

If multiple translations exist, those from dictionaries loaded by loadDictionary always take precedence over others. loadDictionary only supports JSON files with string translations.

Reference

Parameters

Prop

Type

Description

TypeDescription
localeThe locale for which translations should be loaded.

Returns

A Promise<any> that resolves to a dictionary mapping IDs to translations for the given locale.


Setup

Generally, you’ll load the dictionary from the ./public/locales directory.

Define loadDictionary as the default export of a file named loadDictionary.js or loadDictionary.ts, either in the src/ directory or at the project root. Ensure the function returns a promise that resolves to an object containing translations for the given locale.

src/loadDictionary.js
export default async function loadDictionary(locale) {
  const translations = await import(`../public/locales/${locale}.json`);
  return translations.default;
}

Question: What's the difference between loadTranslations and loadDictionary?

  • loadTranslations is used to define custom loading behaviour for fetching translations for your app. This could be getting translations from a CDN, a database, or your app's bundle. These are usually machine-generated translations, managed by the CLI tool, and not very user-friendly to edit.
  • loadDictionary is intended for implementations of gt-next as a standalone library. Users bring their own translations and no translation infrastructure is used.

Notes

  • loadDictionary is used to load custom translations for your app.
  • Dictionaries loaded by loadDictionary will take precedence over translations loaded by loadTranslations.

Next steps

How is this guide?

loadDictionary