Refactor resources.dart

This commit is contained in:
Calcitem 2021-06-27 21:02:52 +08:00
parent 660ca2703e
commit 5733c0f73a
2 changed files with 116 additions and 262 deletions

View File

@ -21,79 +21,9 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:sanmill/common/config.dart'; import 'package:sanmill/common/config.dart';
import 'package:sanmill/common/constants.dart'; import 'package:sanmill/common/constants.dart';
import 'package:sanmill/generated/l10n.dart';
final supportedLocales = [ import 'package:sanmill/style/app_theme.dart';
const Locale('en', ''), import 'package:sanmill/widgets/list_item_divider.dart';
const Locale.fromSubtags(
languageCode: 'ar',
),
const Locale.fromSubtags(
languageCode: 'cs',
),
const Locale.fromSubtags(
languageCode: 'de',
),
const Locale.fromSubtags(
languageCode: 'es',
),
const Locale.fromSubtags(
languageCode: 'fa',
),
const Locale.fromSubtags(
languageCode: 'fr',
),
const Locale.fromSubtags(
languageCode: 'hi',
),
const Locale.fromSubtags(
languageCode: 'hu',
),
const Locale.fromSubtags(
languageCode: 'ja',
),
const Locale.fromSubtags(
languageCode: 'ko',
),
const Locale.fromSubtags(
languageCode: 'pl',
),
const Locale.fromSubtags(
languageCode: 'pt',
),
const Locale.fromSubtags(
languageCode: 'ro',
),
const Locale.fromSubtags(
languageCode: 'ru',
),
const Locale.fromSubtags(
languageCode: 'tr',
),
const Locale.fromSubtags(
languageCode: 'zh',
),
];
Map<String, String> languageCodeToName = {
"ar": "عربى",
"cs": "Čeština",
"de": "Deutsch",
"en": "English",
"es": "Español",
"fa": "فارسی",
"fr": "Français",
"hi": "हिंदी",
"hu": "Magyar",
"it": "Italiano",
"ja": "日本語",
"ko": "한국어",
"pl": "Polskie",
"pt": "Português",
"ro": "Română",
"ru": "Pусский",
"tr": "Türk",
"zh": "简体中文",
};
Map<String, Strings> languageCodeToStrings = { Map<String, Strings> languageCodeToStrings = {
"ar": ArabicStrings(), "ar": ArabicStrings(),
@ -118,41 +48,60 @@ Map<String, Strings> languageCodeToStrings = {
/// Interface strings /// Interface strings
abstract class Strings { abstract class Strings {
String get languageName;
String get tapBackAgainToLeave; String get tapBackAgainToLeave;
} }
/// ar /// ar
class ArabicStrings extends Strings { class ArabicStrings extends Strings {
@override
String get languageName => 'عربى';
@override @override
String get tapBackAgainToLeave => 'انقر مرة أخرى للمغادرة.'; String get tapBackAgainToLeave => 'انقر مرة أخرى للمغادرة.';
} }
/// cs /// cs
class CzechStrings extends Strings { class CzechStrings extends Strings {
@override
String get languageName => 'Čeština';
@override @override
String get tapBackAgainToLeave => 'Opětovným klepnutím zpět odejdete.'; String get tapBackAgainToLeave => 'Opětovným klepnutím zpět odejdete.';
} }
/// de /// de
class GermanStrings extends Strings { class GermanStrings extends Strings {
@override
String get languageName => 'Deutsch';
@override @override
String get tapBackAgainToLeave => 'Nochmal drücken um zu Beenden.'; String get tapBackAgainToLeave => 'Nochmal drücken um zu Beenden.';
} }
/// en /// en
class EnglishStrings extends Strings { class EnglishStrings extends Strings {
@override
String get languageName => 'English';
@override @override
String get tapBackAgainToLeave => 'Tap back again to leave.'; String get tapBackAgainToLeave => 'Tap back again to leave.';
} }
/// es /// es
class SpanishStrings extends Strings { class SpanishStrings extends Strings {
@override
String get languageName => 'Español';
@override @override
String get tapBackAgainToLeave => 'Vuelve a tocar para salir.'; String get tapBackAgainToLeave => 'Vuelve a tocar para salir.';
} }
/// fa /// fa
class FarsiStrings extends Strings { class FarsiStrings extends Strings {
@override
String get languageName => 'فارسی';
@override @override
String get tapBackAgainToLeave => String get tapBackAgainToLeave =>
'برای خروج از برنامه ، دوباره روی دکمه برگشت ضربه بزنید.'; 'برای خروج از برنامه ، دوباره روی دکمه برگشت ضربه بزنید.';
@ -160,18 +109,27 @@ class FarsiStrings extends Strings {
/// fr /// fr
class FrenchStrings extends Strings { class FrenchStrings extends Strings {
@override
String get languageName => 'Français';
@override @override
String get tapBackAgainToLeave => 'Tapez à nouveau pour quitter.'; String get tapBackAgainToLeave => 'Tapez à nouveau pour quitter.';
} }
/// hi /// hi
class HindiStrings extends Strings { class HindiStrings extends Strings {
@override
String get languageName => 'हिंदी';
@override @override
String get tapBackAgainToLeave => 'जाने के लिए फिर से टैप करें।'; String get tapBackAgainToLeave => 'जाने के लिए फिर से टैप करें।';
} }
/// hu /// hu
class HungarianStrings extends Strings { class HungarianStrings extends Strings {
@override
String get languageName => 'Magyar';
@override @override
String get tapBackAgainToLeave => String get tapBackAgainToLeave =>
'A kilépéshez kattintson ismételten a Vissza gombra.'; 'A kilépéshez kattintson ismételten a Vissza gombra.';
@ -179,58 +137,89 @@ class HungarianStrings extends Strings {
/// it /// it
class ItalianStrings extends Strings { class ItalianStrings extends Strings {
@override
String get languageName => 'Italiano';
@override @override
String get tapBackAgainToLeave => 'Tocca di nuovo per uscire.'; String get tapBackAgainToLeave => 'Tocca di nuovo per uscire.';
} }
/// ja /// ja
class JapaneseStrings extends Strings { class JapaneseStrings extends Strings {
@override
String get languageName => '日本語';
@override @override
String get tapBackAgainToLeave => 'もう一度returnを押してアプリケーションを終了する'; String get tapBackAgainToLeave => 'もう一度returnを押してアプリケーションを終了する';
} }
/// ko /// ko
class KoreanStrings extends Strings { class KoreanStrings extends Strings {
@override
String get languageName => '한국어';
@override @override
String get tapBackAgainToLeave => '애플리케이션을 종료하려면 리턴 키를 다시 누르십시오.'; String get tapBackAgainToLeave => '애플리케이션을 종료하려면 리턴 키를 다시 누르십시오.';
} }
/// pl /// pl
class PolishStrings extends Strings { class PolishStrings extends Strings {
@override
String get languageName => 'Polskie';
@override @override
String get tapBackAgainToLeave => 'Stuknij ponownie w tył, aby wyjść.'; String get tapBackAgainToLeave => 'Stuknij ponownie w tył, aby wyjść.';
} }
/// pt /// pt
class PortugueseStrings extends Strings { class PortugueseStrings extends Strings {
@override
String get languageName => 'Português';
@override @override
String get tapBackAgainToLeave => 'Bater novamente para sair.'; String get tapBackAgainToLeave => 'Bater novamente para sair.';
} }
/// ro /// ro
class RomanianStrings extends Strings { class RomanianStrings extends Strings {
@override
String get languageName => 'Română';
@override @override
String get tapBackAgainToLeave => 'Atingeți din nou pentru a putea muta.'; String get tapBackAgainToLeave => 'Atingeți din nou pentru a putea muta.';
} }
/// ru /// ru
class RussianStrings extends Strings { class RussianStrings extends Strings {
@override
String get languageName => 'Pусский';
@override @override
String get tapBackAgainToLeave => 'Нажмите назад еще раз, чтобы выйти.'; String get tapBackAgainToLeave => 'Нажмите назад еще раз, чтобы выйти.';
} }
/// tr /// tr
class TurkishStrings extends Strings { class TurkishStrings extends Strings {
@override
String get languageName => 'Türk';
@override @override
String get tapBackAgainToLeave => 'Tocca di nuovo per uscire.'; String get tapBackAgainToLeave => 'Tocca di nuovo per uscire.';
} }
/// zh /// zh
class ChineseStrings extends Strings { class ChineseStrings extends Strings {
@override
String get languageName => '简体中文';
@override @override
String get tapBackAgainToLeave => '再次按返回键退出应用'; String get tapBackAgainToLeave => '再次按返回键退出应用';
} }
final supportedLocales = [
for (var i in languageCodeToStrings.keys) Locale.fromSubtags(languageCode: i),
];
class Resources { class Resources {
Resources(); Resources();
@ -256,3 +245,41 @@ class Resources {
return Resources(); return Resources();
} }
} }
setLanguage(BuildContext context, var callback) async {
var languageColumn = Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(S.of(context).defaultLanguage),
groupValue: Config.languageCode,
value: Constants.defaultLanguageCodeName,
onChanged: callback,
),
ListItemDivider(),
for (var i in languageCodeToStrings.keys)
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToStrings[i]!.languageName),
groupValue: Config.languageCode,
value: i,
onChanged: callback,
),
],
);
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
scrollable: true,
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return languageColumn;
},
),
);
},
);
}

View File

@ -334,6 +334,21 @@ class _PersonalizationSettingsPageState
} }
List<Widget> children(BuildContext context) { List<Widget> children(BuildContext context) {
langCallback(var langCode) async {
print("[config] languageCode = $langCode");
Navigator.of(context).pop();
setState(() {
Config.languageCode = langCode ?? Constants.defaultLanguageCodeName;
S.load(Locale(Resources.of().languageCode));
});
print("[config] Config.languageCode: ${Config.languageCode}");
Config.save();
}
return <Widget>[ return <Widget>[
Text(S.of(context).display, style: AppTheme.settingsHeaderStyle), Text(S.of(context).display, style: AppTheme.settingsHeaderStyle),
SettingsCard( SettingsCard(
@ -346,7 +361,7 @@ class _PersonalizationSettingsPageState
Config.languageCode == Constants.defaultLanguageCodeName Config.languageCode == Constants.defaultLanguageCodeName
? "" ? ""
: Config.languageCode.toString(), : Config.languageCode.toString(),
onTap: setLanguage, onTap: () => setLanguage(context, langCallback),
), ),
ListItemDivider(), ListItemDivider(),
SettingsSwitchListTile( SettingsSwitchListTile(
@ -466,194 +481,6 @@ class _PersonalizationSettingsPageState
// Display // Display
setLanguage() async {
callback(var langCode) async {
print("[config] languageCode = $langCode");
Navigator.of(context).pop();
setState(() {
Config.languageCode = langCode ?? Constants.defaultLanguageCodeName;
S.load(Locale(Resources.of().languageCode));
});
print("[config] Config.languageCode: ${Config.languageCode}");
Config.save();
}
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
scrollable: true,
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(S.of(context).defaultLanguage),
groupValue: Config.languageCode,
value: Constants.defaultLanguageCodeName,
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["ar"]!),
groupValue: Config.languageCode,
value: "ar",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["cs"]!),
groupValue: Config.languageCode,
value: "cs",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["de"]!),
groupValue: Config.languageCode,
value: "de",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["en"]!),
groupValue: Config.languageCode,
value: "en",
onChanged: callback,
),
ListItemDivider(),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["es"]!),
groupValue: Config.languageCode,
value: "es",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["fa"]!),
groupValue: Config.languageCode,
value: "fa",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["fr"]!),
groupValue: Config.languageCode,
value: "fr",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["hi"]!),
groupValue: Config.languageCode,
value: "hi",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["hu"]!),
groupValue: Config.languageCode,
value: "hu",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["it"]!),
groupValue: Config.languageCode,
value: "it",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["ja"]!),
groupValue: Config.languageCode,
value: "ja",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["ko"]!),
groupValue: Config.languageCode,
value: "ko",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["pl"]!),
groupValue: Config.languageCode,
value: "pl",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["pt"]!),
groupValue: Config.languageCode,
value: "pt",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["ro"]!),
groupValue: Config.languageCode,
value: "ro",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["ru"]!),
groupValue: Config.languageCode,
value: "ru",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["tr"]!),
groupValue: Config.languageCode,
value: "tr",
onChanged: callback,
),
ListItemDivider(),
RadioListTile(
activeColor: AppTheme.switchListTileActiveColor,
title: Text(languageCodeToName["zh"]!),
groupValue: Config.languageCode,
value: "zh",
onChanged: callback,
),
ListItemDivider(),
],
);
},
),
);
},
);
}
setIsPieceCountInHandShown(bool value) async { setIsPieceCountInHandShown(bool value) async {
setState(() { setState(() {
Config.isPieceCountInHandShown = value; Config.isPieceCountInHandShown = value;