npm package: @godgpt/i18n
Description: Internationalization module for GodGPT SDK with lazy loading
Author: GodGPT
Latest version: 0.1.1

# @godgpt/i18n

Internationalization (i18n) module for the GodGPT SDK with interpolation and pluralization.

## Installation

```bash
npm install @godgpt/core @godgpt/i18n
# or
pnpm add @godgpt/core @godgpt/i18n
# or
yarn add @godgpt/core @godgpt/i18n
```

## Usage

### Basic Setup

```typescript
import { createSDK } from "@godgpt/core";
import { I18nModule } from "@godgpt/i18n";

const sdk = createSDK();
const i18n = sdk.use(
  new I18nModule({
    defaultLocale: "en",
    supportedLocales: ["en", "es", "zh-CN"],
    fallbackLocale: "en",
    loadResources: async (locale) => {
      // Load translations from API, file, etc.
      const response = await fetch(`/locales/${locale}.json`);
      return response.json();
    },
  })
);

await sdk.init();
```

### Translation Files

```json
// en.json
{
  "hello": "Hello",
  "greeting": "Hello, {{name}}!",
  "items_zero": "No items",
  "items_one": "1 item",
  "items_other": "{{count}} items"
}
```

### Basic Translation

```typescript
// Simple translation
i18n.t("hello"); // "Hello"

// With interpolation
i18n.t("greeting", { name: "World" }); // "Hello, World!"

// With default value
i18n.t("missing.key", { defaultValue: "Fallback" }); // "Fallback"
```

### Pluralization

```typescript
i18n.t("items", { count: 0 }); // "No items"
i18n.t("items", { count: 1 }); // "1 item"
i18n.t("items", { count: 5 }); // "5 items"
```

### Changing Locale

```typescript
// Get current locale
console.log(i18n.getLocale()); // "en"

// Change locale
await i18n.setLocale("es");

// Check supported locales
console.log(i18n.getSupportedLocales()); // ["en", "es", "zh-CN"]
console.log(i18n.isSupported("fr")); // false
```

### Preloading Locales

```typescript
// Preload translations for faster switching
await i18n.preload("es");
await i18n.preload("zh-CN");

// Check if loaded
console.log(i18n.isLoaded("es")); // true
```

### Adding Translations at Runtime

```typescript
i18n.addResources("en", {
  "new.key": "New translation",
  "another.key": "Another one",
});
```

### i18n Events

```typescript
sdk.on("i18n:ready", () => {
  console.log("i18n is ready");
});

sdk.on("i18n:localeChanged", ({ locale, previousLocale }) => {
  console.log(`Locale changed from ${previousLocale} to ${locale}`);
});
```

## API Reference

### I18nModule Constructor Options

- `defaultLocale` (string): Initial locale
- `supportedLocales` (string[]): List of supported locales
- `fallbackLocale` (string): Fallback when translation missing
- `loadResources` (function): Async function to load translations
- `cacheResources` (boolean): Cache loaded translations (default: true)

### Methods

- `t(key, options?)`: Translate a key
- `getLocale()`: Get current locale
- `setLocale(locale)`: Change locale
- `getSupportedLocales()`: Get all supported locales
- `isSupported(locale)`: Check if locale is supported
- `isLoaded(locale)`: Check if locale translations are loaded
- `preload(locale)`: Preload translations for a locale
- `addResources(locale, resources)`: Add translations at runtime
- `exists(key)`: Check if a translation key exists
- `getResource(key)`: Get raw translation value

### Translation Options

- `defaultValue` (string): Fallback if key not found
- `count` (number): For pluralization
- Any other keys for interpolation (e.g., `{ name: "John" }`)

## License

MIT