Skip to content

Localization Setup

A complete localization system for Vue 3 covering locale-aware formatting (l10n), translations (i18n), and timezone detection. Provided by the optional @cocoar/vue-localization package.

Separate Package

The localization system is shipped separately from @cocoar/vue-ui to keep the core bundle lean. Install it only when you need it.

bash
pnpm add @cocoar/vue-localization

Setup

Register the plugin in your app entry point. The createCoarLocalization() factory creates both the plugin and the underlying service instance.

ts
// main.ts
import { createApp } from 'vue';
import { createCoarLocalization } from '@cocoar/vue-localization';
import App from './App.vue';

const app = createApp(App);

app.use(createCoarLocalization({
  defaultLanguage: 'en',
  // Optional: load locale data from your server
  l10nUrl: (lang) => `/locales/${lang}.json`,
  // Optional: load translations from your server
  i18nUrl: (lang) => `/i18n/${lang}.json`,
}));

app.mount('#app');

The defaultLanguage is loaded automatically on startup using the browser's Intl API as a data source. No JSON files are needed for basic formatting -- the system derives number separators, date patterns, currency symbols, and more directly from Intl.

Configuration Options

OptionTypeDefaultDescription
defaultLanguagestring'en'Initial language code
l10nUrl(lang: string) => stringundefinedURL builder for locale data JSON (overrides Intl defaults)
i18nUrl(lang: string) => stringundefinedURL builder for translation JSON
timezoneProvidersCoarTimezoneProvider[][]Custom timezone providers (checked before browser default)

Changing Language at Runtime

Use the service directly to switch languages. Data is loaded on demand and cached.

ts
import { useLocalization } from '@cocoar/vue-localization';

const service = useLocalization()!;

// Switch language (loads locale data + translations if not cached)
await service.setLanguage('de');

// Preload without switching (useful for instant switching later)
await service.preloadLanguage('fr');

// Force reload from all sources (e.g. after server-side data changes)
await service.reloadLanguage();

Service API

The useLocalization() composable returns the full CoarLocalizationService instance for advanced use cases.

Method / PropertyTypeDescription
languageRef<string>Current language (reactive)
loadingRef<boolean>Whether data is currently being loaded
localeDataComputedRef<CoarLocalizationData | undefined>Locale data for the current language
setLanguage(lang)(string) => Promise<void>Switch language and load data
preloadLanguage(lang)(string) => Promise<void>Preload data without switching
reloadLanguage(lang?)(string?) => Promise<void>Force reload data from all sources
t(key, params?, fallback?)(string, Record?, string?) => stringTranslate a key
getDefaultLanguage()() => stringGet the configured default language
i18nStoreCoarTranslationStoreDirect access to the translation store
l10nStoreCoarLocalizationDataStoreDirect access to the locale data store
timezoneServiceCoarTimezoneServiceDirect access to the timezone service
addLocaleDataSource(source)(CoarLocaleDataSource) => voidAdd a custom locale data source
addTranslationSource(source)(CoarTranslationSource) => voidAdd a custom translation source

Released under the Apache-2.0 License.