diff options
Diffstat (limited to 'server/lib/core/translation_service.dart')
| -rw-r--r-- | server/lib/core/translation_service.dart | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/server/lib/core/translation_service.dart b/server/lib/core/translation_service.dart new file mode 100644 index 0000000..28dbdc2 --- /dev/null +++ b/server/lib/core/translation_service.dart @@ -0,0 +1,53 @@ +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/<locale>.json` and resolves keys with +/// a fallback to the English key. +class TranslationService { + Map<String, String> _translations = {}; + + // Returns the translated string, or the English key if no translation exists. + String t(String key) => _translations[key] ?? key; + + Future<void> 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<String, dynamic> data = + jsonDecode(jsonString) as Map<String, dynamic>; + final Map<String, String> flat = {}; + for (final entry in data.entries) { + final value = entry.value; + if (value is Map<String, dynamic>) { + 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 = {}; + } + } +} |
