mirror of
https://github.com/pd4d10/git-touch.git
synced 2026-04-28 10:28:58 -05:00
fix: locale resolve
This commit is contained in:
30
lib/app.dart
30
lib/app.dart
@@ -5,6 +5,7 @@ import 'package:git_touch/models/auth.dart';
|
||||
import 'package:git_touch/models/theme.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flutter_gen/gen_l10n/S.dart';
|
||||
import 'package:intl/locale.dart' as l;
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
@@ -14,7 +15,34 @@ class MyApp extends StatelessWidget {
|
||||
|
||||
final LocaleListResolutionCallback localeListResolutionCallback =
|
||||
(locales, supportedLocales) {
|
||||
return theme.userSetLocale ?? supportedLocales.first;
|
||||
// 1. user set locale
|
||||
// 2. system locale
|
||||
try {
|
||||
if (theme.locale != null) {
|
||||
final intlLocale = l.Locale.parse(theme.locale);
|
||||
locales = [
|
||||
Locale.fromSubtags(
|
||||
languageCode: intlLocale.languageCode,
|
||||
countryCode: intlLocale.countryCode,
|
||||
scriptCode: intlLocale.scriptCode,
|
||||
),
|
||||
...locales
|
||||
];
|
||||
}
|
||||
} catch (err) {
|
||||
print(err);
|
||||
}
|
||||
|
||||
for (final locale in locales) {
|
||||
// this is necessary because Flutter only handles zh_Hans -> zh
|
||||
// and would not handle non-exist language code
|
||||
if (AppLocalizations.delegate.isSupported(locale)) {
|
||||
return locale;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. if none match, use the default
|
||||
return supportedLocales.firstWhere((l) => l.languageCode == 'en');
|
||||
};
|
||||
|
||||
return Container(
|
||||
|
||||
@@ -9,7 +9,6 @@ import 'package:git_touch/widgets/action_button.dart';
|
||||
import 'package:primer/primer.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:intl/locale.dart' as l;
|
||||
import 'package:flutter_gen/gen_l10n/S.dart';
|
||||
|
||||
class DialogOption<T> {
|
||||
@@ -191,18 +190,6 @@ class ThemeModel with ChangeNotifier {
|
||||
// supported languages
|
||||
String _locale;
|
||||
String get locale => _locale;
|
||||
Locale get userSetLocale {
|
||||
try {
|
||||
final parsedLocale = l.Locale.parse(_locale);
|
||||
return Locale.fromSubtags(
|
||||
languageCode: parsedLocale.languageCode,
|
||||
countryCode: parsedLocale.countryCode,
|
||||
scriptCode: parsedLocale.scriptCode,
|
||||
);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setLocale(String v) async {
|
||||
_locale = v;
|
||||
|
||||
Reference in New Issue
Block a user