formatMessage
API-Referenz zur eigenständigen Funktion formatMessage
Übersicht
Die Methode formatMessage formatiert Meldungen mit Variableneinsetzung und lokalisierungsbewusster Formatierung.
Aufbauend auf der Format.JS‑Bibliothek intl-messageformat unterstützt sie Muster im ICU message format.
Diese Methode ist essenziell für die Variableninterpolation und die Pluralbildung. Außerdem unterstützt sie die Formatierung von Zahlen und Datumsangaben sowie weitere Funktionen.
import { formatMessage } from 'generaltranslation';
const formatted = formatMessage('Hallo {name}, du hast {count} Nachrichten', {
  locales: ['en'],
  variables: { name: 'Alice', count: 5 }
});
// Gibt zurück: „Hallo Alice, du hast 5 Nachrichten“Referenzen
Parameter
Prop
Type
Options-Objekt
| Property | Type | Optional | Default | Description | 
|---|---|---|---|---|
| locales | string | string[] | ✓ | ['en'] | Zu verwendende Locale(s) für die Formatierung | 
| variables | FormatVariables | ✓ | {} | Objekt mit variables für die Interpolation | 
Rückgabewert
string - Die formatierte Nachricht mit ersetzten variables und lokalespezifischer Formatierung.
Verhalten
Locale-Handhabung
- Verwendet den angegebenen locales-Parameter für die Formatierung
- Fällt auf ['en']zurück, wenn keinelocalesangegeben sind
- Unterstützt Fallback-Ketten für Locales mittels Arrays
Variablenverarbeitung
- Einfache Ersetzung: {variable}→ durch den Wert ersetzt
- ICU message format: Vollständige Unterstützung für Pluralformen, Auswahlregeln und Formatierung
- Fehlende Variablen: Im Ausgabestring unverändert belassen
Unterstützung für Message Format
Dieselben ICU-Message-Format-Funktionen wie bei der GT-Klassenmethode:
- Zahlenformatierung: {price, number, currency}
- Datumsformatierung: {date, date, short}
- Pluralbildung: {count, plural, ...}
- Auswahl: {gender, select, ...}
Beispiele
Grundlegende Verwendung
import { formatMessage } from 'generaltranslation';
const greeting = formatMessage('Hallo {name}!', {
  locales: ['en'],
  variables: { name: 'Welt' }
});
console.log(greeting); // „Hallo Welt!“Ohne GT-Instance
// Dienstprogramm-Funktion zum Formatieren ohne Klasseninstanziierung
function quickFormat(template: string, variables: any, locale = 'en') {
  return formatMessage(template, {
    locales: [locale],
    variables
  });
}
const notification = quickFormat(
  'Sie haben {count, plural, =0 {keine Nachrichten} =1 {eine Nachricht} other {# Nachrichten}}',
  { count: 3 },
  'en'
);
console.log(notification); // „Sie haben 3 Nachrichten“Währungs- und Zahlenformatierung
// Deutsche Formatierung (Locale)
const germanPrice = formatMessage('Preis: {price, number, currency}', {
  locales: ['de'],
  variables: { price: 1234.56 }
});
console.log(germanPrice); // "Preis: 1.234,56 €"
// Prozentformatierung
const progress = formatMessage('Fortschritt: {percent, number, percent}', {
  locales: ['en'],
  variables: { percent: 0.85 }
});
console.log(progress); // "Fortschritt: 85%"Vorlagenbibliothek
import { formatMessage } from 'generaltranslation';
class MessageTemplates {
  private locale: string;
  
  constructor(locale: string = 'en') {
    this.locale = locale;
  }
  
  welcome(name: string) {
    return formatMessage('Willkommen zurück, {name}!', {
      locales: [this.locale],
      variables: { name }
    });
  }
  
  itemCount(count: number) {
    return formatMessage(
      '{count, plural, =0 {Keine Elemente} =1 {Ein Element} other {# Elemente}}',
      {
        locales: [this.locale],
        variables: { count }
      }
    );
  }
}
const templates = new MessageTemplates('fr');
console.log(templates.welcome('Marie')); // „Willkommen zurück, Marie!“ (auf Französisch)
console.log(templates.itemCount(5)); // „5 Elemente“Hinweise
- Erfordert eine explizite Angabe der locale in den options
- Unterstützt dieselben Funktionen des ICU message format wie die Methode der GT-Klasse
- Gibt für leere Eingabevorlagen einen leeren String zurück
- variables werden vor den ICU-Formatierungsregeln verarbeitet
Nächste Schritte
- Sieh dir die Dokumentation zu Intl.MessageFormatfür weitere options an
- Verwende die GT-Klasse formatMessagefür zustandsbehaftete Formatierung
- Formatiere Zahlen mit der eigenständigen Funktion formatNum
- Erfahre mehr über den Typ FormatVariables
Wie ist dieser Leitfaden?

