Translate week day names in workout screens

This commit is contained in:
Roland Geider
2021-04-23 19:09:25 +02:00
parent c5fa7c0955
commit 3c21650003
5 changed files with 37 additions and 4 deletions

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:intl/intl.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:wger/models/workouts/set.dart';
@@ -64,6 +65,19 @@ class Day {
return daysOfWeek.map((e) => getDayName(e)).join(', ');
}
String getDaysTextTranslated(locale) {
return daysOfWeek.map((e) => getDayTranslated(e, locale)).join(', ');
}
/// Returns the translated name of the given day
String getDayTranslated(int day, locale) {
// Isn't there another way?... 🙄
final now = DateTime.now();
final firstDayOfWeek = now.subtract(Duration(days: now.weekday));
return DateFormat(DateFormat.WEEKDAY, locale).format(firstDayOfWeek.add(Duration(days: day)));
}
// Boilerplate
factory Day.fromJson(Map<String, dynamic> json) => _$DayFromJson(json);
Map<String, dynamic> toJson() => _$DayToJson(this);

View File

@@ -196,7 +196,7 @@ class DayHeaderDismissible extends StatelessWidget {
style: Theme.of(context).textTheme.headline5,
overflow: TextOverflow.ellipsis,
),
Text(_day.getDaysText),
Text(_day.getDaysTextTranslated(Localizations.localeOf(context).languageCode)),
],
),
),

View File

@@ -113,7 +113,10 @@ class _DayCheckboxState extends State<DayCheckbox> {
Widget build(BuildContext context) {
return CheckboxListTile(
key: Key('field-checkbox-${widget._dayNr}'),
title: Text(widget._day.getDayName(widget._dayNr)),
title: Text(widget._day.getDayTranslated(
widget._dayNr,
Localizations.localeOf(context).languageCode,
)),
value: _isSelected,
onChanged: (bool? newValue) {
setState(() {

View File

@@ -82,4 +82,20 @@ void main() {
//await tester.pumpAndSettle();
//verify(mockObserver.didPop(any, any));
});
testWidgets('Tests the localization of days - EN', (WidgetTester tester) async {
await tester.pumpWidget(createHomeScreen());
expect(find.text('Monday'), findsOneWidget);
expect(find.text('Tuesday'), findsOneWidget);
expect(find.text('Thursday'), findsOneWidget);
});
testWidgets('Tests the localization of days - DE', (WidgetTester tester) async {
await tester.pumpWidget(createHomeScreen(locale: 'de'));
expect(find.text('Montag'), findsOneWidget);
expect(find.text('Dienstag'), findsOneWidget);
expect(find.text('Donnerstag'), findsOneWidget);
});
}

View File

@@ -80,7 +80,7 @@ void main() {
await tester.tap(find.byType(TextButton));
await tester.pumpAndSettle();
expect(find.text('Monday, Tuesday'), findsOneWidget);
expect(find.text('Thursday'), findsOneWidget);
expect(find.text('Montag, Dienstag'), findsOneWidget);
expect(find.text('Donnerstag'), findsOneWidget);
});
}