add structure
This commit is contained in:
42
lib/widget/localization/app_translations.dart
Normal file
42
lib/widget/localization/app_translations.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
|
||||
class AppTranslations {
|
||||
Locale locale;
|
||||
static Map<dynamic, dynamic> _localisedValues;
|
||||
|
||||
AppTranslations(Locale locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
static AppTranslations of(BuildContext context) {
|
||||
return Localizations.of<AppTranslations>(context, AppTranslations);
|
||||
}
|
||||
|
||||
static Future<AppTranslations> load(Locale locale) async {
|
||||
AppTranslations appTranslations = AppTranslations(locale);
|
||||
String jsonContent = await rootBundle
|
||||
.loadString("assets/local/localization_${locale.languageCode}.json");
|
||||
_localisedValues = json.decode(jsonContent);
|
||||
return appTranslations;
|
||||
}
|
||||
|
||||
get currentLanguage => locale.languageCode;
|
||||
|
||||
String text(String key, {List<String> translationVariables}) {
|
||||
String value = _localisedValues[key];
|
||||
if (value == null) {
|
||||
return "$key not found";
|
||||
}
|
||||
if (translationVariables != null) {
|
||||
translationVariables.asMap().forEach((i, s) {
|
||||
value = value.replaceAll("{$i}", s);
|
||||
});
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
25
lib/widget/localization/app_translations_delegate.dart
Normal file
25
lib/widget/localization/app_translations_delegate.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'app_translations.dart';
|
||||
import 'transalation.dart';
|
||||
|
||||
class AppTranslationsDelegate extends LocalizationsDelegate<AppTranslations> {
|
||||
final Locale newLocale;
|
||||
|
||||
const AppTranslationsDelegate({this.newLocale});
|
||||
|
||||
@override
|
||||
bool isSupported(Locale locale) {
|
||||
return Translation().supportedLanguagesCodes.contains(locale.languageCode);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AppTranslations> load(Locale locale) {
|
||||
return AppTranslations.load(newLocale ?? locale);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldReload(LocalizationsDelegate<AppTranslations> old) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
27
lib/widget/localization/transalation.dart
Normal file
27
lib/widget/localization/transalation.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'dart:ui';
|
||||
|
||||
typedef void LocaleChangeCallback(Locale locale);
|
||||
|
||||
class Translation {
|
||||
static final Translation _translation = Translation._internal();
|
||||
|
||||
factory Translation() {
|
||||
return _translation;
|
||||
}
|
||||
|
||||
Translation._internal();
|
||||
|
||||
final List<String> supportedLanguages = [
|
||||
"English",
|
||||
"မြန်မာ ",
|
||||
];
|
||||
|
||||
final List<String> supportedLanguagesCodes = ["en", "mu"];
|
||||
|
||||
//returns the list of supported Locales
|
||||
Iterable<Locale> supportedLocales() =>
|
||||
supportedLanguagesCodes.map<Locale>((language) => Locale(language, ""));
|
||||
|
||||
//function to be invoked when changing the language
|
||||
LocaleChangeCallback onLocaleChanged;
|
||||
}
|
||||
Reference in New Issue
Block a user