nc-photos/app/lib/language_util.dart

80 lines
2.8 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 {
2021-08-28 22:58:10 +02:00
const AppLanguage(this.langId, this.nativeName, this.isoName, this.locale);
2021-06-23 10:15:25 +02:00
final int langId;
final String nativeName;
2021-08-28 22:58:10 +02:00
final String? isoName;
2021-07-23 22:05:57 +02:00
final Locale? locale;
2021-06-23 10:15:25 +02:00
}
2021-08-29 10:30:37 +02:00
String getSelectedLanguageName() => _getSelectedLanguage().nativeName;
Locale? getSelectedLocale() => _getSelectedLanguage().locale;
2021-06-23 10:15:25 +02:00
final supportedLanguages = {
2021-08-28 22:58:10 +02:00
_AppLanguageEnum.systemDefault.index: AppLanguage(
_AppLanguageEnum.systemDefault.index, "System default", null, null),
2021-08-02 07:53:13 +02:00
// sorted by alphabetic order of their ISO language names
2022-05-14 08:46:31 +02:00
_AppLanguageEnum.chineseHans.index: AppLanguage(
_AppLanguageEnum.chineseHans.index,
"中文 (简体)",
"Chinese (Simplified)",
const Locale.fromSubtags(languageCode: "zh", scriptCode: "Hans")),
_AppLanguageEnum.chineseHant.index: AppLanguage(
_AppLanguageEnum.chineseHant.index,
"中文 (繁體/正體)",
"Chinese (Traditional)",
const Locale.fromSubtags(languageCode: "zh", scriptCode: "Hant")),
2021-10-01 21:39:36 +02:00
_AppLanguageEnum.czech.index: AppLanguage(
_AppLanguageEnum.czech.index, "Čeština", "Czech", const Locale("cs")),
2021-06-23 10:15:25 +02:00
_AppLanguageEnum.english.index: AppLanguage(
2021-08-28 22:58:10 +02:00
_AppLanguageEnum.english.index, "English", "English", const Locale("en")),
2022-01-05 21:31:29 +01:00
_AppLanguageEnum.finnish.index: AppLanguage(
_AppLanguageEnum.finnish.index, "Suomi", "Finnish", const Locale("fi")),
2021-08-05 08:59:04 +02:00
_AppLanguageEnum.french.index: AppLanguage(
_AppLanguageEnum.french.index, "Français", "French", const Locale("fr")),
2021-09-12 23:26:38 +02:00
_AppLanguageEnum.german.index: AppLanguage(
_AppLanguageEnum.german.index, "Deutsch", "German", const Locale("de")),
2021-08-28 22:58:10 +02:00
_AppLanguageEnum.greek.index: AppLanguage(
_AppLanguageEnum.greek.index, "Ελληνικά", "Greek", const Locale("el")),
2022-01-22 12:20:49 +01:00
_AppLanguageEnum.polish.index: AppLanguage(
_AppLanguageEnum.polish.index, "Polski", "Polish", const Locale("pl")),
2022-04-18 21:47:15 +02:00
_AppLanguageEnum.portuguese.index: AppLanguage(
_AppLanguageEnum.portuguese.index,
"Português",
"Portuguese",
const Locale("pt")),
2021-08-22 23:02:58 +02:00
_AppLanguageEnum.russian.index: AppLanguage(
_AppLanguageEnum.russian.index, "Русский", "Russian", const Locale("ru")),
2021-06-23 10:15:25 +02:00
_AppLanguageEnum.spanish.index: AppLanguage(
2021-08-28 22:58:10 +02:00
_AppLanguageEnum.spanish.index, "Español", "Spanish", const Locale("es")),
2021-06-23 10:15:25 +02:00
};
enum _AppLanguageEnum {
// the order must not be changed
systemDefault,
english,
spanish,
2021-08-02 07:53:13 +02:00
greek,
2021-08-05 08:59:04 +02:00
french,
2021-08-22 23:02:58 +02:00
russian,
2021-09-12 23:26:38 +02:00
german,
2021-10-01 21:39:36 +02:00
czech,
2022-01-05 21:31:29 +01:00
finnish,
2022-01-22 12:20:49 +01:00
polish,
2022-04-18 21:47:15 +02:00
portuguese,
2022-05-14 08:46:31 +02:00
chineseHans,
chineseHant,
2021-06-23 10:15:25 +02:00
}
2021-08-29 10:30:37 +02:00
AppLanguage _getSelectedLanguage() {
2021-06-23 10:15:25 +02:00
try {
2021-10-27 22:40:54 +02:00
final lang = Pref().getLanguageOr(0);
2021-07-23 22:05:57 +02:00
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
}
}