AkiLocale
A localization library for Akinon applications. Wraps i18next with a simplified API for managing translations.
Installation
pnpm add @akinon/akilocaleBasic 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
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
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
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
initialLanguageoption (if provided)localStorage value (if exists)
fallbackLngoption
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
JSON Files (Recommended)
TypeScript Files
Best Practices
Create a single instance - Initialize locale once at app startup and export
tUse JSON files - Store translations in JSON for easy editing and tooling support
Consistent key naming - Use dot notation or nested structure, but be consistent
Sync with Akidate - Call
akidate.setLocale(locale.lng)after initializationHandle missing translations - Missing keys return the key itself, useful for development
Reload on language change - Call
window.location.reload()aftersetLanguagefor full effect
Related
Akidate - Date formatting with locale support
Last updated
Was this helpful?

