47 lines
1.2 KiB
Dart
47 lines
1.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class AppLocalizations {
|
|
static final AppLocalizations _singleton = new AppLocalizations._internal();
|
|
AppLocalizations._internal();
|
|
static AppLocalizations get instance => _singleton;
|
|
|
|
Map<dynamic, dynamic> _localisedValues;
|
|
Locale _currentLocale;
|
|
|
|
Future<AppLocalizations> load(Locale locale) async {
|
|
String jsonContent = await rootBundle
|
|
.loadString("locale/locale_${locale.languageCode}.json");
|
|
_localisedValues = json.decode(jsonContent);
|
|
_currentLocale = locale;
|
|
return this;
|
|
}
|
|
|
|
String getLocale() {
|
|
return _currentLocale.languageCode;
|
|
}
|
|
|
|
String text(String key) {
|
|
return _localisedValues[key] ?? "$key not found";
|
|
}
|
|
}
|
|
|
|
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
|
|
const AppLocalizationsDelegate();
|
|
|
|
@override
|
|
bool isSupported(Locale locale) =>
|
|
['en', 'pl', 'de'].contains(locale.languageCode);
|
|
|
|
@override
|
|
Future<AppLocalizations> load(Locale locale) {
|
|
return AppLocalizations.instance.load(locale);
|
|
}
|
|
|
|
@override
|
|
bool shouldReload(AppLocalizationsDelegate old) => true;
|
|
}
|