@godgpt/i18n
v0.1.1
Published
Internationalization module for GodGPT SDK with lazy loading
Readme
@godgpt/i18n
Internationalization (i18n) module for the GodGPT SDK with interpolation and pluralization.
Installation
npm install @godgpt/core @godgpt/i18n
# or
pnpm add @godgpt/core @godgpt/i18n
# or
yarn add @godgpt/core @godgpt/i18nUsage
Basic Setup
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
// en.json
{
"hello": "Hello",
"greeting": "Hello, {{name}}!",
"items_zero": "No items",
"items_one": "1 item",
"items_other": "{{count}} items"
}Basic Translation
// 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
i18n.t("items", { count: 0 }); // "No items"
i18n.t("items", { count: 1 }); // "1 item"
i18n.t("items", { count: 5 }); // "5 items"Changing Locale
// 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")); // falsePreloading Locales
// Preload translations for faster switching
await i18n.preload("es");
await i18n.preload("zh-CN");
// Check if loaded
console.log(i18n.isLoaded("es")); // trueAdding Translations at Runtime
i18n.addResources("en", {
"new.key": "New translation",
"another.key": "Another one",
});i18n Events
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 localesupportedLocales(string[]): List of supported localesfallbackLocale(string): Fallback when translation missingloadResources(function): Async function to load translationscacheResources(boolean): Cache loaded translations (default: true)
Methods
t(key, options?): Translate a keygetLocale(): Get current localesetLocale(locale): Change localegetSupportedLocales(): Get all supported localesisSupported(locale): Check if locale is supportedisLoaded(locale): Check if locale translations are loadedpreload(locale): Preload translations for a localeaddResources(locale, resources): Add translations at runtimeexists(key): Check if a translation key existsgetResource(key): Get raw translation value
Translation Options
defaultValue(string): Fallback if key not foundcount(number): For pluralization- Any other keys for interpolation (e.g.,
{ name: "John" })
License
MIT
