nc-photos/app/lib/language_util.dart

107 lines
3.7 KiB
Dart
Raw Normal View History

2021-06-23 10:15:25 +02:00
import 'package:flutter/widgets.dart';
2022-08-08 08:35:26 +02:00
import 'package:nc_photos/app_localizations.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2021-06-23 10:15:25 +02:00
class AppLanguage {
2022-08-08 08:35:26 +02:00
const AppLanguage(this.langId, this._nativeName, this.isoName, this.locale);
String get nativeName {
if (langId == _AppLanguageEnum.systemDefault.index) {
try {
return L10n.global().settingsLanguageOptionSystemDefaultLabel;
} catch (_) {
return _nativeName;
}
} else {
return _nativeName;
}
}
2021-06-23 10:15:25 +02:00
final int langId;
2022-08-08 08:35:26 +02:00
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
}
2023-06-06 15:39:58 +02:00
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
2024-08-10 16:27:28 +02:00
_AppLanguageEnum.catalan.index: AppLanguage(
_AppLanguageEnum.catalan.index, "català", "Catalan", const Locale("ca")),
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")),
2023-06-06 16:31:51 +02:00
_AppLanguageEnum.dutch.index: AppLanguage(
_AppLanguageEnum.dutch.index, "Nederlands", "Dutch", const Locale("nl")),
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")),
2023-06-06 16:31:51 +02:00
_AppLanguageEnum.italian.index: AppLanguage(_AppLanguageEnum.italian.index,
"Italiano", "Italian", const Locale("it")),
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")),
2024-12-17 18:11:58 +01:00
_AppLanguageEnum.slovak.index: AppLanguage(_AppLanguageEnum.slovak.index,
"slovenčina", "Slovak", const Locale("sk")),
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")),
2024-04-11 19:25:15 +02:00
_AppLanguageEnum.turkish.index: AppLanguage(
_AppLanguageEnum.turkish.index, "Türkçe", "Turkish", const Locale("tr")),
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,
2023-06-06 16:31:51 +02:00
italian,
dutch,
2023-09-01 20:58:38 +02:00
catalan,
2024-04-11 19:25:15 +02:00
turkish,
2024-12-17 18:11:58 +01:00
slovak,
2021-06-23 10:15:25 +02:00
}
2023-06-06 15:39:58 +02:00
AppLanguage _getSelectedLanguage() {
try {
final lang = Pref().getLanguageOr(0);
return supportedLanguages[lang]!;
} catch (_) {
return supportedLanguages[_AppLanguageEnum.systemDefault.index]!;
}
}