Skip to content

Commit

Permalink
Merge pull request #76 from mario-bermonti:mario-bermonti/issue75
Browse files Browse the repository at this point in the history
Translate demographics survey elements into Spanish
  • Loading branch information
mario-bermonti authored Sep 10, 2024
2 parents 633d7c3 + d9c8eb9 commit cb7da96
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
2 changes: 2 additions & 0 deletions assets/lang/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
8 changes: 8 additions & 0 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:mdigit_span_tasks_ema/src/routes.dart';
import 'package:research_package/research_package.dart';

import 'intl/internationalization.dart';

class MyApp extends StatelessWidget {
const MyApp({super.key});
Expand All @@ -13,6 +17,10 @@ class MyApp extends StatelessWidget {
initialRoute: '/',
theme: ThemeData(primarySwatch: Colors.grey),
getPages: routes,
locale: locale,
supportedLocales: supportedLocales,
localizationsDelegates: localizationsDelegates,
localeResolutionCallback: resolveLocale,
);
}
}
33 changes: 33 additions & 0 deletions lib/src/intl/internationalization.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:research_package/research_package.dart';

/// default locale
const Locale locale = Locale('es');

const List<Locale> supportedLocales = <Locale>[
Locale('es'),
Locale('en'),
];

final List<LocalizationsDelegate> localizationsDelegates =
<LocalizationsDelegate>[
RPLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
];

/// Resolves the [Locale] to the last one in the list of [supportedLocales]
/// if the [locale] is unsupported.
Locale? resolveLocale(Locale? locale, Iterable<Locale>? supportedLocales) {
if (locale == null || supportedLocales == null) {
return null;
}
for (Locale supportedLocale in supportedLocales) {
if (supportedLocale.languageCode == locale.languageCode) {
return supportedLocale;
}
}
return supportedLocales.last;
}
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies:
get_storage: ^2.1.1
freezed_annotation: ^2.4.2
json_annotation: ^4.9.0
flutter_localizations:
sdk: flutter

dev_dependencies:
flutter_test:
Expand All @@ -49,6 +51,7 @@ flutter:
uses-material-design: true
assets:
- assets/demographics_questions/
- assets/lang/

flutter_launcher_icons:
android: true
Expand Down
59 changes: 59 additions & 0 deletions test/src/intl/internationalization_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mdigit_span_tasks_ema/src/intl/internationalization.dart';

void main() {
group('resolveLocale', () {
test(
"Given valid locale and supported locales, returns the locale.",
() {
const Locale expectedLocale = Locale('en');
final Iterable<Locale> supportedLocales = <Locale>[
const Locale('en'),
const Locale('es'),
];

final Locale? actualLocale = resolveLocale(
expectedLocale,
supportedLocales,
);

expect(actualLocale, expectedLocale);
},
);
});
test(
"""Given an unsupported locale and valid supported locales,
returns Locale('es').""",
() {
const Locale locale = Locale('fr');
final Iterable<Locale> supportedLocales = <Locale>[
const Locale('en'),
const Locale('es'),
];

final Locale? actualLocale = resolveLocale(
locale,
supportedLocales,
);

expect(actualLocale, supportedLocales.last);
},
);
test(
"Given a null locale and valid supported locales, returns null.",
() {
final Iterable<Locale> supportedLocales = <Locale>[
const Locale('en'),
const Locale('es'),
];

final Locale? actualLocale = resolveLocale(
null,
supportedLocales,
);

expect(actualLocale, isNull);
},
);
}

0 comments on commit cb7da96

Please sign in to comment.