Nav apraksta

localization.dart 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. class DemoLocalization {
  5. DemoLocalization(this.locale);
  6. final Locale locale;
  7. static DemoLocalization of(BuildContext context) {
  8. return Localizations.of<DemoLocalization>(context, DemoLocalization);
  9. }
  10. Map<String, String> _localizedValues;
  11. Future<void> load() async {
  12. String jsonStringValues =
  13. await rootBundle.loadString('lib/languages/${locale.languageCode}.json');
  14. Map<String, dynamic> mappedJson = json.decode(jsonStringValues);
  15. _localizedValues =
  16. mappedJson.map((key, value) => MapEntry(key, value.toString()));
  17. }
  18. String translate(String key) {
  19. return _localizedValues[key];
  20. }
  21. // static member to have simple access to the delegate from Material App
  22. static const LocalizationsDelegate<DemoLocalization> delegate =
  23. _DemoLocalizationsDelegate();
  24. }
  25. class _DemoLocalizationsDelegate
  26. extends LocalizationsDelegate<DemoLocalization> {
  27. const _DemoLocalizationsDelegate();
  28. @override
  29. bool isSupported(Locale locale) {
  30. return ['en', 'es', 'fa', 'hi'].contains(locale.languageCode);
  31. }
  32. @override
  33. Future<DemoLocalization> load(Locale locale) async {
  34. DemoLocalization localization = new DemoLocalization(locale);
  35. await localization.load();
  36. return localization;
  37. }
  38. @override
  39. bool shouldReload(LocalizationsDelegate<DemoLocalization> old) => false;
  40. }