import 'dart:convert'; import 'dart:io'; import 'package:logging/logging.dart'; final _log = Logger('TranslationService'); /// In-game ability-text translations (the reference app's `TranslationService`, /// disk-based port): loads `assets/i18n/.json` and resolves keys with /// a fallback to the English key. class TranslationService { Map _translations = {}; // Returns the translated string, or the English key if no translation exists. String t(String key) => _translations[key] ?? key; Future load(String locale) async { if (locale == 'en') { _translations = {}; return; } try { final file = File('assets/i18n/$locale.json'); if (!file.existsSync()) { _log.warning( 'TranslationService: locale file "$locale.json" not found', ); _translations = {}; return; } final jsonString = await file.readAsString(); final Map data = jsonDecode(jsonString) as Map; final Map flat = {}; for (final entry in data.entries) { final value = entry.value; if (value is Map) { for (final inner in value.entries) { if (inner.value is String) { flat[inner.key] = inner.value as String; } } } else if (value is String) { flat[entry.key] = value; } } _translations = flat; } catch (e) { _log.warning('TranslationService: failed to load locale "$locale": $e'); _translations = {}; } } }