Skip to content

Commit

Permalink
test(i18n): system locale
Browse files Browse the repository at this point in the history
  • Loading branch information
SiongSng committed Jul 27, 2023
1 parent 447de71 commit b41a6eb
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class _AppHomeState extends State<_AppHome> {
Future<void> load() async {
if (mounted) {
final i18n = context.i18n;
i18n.setLocale(I18nLocale.getFromSystemLocale());
i18n.setLocale(I18nLocale.getFromSystemLocale(WidgetsBinding.instance.platformDispatcher));
await i18n.load();
setState(() {
isLoaded = true;
Expand Down
18 changes: 10 additions & 8 deletions app/lib/pages/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class _MainPageState extends State<MainPage> {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
showEraDialog(
context: context,
barrierDismissible: true,
dialog: const InteractiveDialog(
title: '歡迎使用',
description:
'歡迎你來到 Era Connect!\n開始使用啟動器之前,讓我們協助你進行快速設定,就可以盡情地暢玩遊戲啦。',
logoBoxText: '01',
));
context: context,
barrierDismissible: true,
dialog: const InteractiveDialog(
title: '歡迎使用',
description:
'歡迎你來到 Era Connect!\n開始使用啟動器之前,讓我們協助你進行快速設定,就可以盡情地暢玩遊戲啦。',
logoBoxText: '01',
child: Placeholder(),
),
);
}
});
}
Expand Down
7 changes: 4 additions & 3 deletions packages/era_connect_i18n/lib/i18n_locale.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter/widgets.dart';
import 'dart:ui' as ui;

/// An enum that represents the supported locales for the app.
enum I18nLocale {
Expand All @@ -10,8 +10,9 @@ enum I18nLocale {

const I18nLocale(this.code);

static I18nLocale getFromSystemLocale() {
final systemLocale = WidgetsBinding.instance.platformDispatcher.locale;
static I18nLocale getFromSystemLocale(
ui.PlatformDispatcher platformDispatcher) {
final systemLocale = platformDispatcher.locale;
final languageTag = systemLocale.toLanguageTag();

return I18nLocale.values.firstWhere(
Expand Down
2 changes: 2 additions & 0 deletions packages/era_connect_i18n/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
mockito: ^5.4.2
build_runner: ^2.4.6

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
37 changes: 37 additions & 0 deletions packages/era_connect_i18n/test/i18n_locale_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:era_connect_i18n/i18n_locale.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'dart:ui' as ui;

import 'i18n_locale_test.mocks.dart';

@GenerateMocks([ui.PlatformDispatcher])
void main() {
group('I18nLocale', () {
setUp(() => WidgetsFlutterBinding.ensureInitialized());

test(
'getFromSystemLocale returns americanEnglish when system locale is not supported',
() {
final mockPlatformDispatcher = MockPlatformDispatcher();
when(mockPlatformDispatcher.locale).thenReturn(Locale('fr', 'FR'));

final result = I18nLocale.getFromSystemLocale(mockPlatformDispatcher);

expect(result, I18nLocale.americanEnglish);
});

test(
'getFromSystemLocale returns the correct locale when system locale is supported',
() {
final mockPlatformDispatcher = MockPlatformDispatcher();
when(mockPlatformDispatcher.locale).thenReturn(Locale('zh', 'TW'));

final result = I18nLocale.getFromSystemLocale(mockPlatformDispatcher);

expect(result, I18nLocale.traditionalChineseTW);
});
});
}
Loading

0 comments on commit b41a6eb

Please sign in to comment.