-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preferences page and select language function
Install Package: $ flutter pub add restart_app Refer to : https://github.com/DeveloperLibs/flutter_localization https://github.com/JohannesMilke/localization_arb_example https://github.com/Daniel-Ioannou/flutter_country_picker/blob/master/lib/src/utils.dart
- Loading branch information
1 parent
0b188b0
commit 875a138
Showing
23 changed files
with
546 additions
and
96 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class L10n { | ||
static final all = [ | ||
const Locale('en'), | ||
const Locale('zh'), | ||
]; | ||
static String getName(String code) { | ||
switch (code) { | ||
case 'zh': | ||
return '繁體中文'; | ||
case 'en': | ||
default: | ||
return 'English'; | ||
} | ||
} | ||
|
||
static String getCountryFlag(String code) { | ||
// See https://en.wikipedia.org/wiki/Regional_indicator_symbol | ||
switch (code) { | ||
case 'zh': | ||
return 'TW'; | ||
case 'en': | ||
default: | ||
return 'US'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart' show rootBundle; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class AppTranslations { | ||
Locale locale; | ||
static Map<dynamic, dynamic>? _localisedValues; | ||
|
||
AppTranslations(this.locale); | ||
|
||
static AppTranslations? of(BuildContext context) { | ||
return Localizations.of<AppTranslations>(context, AppTranslations); | ||
} | ||
|
||
static Future<AppTranslations> load(Locale locale) async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
var languageCode = prefs.getString('languageCode'); | ||
if (languageCode != null) { | ||
locale = Locale(languageCode); | ||
} | ||
AppTranslations appTranslations = AppTranslations(locale); | ||
String jsonContent = | ||
await rootBundle.loadString("lib/l10n/app_${locale.languageCode}.arb"); | ||
_localisedValues = json.decode(jsonContent); | ||
return appTranslations; | ||
} | ||
|
||
get currentLanguage => locale.languageCode; | ||
|
||
String text(String key) { | ||
return _localisedValues![key] ?? "$key not found"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dart:async'; | ||
import 'package:flutter/material.dart'; | ||
import 'app_translations.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
|
||
class AppTranslationsDelegate extends LocalizationsDelegate<AppTranslations> { | ||
final Locale newLocale; | ||
|
||
const AppTranslationsDelegate({required this.newLocale}); | ||
|
||
@override | ||
bool isSupported(Locale locale) { | ||
return AppLocalizations.supportedLocales.contains(locale); | ||
} | ||
|
||
@override | ||
Future<AppTranslations> load(Locale locale) { | ||
return AppTranslations.load(newLocale); | ||
} | ||
|
||
@override | ||
bool shouldReload(LocalizationsDelegate<AppTranslations> old) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'dart:ui'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
|
||
class Application { | ||
static final Application _application = Application._internal(); | ||
|
||
factory Application() => _application; | ||
|
||
Application._internal(); | ||
|
||
//returns the list of supported Locales | ||
Iterable<Locale> supportedLocales() => AppLocalizations.supportedLocales; | ||
late LocaleChangeCallback onLocaleChanged; | ||
} | ||
|
||
Application application = Application(); | ||
|
||
typedef LocaleChangeCallback = void Function(Locale locale); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'application.dart'; | ||
export 'app_translations.dart'; | ||
export 'app_translations_delegate.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.