Documentation for lang.ts
Overview
The lang.ts file is a TypeScript module responsible for translating interface strings based on given language codes. It currently supports two languages: German (denoted by "ger") and English (denoted by "eng"). This functionality is essential for applications that require multi-language support, especially in user interfaces where users can choose their preferred language.
Table of Contents
Function Description
The primary function in this module is:
export default function text(lang: string, input: string): string
- Purpose: This function takes in a language code and an input key and returns the corresponding translated string.
- Parameters:
lang: A string representing the language code ("ger" for German, "eng" for English).input: A string representing a key that needs to be translated.
- Returns: A string that is the translation of the input key in the specified language. If no translation is available, it returns the input key itself or an error message if the language is not supported.
Supported Languages
This module supports translation for two languages:
| Language Code | Language |
|---|---|
ger |
German |
eng |
English |
Key Translations
The module includes translations for several interface components and messages. Below is a list of some key translations:
| Key | English Translation | German Translation |
|---|---|---|
header_title |
OR-Tool | OR-Tool |
header_subtitle |
by Spaceholder Programming | von Spaceholder Programming |
boxObjTitle |
Objective | Ziel |
GmplHeader |
General Linear Problems | Allgemeine Lineare Probleme |
FileUpload |
Upload File | Datei hochladen |
buttonCalc |
Calculate | Berechnen |
err_invalidInput |
Error: Invalid input in | Fehler: Ungültige Eingabe in |
calculating |
Calculating... | Berechne... |
maximize |
Maximize | Maximieren |
minimize |
Minimize | Minimieren |
Example Usage
To use the text function, simply import it in your TypeScript file and call it with the desired language code and input key.
import text from './lang';
// Example: Getting the German translation for the "Calculate" button
const germanCalculateButton = text("ger", "buttonCalc");
console.log(germanCalculateButton); // Outputs: "Berechnen"
// Example: Getting the English translation for the "Objective" title
const englishObjectiveTitle = text("eng", "boxObjTitle");
console.log(englishObjectiveTitle); // Outputs: "Objective"
Error Handling
The function is designed to handle various error scenarios:
-
Unknown Language: If the language code provided is not recognized, the function will return an error message:
return "Error: Translation Module - Language Not Known."; -
Invalid Input Key: If the input key does not have a corresponding translation, the function returns the input key itself. This acts as a fallback mechanism to prevent the application from crashing due to missing translations.
-
Redundant Error Message Key: It is noted that the
err_invalidInputkey is duplicated in the code. This should be addressed to ensure consistent error messaging.
Spaceholder-Programming 2024.