Refactor
This commit is contained in:
13
lib/core/localization/locale_extensions.dart
Normal file
13
lib/core/localization/locale_extensions.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
|
||||
import 'package:hiddify/gen/fonts.gen.dart';
|
||||
import 'package:hiddify/gen/translations.g.dart';
|
||||
|
||||
extension AppLocaleX on AppLocale {
|
||||
String get preferredFontFamily =>
|
||||
this == AppLocale.fa ? FontFamily.shabnam : "";
|
||||
|
||||
String get localeName =>
|
||||
LocaleNamesLocalizationsDelegate
|
||||
.nativeLocaleNames[flutterLocale.toString()] ??
|
||||
name;
|
||||
}
|
||||
28
lib/core/localization/locale_preferences.dart
Normal file
28
lib/core/localization/locale_preferences.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:hiddify/core/preferences/preferences_provider.dart';
|
||||
import 'package:hiddify/gen/translations.g.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'locale_preferences.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class LocalePreferences extends _$LocalePreferences {
|
||||
@override
|
||||
AppLocale build() {
|
||||
final persisted =
|
||||
ref.watch(sharedPreferencesProvider).requireValue.getString("locale");
|
||||
if (persisted == null) return AppLocaleUtils.findDeviceLocale();
|
||||
// keep backward compatibility with chinese after changing zh to zh_CN
|
||||
if (persisted == "zh") {
|
||||
return AppLocale.zhCn;
|
||||
}
|
||||
return AppLocale.values.byName(persisted);
|
||||
}
|
||||
|
||||
Future<void> changeLocale(AppLocale value) async {
|
||||
state = value;
|
||||
await ref
|
||||
.read(sharedPreferencesProvider)
|
||||
.requireValue
|
||||
.setString("locale", value.name);
|
||||
}
|
||||
}
|
||||
11
lib/core/localization/translations.dart
Normal file
11
lib/core/localization/translations.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'package:hiddify/core/localization/locale_preferences.dart';
|
||||
import 'package:hiddify/gen/translations.g.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
export 'package:hiddify/gen/translations.g.dart';
|
||||
|
||||
part 'translations.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
TranslationsEn translations(TranslationsRef ref) =>
|
||||
ref.watch(localePreferencesProvider).build();
|
||||
Reference in New Issue
Block a user