From a984fc15b028bf498cdb4720d2bc3e6ed2b4fc85 Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 18 Dec 2024 18:41:37 +0100 Subject: [PATCH] added localizations for content --- lib/pages/landing_page.dart | 2 +- lib/services/content_provider.dart | 26 +++++++++++++++++++++----- pubspec.yaml | 3 ++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/pages/landing_page.dart b/lib/pages/landing_page.dart index 78f2740..9781e3f 100644 --- a/lib/pages/landing_page.dart +++ b/lib/pages/landing_page.dart @@ -25,7 +25,7 @@ class _LandingPageState extends State { void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) async { - await ContentProvider.init(); + await ContentProvider.init(context); setState(() => loadingDone = true); }); } diff --git a/lib/services/content_provider.dart b/lib/services/content_provider.dart index 38bb53e..a649520 100644 --- a/lib/services/content_provider.dart +++ b/lib/services/content_provider.dart @@ -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 init() async { + static Future 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 _content = { diff --git a/pubspec.yaml b/pubspec.yaml index 7363561..829f315 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,7 +27,8 @@ flutter: uses-material-design: true assets: - - assets/content/content.json + - assets/content/content_de.json + - assets/content/content_en.json - assets/profile.jpg - assets/de_icon.png - assets/gb_icon.png