Skip to content

@web-engine-dev/i18n

Game localization system for web-engine-dev.

Features

  • String Localization: Translate text strings
  • Pluralization: Language-aware plurals
  • Date/Number Formatting: Locale formatting
  • Asset Localization: Localized images/audio
  • Hot Swap: Change language at runtime
  • Fallbacks: Missing translation handling

Installation

bash
npm install @web-engine-dev/i18n
# or
pnpm add @web-engine-dev/i18n

Quick Start

typescript
import { I18n, t } from '@web-engine-dev/i18n';

// Initialize
const i18n = new I18n({
  defaultLocale: 'en',
  fallbackLocale: 'en',
});

// Load translations
await i18n.loadLocale('en', {
  greeting: 'Hello, {name}!',
  score: 'Score: {score}',
  items: '{count, plural, one {# item} other {# items}}',
});

await i18n.loadLocale('es', {
  greeting: '¡Hola, {name}!',
  score: 'Puntuación: {score}',
});

// Set locale
i18n.setLocale('es');

// Translate
console.log(t('greeting', { name: 'Player' })); // ¡Hola, Player!

API Overview

Basic Translation

typescript
// Simple string
t('menu.start');

// With variables
t('greeting', { name: playerName });

// With count
t('enemies', { count: 5 }); // "5 enemies"

Pluralization

typescript
// Translation file
{
  "lives": "{count, plural, =0 {No lives} one {# life} other {# lives}}"
}

// Usage
t('lives', { count: 0 }); // "No lives"
t('lives', { count: 1 }); // "1 life"
t('lives', { count: 3 }); // "3 lives"

Formatting

typescript
// Numbers
i18n.formatNumber(1234.56); // "1,234.56" (en) or "1.234,56" (de)

// Currency
i18n.formatCurrency(9.99, 'USD'); // "$9.99" (en)

// Dates
i18n.formatDate(date, 'long'); // "January 1, 2024"

// Relative time
i18n.formatRelative(-5, 'day'); // "5 days ago"

Locale Management

typescript
// Get current locale
const locale = i18n.getLocale();

// Get available locales
const locales = i18n.getAvailableLocales();

// Check if locale supported
i18n.isLocaleSupported('ja');

// Switch locale
await i18n.setLocale('fr');

// Listen for changes
i18n.on('localeChange', (newLocale) => {
  refreshUI();
});

Asset Localization

typescript
// Get localized asset path
const path = i18n.getAssetPath('images/title.png');
// Returns: "images/title_ja.png" if locale is 'ja'

// Register localized assets
i18n.registerAsset('images/title.png', {
  en: 'images/title_en.png',
  ja: 'images/title_ja.png',
  default: 'images/title_en.png',
});

Proprietary software. All rights reserved.