nc-photos/lib/language_util.dart

41 lines
1.1 KiB
Dart
Raw Normal View History

2021-06-23 10:15:25 +02:00
import 'package:flutter/widgets.dart';
import 'package:nc_photos/pref.dart';
class AppLanguage {
const AppLanguage(this.langId, this.nativeName, this.locale);
final int langId;
final String nativeName;
2021-07-23 22:05:57 +02:00
final Locale? locale;
2021-06-23 10:15:25 +02:00
}
String getSelectedLanguageName(BuildContext context) =>
_getSelectedLanguage(context).nativeName;
2021-07-23 22:05:57 +02:00
Locale? getSelectedLocale(BuildContext context) =>
2021-06-23 10:15:25 +02:00
_getSelectedLanguage(context).locale;
final supportedLanguages = {
_AppLanguageEnum.systemDefault.index:
AppLanguage(_AppLanguageEnum.systemDefault.index, "System default", null),
_AppLanguageEnum.english.index: AppLanguage(
_AppLanguageEnum.english.index, "English", const Locale("en")),
_AppLanguageEnum.spanish.index: AppLanguage(
_AppLanguageEnum.spanish.index, "Español", const Locale("es")),
};
enum _AppLanguageEnum {
// the order must not be changed
systemDefault,
english,
spanish,
}
AppLanguage _getSelectedLanguage(BuildContext context) {
try {
2021-07-23 22:05:57 +02:00
final lang = Pref.inst().getLanguageOr(0);
return supportedLanguages[lang]!;
2021-06-23 10:15:25 +02:00
} catch (_) {
2021-07-23 22:05:57 +02:00
return supportedLanguages[_AppLanguageEnum.systemDefault.index]!;
2021-06-23 10:15:25 +02:00
}
}