added localizations for content

This commit is contained in:
2024-12-18 18:41:37 +01:00
parent 0d9118ab3e
commit a984fc15b0
3 changed files with 24 additions and 7 deletions

View File

@@ -1,21 +1,37 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:resume/constants.dart';
class ContentProvider {
ContentProvider._();
static const String _jsonPath = 'assets/content/content.json';
static const String _baseJsonPath = 'assets/content/content';
static Future<bool> init() async {
static Future<bool> init(BuildContext context) async {
try {
String file = await rootBundle.loadString(_jsonPath);
_content = json.decode(file);
// Get the current locale
final String currentLocale = Localizations.localeOf(context).languageCode;
// Construct the path with the locale
final String localizedPath = '${_baseJsonPath}_$currentLocale.json';
// Try to load the localized version first
try {
String file = await rootBundle.loadString(localizedPath);
_content = json.decode(file);
return true;
} catch (e) {
// If localized version fails, fall back to default English
String file = await rootBundle.loadString('${_baseJsonPath}_de.json');
_content = json.decode(file);
return true;
}
} catch (e) {
print('Error loading content: $e');
return false;
}
return true;
}
static Map<String, dynamic> _content = {