Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

2 years ago
2 years ago
2 years ago
  1. import 'dart:convert';
  2. import 'package:data/app_setting_data/repository/app_setting_box_repository_impl.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart' show rootBundle;
  5. import 'package:local_db_core/lib/boxes/box_list/setting_box/app_setting_box.dart';
  6. import 'package:repositories/app_setting_box_domain/repository/app_setting_box_repository.dart';
  7. import 'package:sonnat/core/language/languages.dart';
  8. final List<String> supportedLanguages = [
  9. 'en',
  10. 'fa',
  11. 'ar',
  12. ];
  13. class Translator {
  14. static Map<dynamic, dynamic>? _localizedValues;
  15. static Iterable<Locale> supportedLocales() => supportedLanguages.map<Locale>((lang) => Locale(lang, ''));
  16. static String translate(String key) {
  17. if (_localizedValues == null || _localizedValues![key] == null) {
  18. return '$key not found';
  19. }
  20. return _localizedValues![key];
  21. }
  22. static Future<void> setNewLanguage(Languages newLanguage) async {
  23. AppSettingBoxRepository repository = AppSettingBoxRepositoryImpl(appSettingBox: AppSettingBox());
  24. Locale locale = Locale(newLanguage.value, '');
  25. String jsonContent = await rootBundle.loadString('assets/languages/${locale.languageCode}.json');
  26. _localizedValues = json.decode(jsonContent);
  27. await repository.setCurrentLanguage(locale.languageCode);
  28. return;
  29. }
  30. Translator._internal();
  31. }