Réécriture complète 4.0
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 6m16s
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 6m16s
This commit is contained in:
111
src/utils/i18n.ts
Normal file
111
src/utils/i18n.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import type { Locale } from "discord.js"
|
||||
import frLocale from "@/locales/fr.json"
|
||||
import enLocale from "@/locales/en.json"
|
||||
|
||||
// Variables d'environnement pour les locales avec valeurs par défaut
|
||||
const DEFAULT_LOCALE = process.env.DEFAULT_LOCALE ?? 'fr'
|
||||
const CONSOLE_LOCALE = process.env.CONSOLE_LOCALE ?? 'en'
|
||||
|
||||
// Types pour l'internationalisation
|
||||
type LocaleData = Record<string, unknown>
|
||||
type ReplacementParams = Record<string, string | number>
|
||||
type TranslationKey = string
|
||||
|
||||
// Conversion des imports en type LocaleData
|
||||
const frLocaleData = frLocale as unknown as LocaleData
|
||||
const enLocaleData = enLocale as unknown as LocaleData
|
||||
|
||||
// Locales supportées
|
||||
const locales = {
|
||||
'fr': frLocaleData,
|
||||
'en-US': enLocaleData,
|
||||
'en-GB': enLocaleData
|
||||
} as const
|
||||
|
||||
type SupportedLocale = keyof typeof locales
|
||||
|
||||
/**
|
||||
* Récupère une traduction basée sur la locale et la clé
|
||||
*/
|
||||
function getNestedValue(obj: LocaleData, path: string): string | undefined {
|
||||
try {
|
||||
const keys = path.split('.')
|
||||
let current: unknown = obj
|
||||
|
||||
for (const key of keys) {
|
||||
if (current !== null && typeof current === 'object' && key in current) current = (current as Record<string, unknown>)[key]
|
||||
else return undefined
|
||||
}
|
||||
|
||||
return typeof current === 'string' ? current : undefined
|
||||
} catch { return undefined }
|
||||
}
|
||||
|
||||
/**
|
||||
* Remplace les paramètres dans une chaîne de caractères
|
||||
* Exemple: "Hello {name}" avec {name: "World"} devient "Hello World"
|
||||
*/
|
||||
function replaceParams(text: string, params: ReplacementParams = {}): string {
|
||||
return text.replace(/\{(\w+)\}/g, (match, key: string) => {
|
||||
if (Object.prototype.hasOwnProperty.call(params, key)) return params[key].toString()
|
||||
return match
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction principale de localisation
|
||||
* @param locale - La locale Discord de l'utilisateur
|
||||
* @param key - La clé de traduction (ex: "player.not_in_voice")
|
||||
* @param params - Les paramètres à remplacer dans la traduction
|
||||
* @returns La chaîne traduite
|
||||
*/
|
||||
export function t(locale: Locale | string, key: TranslationKey, params: ReplacementParams = {}): string {
|
||||
// Détermine la locale à utiliser (par défaut de la config)
|
||||
const supportedLocale: SupportedLocale = (Object.keys(locales).includes(locale)) ? locale as SupportedLocale : DEFAULT_LOCALE as SupportedLocale
|
||||
|
||||
// Récupère les données de locale
|
||||
const localeData = locales[supportedLocale]
|
||||
|
||||
// Récupère la traduction
|
||||
const translation = getNestedValue(localeData, key)
|
||||
|
||||
if (!translation) {
|
||||
console.warn(`[Localization] Translation not found for key: ${key} in locale: ${supportedLocale}`)
|
||||
return key // Retourne la clé si la traduction n'est pas trouvée
|
||||
}
|
||||
|
||||
// Remplace les paramètres et retourne la traduction
|
||||
return replaceParams(translation, params)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction helper pour obtenir la locale française par défaut
|
||||
*/
|
||||
export function fr(key: TranslationKey, params: ReplacementParams = {}): string {
|
||||
return t('fr', key, params)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction helper pour obtenir la locale anglaise
|
||||
*/
|
||||
export function en(key: TranslationKey, params: ReplacementParams = {}): string {
|
||||
return t('en-US', key, params)
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtient les locales supportées pour une commande
|
||||
* Utilisé pour les localisations des commandes slash
|
||||
*/
|
||||
export function getCommandLocalizations(baseKey: string) {
|
||||
return {
|
||||
fr: getNestedValue(frLocaleData, baseKey) ?? baseKey,
|
||||
'en-US': getNestedValue(enLocaleData, baseKey) ?? baseKey,
|
||||
'en-GB': getNestedValue(enLocaleData, baseKey) ?? baseKey
|
||||
}
|
||||
}
|
||||
|
||||
// Export des constantes de locale
|
||||
export { DEFAULT_LOCALE, CONSOLE_LOCALE }
|
||||
|
||||
// Export des types pour utilisation externe
|
||||
export type { TranslationKey, ReplacementParams, SupportedLocale }
|
||||
Reference in New Issue
Block a user