feat: Improve language update settings (#1838)

This commit is contained in:
Lam Thanh Nhan 2024-04-05 07:55:16 +07:00 committed by GitHub
parent e80ee09893
commit f9e6ef3fd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 24 deletions

View File

@ -34,17 +34,18 @@ class SUpdateLanguage extends BaseViewModel {
}
Future<void> showLanguagesDialog(BuildContext parentContext) {
final ValueNotifier<String> selectedLanguageCode = ValueNotifier(
'${LocaleSettings.currentLocale.languageCode}-${LocaleSettings.currentLocale.countryCode}',
final ValueNotifier<AppLocale> selectedLanguageCode = ValueNotifier(
LocaleSettings.currentLocale,
);
LanguageCodes getLanguageCode(locale) {
return LanguageCodes.fromCode(
'${locale.languageCode}_${locale.countryCode}',
LanguageCodes getLanguageCode(Locale locale) {
return LanguageCodes.fromLocale(
locale,
orElse: () => LanguageCodes.fromCode(locale.languageCode),
);
}
final currentlyUsedLanguage = getLanguageCode(LocaleSettings.currentLocale);
final currentlyUsedLanguage =
getLanguageCode(LocaleSettings.currentLocale.flutterLocale);
// initLang();
// Return a dialog with list for each language supported by the application.
@ -63,38 +64,34 @@ class SUpdateLanguage extends BaseViewModel {
child: ListBody(
children: [
RadioListTile(
// TODO(Someone): There must've been a better way to do this.
title: Text(currentlyUsedLanguage.englishName),
subtitle: Text(
'${currentlyUsedLanguage.nativeName} (${LocaleSettings.currentLocale.languageCode}${LocaleSettings.currentLocale.countryCode != null ? '-${LocaleSettings.currentLocale.countryCode}' : ''})',
'${currentlyUsedLanguage.nativeName}\n'
'(${LocaleSettings.currentLocale.languageTag})',
),
value:
'${LocaleSettings.currentLocale.languageCode}-${LocaleSettings.currentLocale.countryCode}' ==
value: LocaleSettings.currentLocale ==
selectedLanguageCode.value,
groupValue: true,
onChanged: (value) {
selectedLanguageCode.value =
'${LocaleSettings.currentLocale.languageCode}-${LocaleSettings.currentLocale.countryCode}';
selectedLanguageCode.value = LocaleSettings.currentLocale;
},
),
...AppLocale.values
.where(
(locale) =>
locale.languageCode != currentlyUsedLanguage.code,
(locale) => locale != LocaleSettings.currentLocale,
)
.map((locale) {
final languageCode = getLanguageCode(locale);
final languageCode = getLanguageCode(locale.flutterLocale);
return RadioListTile(
title: Text(languageCode.englishName),
subtitle: Text(
'${languageCode.nativeName} (${locale.languageCode}${locale.countryCode != null ? '-${locale.countryCode}' : ''})',
'${languageCode.nativeName}\n'
'(${locale.languageTag})',
),
value: '${locale.languageCode}-${locale.countryCode}' ==
selectedLanguageCode.value,
value: locale == selectedLanguageCode.value,
groupValue: true,
onChanged: (value) {
selectedLanguageCode.value =
'${locale.languageCode}-${locale.countryCode}';
selectedLanguageCode.value = locale;
},
);
}),
@ -113,7 +110,7 @@ class SUpdateLanguage extends BaseViewModel {
TextButton(
onPressed: () {
// TODO(nullcube): Translation will not update until we refresh the page.
updateLocale(selectedLanguageCode.value);
updateLocale(selectedLanguageCode.value.languageTag);
Navigator.of(context).pop();
},
child: Text(t.okButton),
@ -133,7 +130,7 @@ class SUpdateLanguageUI extends StatelessWidget {
padding: const EdgeInsets.symmetric(horizontal: 20.0),
title: t.settingsView.languageLabel,
subtitle:
LanguageCodes.fromCode(LocaleSettings.currentLocale.languageCode)
LanguageCodes.fromLocale(LocaleSettings.currentLocale.flutterLocale)
.nativeName,
onTap: () =>
_settingViewModel.sUpdateLanguage.showLanguagesDialog(context),

View File

@ -41,7 +41,7 @@ dependencies:
injectable: ^2.1.1
intl: ^0.18.0
json_annotation: ^4.8.1
language_code: ^0.4.2
language_code: ^0.5.1
logcat:
git:
url: https://github.com/BenjaminHalko/logcat