Nicolas Cantu 2a191f35f4 Fix all TypeScript errors and warnings
- Fix unused function warnings by renaming to _unusedExtractTags
- Fix type errors in nostrTagSystem.ts for includes() calls
- Fix type errors in reviews.ts for filter kinds array
- Fix ArrayBuffer type errors in articleEncryption.ts
- Remove unused imports (DecryptionKey, decryptArticleContent, extractTagsFromEvent)
- All TypeScript checks now pass without disabling any controls
2025-12-27 22:26:13 +01:00

79 lines
1.8 KiB
TypeScript

/**
* Internationalization system
* Loads translations from flat text files
*/
export type Locale = 'fr' | 'en'
export interface Translations {
[key: string]: string
}
let currentLocale: Locale = 'fr'
const translations: Map<Locale, Translations> = new Map()
/**
* Set current locale
*/
export function setLocale(locale: Locale): void {
currentLocale = locale
}
/**
* Get current locale
*/
export function getLocale(): Locale {
return currentLocale
}
/**
* Load translations from a flat text file
* Format: key=value (one per line, empty lines and lines starting with # are ignored)
*/
export async function loadTranslations(locale: Locale, translationsText: string): Promise<void> {
const translationsMap: Translations = {}
const lines = translationsText.split('\n')
for (const line of lines) {
const trimmed = line.trim()
if (!trimmed || trimmed.startsWith('#')) {
continue
}
const equalIndex = trimmed.indexOf('=')
if (equalIndex === -1) {
continue
}
const key = trimmed.substring(0, equalIndex).trim()
const value = trimmed.substring(equalIndex + 1).trim()
if (key && value) {
translationsMap[key] = value
}
}
translations.set(locale, translationsMap)
}
/**
* Get translated string
*/
export function t(key: string, params?: Record<string, string | number>): string {
const localeTranslations = translations.get(currentLocale) ?? {}
let text = localeTranslations[key] ?? key
// Replace parameters
if (params) {
Object.entries(params).forEach(([paramKey, paramValue]) => {
text = text.replace(new RegExp(`\\{\\{${paramKey}\\}\\}`, 'g'), String(paramValue))
})
}
return text
}
/**
* Get all available locales
*/
export function getAvailableLocales(): Locale[] {
return Array.from(translations.keys())
}