AkiLocale

A localization library for Akinon applications. Wraps i18next with a simplified API for managing translations.

Installation

pnpm add @akinon/akilocale

Basic Usage

import { Akilocale } from '@akinon/akilocale';

// Create a locale instance with translations
const locale = Akilocale.createInstance({
  fallbackLng: 'en',
  translations: {
    en: {
      greeting: 'Hello',
      welcome: 'Welcome, {{name}}!'
    },
    tr: {
      greeting: 'Merhaba',
      welcome: 'Hoş geldin, {{name}}!'
    }
  }
});

// Use the translation function
locale.t('greeting'); // "Hello" or "Merhaba" based on current language
locale.t('welcome', { name: 'John' }); // "Welcome, John!"

// Get current language
console.log(locale.lng); // "en"

API Reference

Akilocale.createInstance

Creates a new locale instance with the provided translations.

Options

Option
Type
Default
Description

translations

Record<string, Translations>

-

Translation objects keyed by language code

fallbackLng

string

'en'

Fallback language when translation is missing

initialLanguage

string

undefined

Initial language (overrides localStorage)

debug

boolean

false

Enable debug logging

Returns: AkilocaleInstance

Property
Type
Description

t

(key: string, options?: TOptions) => string

Translation function

lng

string

Current language code


Akilocale.setLanguage

Persists the selected language to localStorage. Requires page reload to take effect.

Parameters

Parameter
Type
Description

lng

string

Language code to set


Translation Function (t)

The t function translates keys with optional interpolation.


Translation Structure

Translations can be flat or nested objects:

React Integration

For React applications, use the /react export which provides react-i18next integration:


Language Persistence

Akilocale automatically persists language selection to localStorage:

Language Priority

  1. initialLanguage option (if provided)

  2. localStorage value (if exists)

  3. fallbackLng option


Common Use Cases

App Initialization

Language Switcher

Integration with Akidate

Keep Akidate in sync with Akilocale for consistent date formatting:

With Intl APIs

Use locale.lng for native JavaScript internationalization:

Table with Translations


Translation File Organization

TypeScript Files


Best Practices

  1. Create a single instance - Initialize locale once at app startup and export t

  2. Use JSON files - Store translations in JSON for easy editing and tooling support

  3. Consistent key naming - Use dot notation or nested structure, but be consistent

  4. Sync with Akidate - Call akidate.setLocale(locale.lng) after initialization

  5. Handle missing translations - Missing keys return the key itself, useful for development

  6. Reload on language change - Call window.location.reload() after setLanguage for full effect

  • Akidate - Date formatting with locale support

Last updated

Was this helpful?