LanguageHandler

LanguageHandler

Class used to handle interpretation and use of language files. Please load the LanguageHandler before any other handlers to ensure other handlers have access to the loaclised versions of any text they require. Any errors thrown from the LanguageHandler will be in English, as they cannot be localised.

Constructor

# new LanguageHandler()

Members

# defaultLanguage :module:handlers/LanguageHandler~ISOLangCode

ISO Language Code of the language to default to in cases in which the correct language to use cannot be determined.

Type:
  • module:handlers/LanguageHandler~ISOLangCode

Methods

# (static) addLanguageFolder(languageFolder)

Add a folder to scan for language files when language files are loaded.

Parameters:
Name Type Description
languageFolder String

The absolute path to the folder containing language files.

Example
// Add "src/lang" to the list of folders to scan

const { join } = require("path");

LanguageHandler.addLanguageFolder(join(__dirname, "src", "lang"));

# (static) getCommandLocalisations(command) → {module:types~CommandLocalisationObject}

Get the localisations of a command to provide to the Discord API.

Parameters:
Name Type Description
command module:types~Command

The command to retrieve localisations for.

Returns:

The command's localisations.

Type
module:types~CommandLocalisationObject

# (static) getCommandOptionLocalisations(command) → {module:types~CommandOptionLocalisationObject}

Get the localistions of options of a command to provide to the Discord API.

Parameters:
Name Type Description
command module:types~Command

The command to retrieve localisations for.

Returns:

The command's localisations.

Type
module:types~CommandOptionLocalisationObject

# (static) getLocalisation(locale, path, …replaceopt) → {String}

Get localised text.

Parameters:
Name Type Attributes Description
locale module:handlers/LanguageHandler~ISOLangCode

The ISO language code of the locale to use.

path Array.<String>

An array containing the path to the localisation.

replace String <optional>
<repeatable>

Items to place within the localised text.

Returns:

The localised text.

Type
String
Example
// Get a localised logging level name

let name = LanguageHandler.getLocalisation("en", ["console", "logging", "debug"]);

# (static) getLocalisationFromAPILocale(locale, path, …replaceopt) → {String}

Get localised text with a locale defined by a Discord API locale..

Parameters:
Name Type Attributes Description
locale String | module:discord.js~Interaction

The Discord API locale or Discord Interaction.

path Array.<String>

An array containing the path to the localisation.

replace String <optional>
<repeatable>

Items to place within the localised text.

Returns:

The localised text.

Type
String
Example
// Get a localised logging level name

let name = LanguageHandler.getLocalisationFromAPILocale(interaction, ["console", "logging", "debug"]);
let otherName = LanguageHandler.getLocalisationFromAPILocale("en-GB", ["console", "logging", "debug"]);

# (static) loadAllLanguages(loggeropt)

Clear any languages currently loaded and load all languages currently set to be used.

Parameters:
Name Type Attributes Description
logger module:winston~Logger <optional>

The logger being used (to report errors with language files).

Example
// Load all languages in src/lang

const { join } = require("path");

LanguageHandler.addLanguageFolder(join(__dirname, "src", "lang"));
LanguageHandler.loadAllLanguages();

# (static) loadLanguageFile(filepath)

Load an individual language file.

Parameters:
Name Type Description
filepath String

The path to the file to load.

Example
// Load the default "en.lang" language file

const { join } = require("path");

LanguageHandler.loadLanguageFile(join(__dirname, "lang", "en.lang"));