Utility FunctionsFormatting

formatMessage

API-Referenz für die eigenständige Funktion formatMessage

Überblick

Die Methode formatMessage formatiert Meldungen mit Variablenersetzung und lokalisierungsbewusster Formatierung. Auf Basis der Format.JS‑Bibliothek intl-messageformat unterstützt sie Muster des ICU Message Format.

Diese Methode ist essenziell für Variableninterpolation und Pluralbildung. Außerdem unterstützt sie Zahlen- und Datumsformatierung 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

EigenschaftTypOptionalStandardwertBeschreibung
localesstring | string[]['en']Zu verwendende locale(s) für die Formatierung
variablesFormatVariables{}Objekt mit Variablen zur Interpolation

Rückgabewert

string - Die formatierte Nachricht mit ersetzten Variablen und angewandter localespezifischer Formatierung.


Verhalten

Locale-Verarbeitung

  • Verwendet den Parameter locales zur Formatierung
  • Fällt auf ['en'] zurück, wenn keine locales angegeben sind
  • Unterstützt Fallback-Ketten für Locales mit Arrays

Variablenverarbeitung

  • Einfache Ersetzung: {variable} → durch value ersetzt
  • ICU-Nachrichtenformat: Vollständige Unterstützung für Plurale, Selects und Formatierungen
  • Fehlende Variablen: Im Output unverändert belassen

Unterstützung des Message-Formats

Dieselben Funktionen des ICU-Message-Formats wie bei der GT-Klasse-Methode:

  • 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: 'World' }
});
console.log(greeting); // "Hallo Welt!"

Ohne GT-Instance

// Hilfsfunktion zum Formatieren ohne Klasseninstanz
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"

Zahlen- und Währungsformatierung

// Deutsches Locale-Format
const germanPrice = formatMessage('Preis: {price, number, currency}', {
  locales: ['de'],
  variables: { price: 1234.56 }
});
console.log(germanPrice); // "Preis: 1.234,56 €"

// Prozentformatierung
const progress = formatMessage('Progress: {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 locale-Angabe in options
  • Unterstützt dieselben ICU message format‑Funktionen wie die GT-Klasse
  • Gibt für leere Eingabevorlagen einen leeren String zurück
  • Variablen werden vor den ICU-Formatierungsregeln verarbeitet

Nächste Schritte

Wie ist diese Anleitung?