import { Router, Request, Response } from 'express'; import { i18nService } from '../i18n'; export function createI18nRoutes(): Router { const router = Router(); /** * Get translations for a specific locale * GET /api/i18n/:locale */ router.get('/:locale', (req: Request, res: Response): void => { const { locale } = req.params; // Validate locale if (!i18nService.isLocaleSupported(locale)) { res.status(400).json({ error: 'Unsupported locale', supportedLocales: i18nService.getAvailableLocales() }); return; } // Get translations for the locale const translations = i18nService.getTranslations(locale); if (!translations) { res.status(404).json({ error: 'Translations not found for locale', locale }); return; } // Set appropriate headers for caching res.set({ 'Cache-Control': 'public, max-age=3600', // Cache for 1 hour 'Content-Type': 'application/json' }); res.json(translations); }); /** * Get available locales and their display names * GET /api/i18n */ router.get('/', (req: Request, res: Response): void => { const locales = i18nService.getAvailableLocales().map(locale => ({ code: locale, name: i18nService.getLocaleDisplayName(locale) })); res.json({ default: i18nService.getDefaultLocale(), available: locales }); }); /** * Detect user's preferred locale based on Accept-Language header * GET /api/i18n/detect */ router.get('/detect', (req: Request, res: Response): void => { const acceptLanguage = req.get('Accept-Language'); const detectedLocale = i18nService.detectLocale(acceptLanguage); res.json({ detected: detectedLocale, displayName: i18nService.getLocaleDisplayName(detectedLocale) }); }); return router; } export default createI18nRoutes;