๐Ÿ“… let's chat! explore the endless possibilities creating industries that don't exist. click here

lokalise-reference-architecture

Implement Lokalise reference architecture with best-practice project layout. Use when designing new Lokalise integrations, reviewing project structure, or establishing architecture standards for Lokalise applications. Trigger with phrases like "lokalise architecture", "lokalise best practices", "lokalise project structure", "how to organize lokalise", "lokalise layout". allowed-tools: Read, Grep version: 1.0.0 license: MIT author: Jeremy Longshore <jeremy@intentsolutions.io>

Allowed Tools

No tools specified

Provided by Plugin

lokalise-pack

Claude Code skill pack for Lokalise (24 skills)

saas packs v1.0.0
View Plugin

Installation

This skill is included in the lokalise-pack plugin:

/plugin install lokalise-pack@claude-code-plugins-plus

Click to copy

Instructions

# Lokalise Reference Architecture ## Overview Production-ready architecture patterns for Lokalise integrations. ## Prerequisites - Understanding of layered architecture - Lokalise SDK knowledge - TypeScript project setup - Testing framework configured ## Project Structure ``` my-app/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ i18n/ โ”‚ โ”‚ โ”œโ”€โ”€ index.ts # i18n library setup โ”‚ โ”‚ โ”œโ”€โ”€ config.ts # Lokalise configuration โ”‚ โ”‚ โ”œโ”€โ”€ types.ts # TypeScript types โ”‚ โ”‚ โ””โ”€โ”€ loaders/ โ”‚ โ”‚ โ”œโ”€โ”€ static.ts # Bundled translations โ”‚ โ”‚ โ”œโ”€โ”€ dynamic.ts # Runtime loading โ”‚ โ”‚ โ””โ”€โ”€ ota.ts # Over-the-air (mobile) โ”‚ โ”œโ”€โ”€ services/ โ”‚ โ”‚ โ””โ”€โ”€ lokalise/ โ”‚ โ”‚ โ”œโ”€โ”€ index.ts # Service facade โ”‚ โ”‚ โ”œโ”€โ”€ client.ts # Lokalise client wrapper โ”‚ โ”‚ โ”œโ”€โ”€ cache.ts # Caching layer โ”‚ โ”‚ โ”œโ”€โ”€ sync.ts # Translation sync โ”‚ โ”‚ โ””โ”€โ”€ webhooks.ts # Webhook handlers โ”‚ โ”œโ”€โ”€ locales/ โ”‚ โ”‚ โ”œโ”€โ”€ en.json # English (source) โ”‚ โ”‚ โ”œโ”€โ”€ es.json # Spanish โ”‚ โ”‚ โ”œโ”€โ”€ fr.json # French โ”‚ โ”‚ โ””โ”€โ”€ index.ts # Locale exports โ”‚ โ””โ”€โ”€ api/ โ”‚ โ””โ”€โ”€ webhooks/ โ”‚ โ””โ”€โ”€ lokalise.ts # Webhook endpoint โ”œโ”€โ”€ scripts/ โ”‚ โ”œโ”€โ”€ lokalise-pull.sh # Download translations โ”‚ โ”œโ”€โ”€ lokalise-push.sh # Upload source strings โ”‚ โ””โ”€โ”€ check-translations.ts # Validation script โ”œโ”€โ”€ tests/ โ”‚ โ”œโ”€โ”€ unit/ โ”‚ โ”‚ โ””โ”€โ”€ i18n/ โ”‚ โ””โ”€โ”€ integration/ โ”‚ โ””โ”€โ”€ lokalise/ โ”œโ”€โ”€ config/ โ”‚ โ”œโ”€โ”€ lokalise.development.json โ”‚ โ”œโ”€โ”€ lokalise.staging.json โ”‚ โ””โ”€โ”€ lokalise.production.json โ”œโ”€โ”€ .env.example โ”œโ”€โ”€ lokalise.json # CLI configuration โ””โ”€โ”€ package.json ``` ## Layer Architecture ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Application Layer โ”‚ โ”‚ (React/Vue/Angular Components) โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ i18n Library Layer โ”‚ โ”‚ (i18next, react-intl, vue-i18n) โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Translation Service Layer โ”‚ โ”‚ (Loading, Caching, Fallback Logic) โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Lokalise Layer โ”‚ โ”‚ (SDK Client, Sync, Webhooks) โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Infrastructure Layer โ”‚ โ”‚ (Cache, Queue, Monitoring) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ## Key Components ### Step 1: Lokalise Client Wrapper ```typescript // src/services/lokalise/client.ts import { LokaliseApi } from "@lokalise/node-api"; let instance: LokaliseApi | null = null; export interface LokaliseConfig { apiKey: string; projectId: string; enableCompression?: boolean; } export function getLokaliseClient(config?: Partial): LokaliseApi { if (!instance) { instance = new LokaliseApi({ apiKey: config?.apiKey || process.env.LOKALISE_API_TOKEN!, enableCompression: config?.enableCompression ?? true, }); } return instance; } export function getProjectId(): string { return process.env.LOKALISE_PROJECT_ID!; } // Reset for testing export function resetClient(): void { instance = null; } ``` ### Step 2: Translation Service Facade ```typescript // src/services/lokalise/index.ts import { getLokaliseClient, getProjectId } from "./client"; import { TranslationCache } from "./cache"; import { syncTranslations } from "./sync"; export interface TranslationService { getTranslations(locale: string): Promise>; getKey(locale: string, key: string): Promise; syncFromLokalise(): Promise; invalidateCache(locale?: string): void; } const cache = new TranslationCache(); export const translationService: TranslationService = { async getTranslations(locale) { // Try cache first const cached = cache.get(locale); if (cached) return cached; // Load from bundled files or Lokalise const translations = await loadTranslations(locale); cache.set(locale, translations); return translations; }, async getKey(locale, key) { const translations = await this.getTranslations(locale); return translations[key] ?? null; }, async syncFromLokalise() { await syncTranslations(getProjectId()); }, invalidateCache(locale) { if (locale) { cache.delete(locale); } else { cache.clear(); } }, }; ``` ### Step 3: Translation Loader Pattern ```typescript // src/i18n/loaders/static.ts // For bundled translations (build-time) export async function loadStaticTranslations( locale: string ): Promise> { try { const translations = await import(`../../locales/${locale}.json`); return translations.default; } catch { console.warn(`Locale ${locale} not found, falling back to English`); const fallback = await import("../../locales/en.json"); return fallback.default; } } // src/i18n/loaders/dynamic.ts // For runtime loading (CDN/API) export async function loadDynamicTranslations( locale: string, baseUrl = "/locales" ): Promise> { const response = await fetch(`${baseUrl}/${locale}.json`); if (!response.ok) { console.warn(`Failed to load ${locale}, falling back to English`); return loadDynamicTranslations("en", baseUrl); } return response.json(); } ``` ### Step 4: i18n Library Integration ```typescript // src/i18n/index.ts import i18n from "i18next"; import { initReactI18next } from "react-i18next"; import { loadStaticTranslations } from "./loaders/static"; export const SUPPORTED_LOCALES = ["en", "es", "fr", "de", "ja"]; export const DEFAULT_LOCALE = "en"; export async function initI18n(locale = DEFAULT_LOCALE) { const resources: Record = {}; // Load initial locale resources[locale] = { translation: await loadStaticTranslations(locale), }; // Load English fallback if different if (locale !== "en") { resources.en = { translation: await loadStaticTranslations("en"), }; } await i18n.use(initReactI18next).init({ resources, lng: locale, fallbackLng: "en", supportedLngs: SUPPORTED_LOCALES, interpolation: { escapeValue: false, }, }); return i18n; } // Lazy load additional locales export async function loadLocale(locale: string) { if (i18n.hasResourceBundle(locale, "translation")) { return; } const translations = await loadStaticTranslations(locale); i18n.addResourceBundle(locale, "translation", translations); } ``` ## Data Flow Diagram ``` Developer adds string โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ en.json โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ (source) โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ–ผ โ”‚ git push โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ CI/CD โ”‚ โ–ผ โ”‚ Pipeline โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ Lokalise โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ Project โ”‚ lokalise push โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ Translators work โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Translations โ”‚ โ”‚ Complete โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ Webhook / CI sync โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ App Build โ”‚โ”€โ”€โ”€โ”€โ–ถ Production โ”‚ with i18n โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ## Configuration Management ```typescript // config/lokalise.ts import devConfig from "./lokalise.development.json"; import stagingConfig from "./lokalise.staging.json"; import prodConfig from "./lokalise.production.json"; type Environment = "development" | "staging" | "production"; interface LokaliseEnvConfig { projectId: string; enableWebhooks: boolean; cacheEnabled: boolean; cacheTtlSeconds: number; } const configs: Record = { development: devConfig, staging: stagingConfig, production: prodConfig, }; export function getLokaliseEnvConfig(): LokaliseEnvConfig { const env = (process.env.NODE_ENV || "development") as Environment; return configs[env] || configs.development; } ``` ## Instructions ### Step 1: Create Directory Structure Set up the project layout following the reference structure. ### Step 2: Implement Client Wrapper Create the singleton client with caching support. ### Step 3: Build Translation Service Implement the facade pattern for translation operations. ### Step 4: Integrate i18n Library Connect Lokalise translations to your UI framework. ## Output - Structured project layout - Client wrapper with caching - Translation service facade - i18n library integration ## Error Handling | Issue | Cause | Solution | |-------|-------|----------| | Circular imports | Wrong layering | Separate by layer | | Missing locale | Not bundled | Add fallback logic | | Stale translations | Cache not invalidated | Use webhooks to invalidate | | Type errors | Missing types | Generate from source locale | ## Examples ### Quick Setup Script ```bash #!/bin/bash # Create reference structure mkdir -p src/i18n/loaders mkdir -p src/services/lokalise mkdir -p src/locales mkdir -p scripts mkdir -p config mkdir -p tests/{unit,integration}/lokalise touch src/i18n/{index,config,types}.ts touch src/i18n/loaders/{static,dynamic,ota}.ts touch src/services/lokalise/{index,client,cache,sync,webhooks}.ts touch scripts/{lokalise-pull.sh,lokalise-push.sh,check-translations.ts} touch config/lokalise.{development,staging,production}.json ``` ## Resources - [Lokalise SDK Documentation](https://lokalise.github.io/node-lokalise-api/) - [i18next Documentation](https://www.i18next.com/) - [React i18next](https://react.i18next.com/) ## Flagship Skills For multi-environment setup, see `lokalise-multi-env-setup`.

Skill file: plugins/saas-packs/lokalise-pack/skills/lokalise-reference-architecture/SKILL.md