content localized on locale change
This commit is contained in:
58
lib/providers/content_provider.dart
Normal file
58
lib/providers/content_provider.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:resume/constants.dart';
|
||||
import 'package:resume/providers/locale_provider.dart';
|
||||
|
||||
class ContentProvider extends ChangeNotifier {
|
||||
static final ContentProvider _instance = ContentProvider._();
|
||||
factory ContentProvider() => _instance;
|
||||
ContentProvider._();
|
||||
|
||||
static const String _baseJsonPath = 'assets/content/content';
|
||||
|
||||
Map<String, dynamic> _content = {
|
||||
'experience': <List<dynamic>>[],
|
||||
'education': <List<dynamic>>[],
|
||||
'skills': <List<dynamic>>[],
|
||||
'text': <String>[],
|
||||
'general_skills': <String>[],
|
||||
};
|
||||
|
||||
Future<bool> loadContent(BuildContext context) async {
|
||||
String currentLocale = context.read<LocaleProvider>().locale.languageCode;
|
||||
final String localizedPath = '${_baseJsonPath}_$currentLocale.json';
|
||||
|
||||
try {
|
||||
String file = await rootBundle.loadString(localizedPath);
|
||||
_content = json.decode(file);
|
||||
notifyListeners();
|
||||
return true;
|
||||
} catch (e) {
|
||||
// If localized version fails, fall back to default German
|
||||
String file = await rootBundle.loadString('${_baseJsonPath}_de.json');
|
||||
_content = json.decode(file);
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
T getContent<T>(ContentType contentType) {
|
||||
switch (contentType) {
|
||||
case ContentType.experience:
|
||||
return _content['experience'] as T;
|
||||
case ContentType.education:
|
||||
return _content['education'] as T;
|
||||
case ContentType.skills:
|
||||
return _content['skills'] as T;
|
||||
case ContentType.text:
|
||||
return _content['text'] as T;
|
||||
case ContentType.generalSkills:
|
||||
return _content['general_skills'] as T;
|
||||
default:
|
||||
return [] as T;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
lib/providers/locale_provider.dart
Normal file
12
lib/providers/locale_provider.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LocaleProvider extends ChangeNotifier {
|
||||
Locale _locale = const Locale('de');
|
||||
|
||||
Locale get locale => _locale;
|
||||
|
||||
void setLocale(Locale locale) {
|
||||
_locale = locale;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user