Skip to content

Commit f256918

Browse files
committed
Replaced text for keys in notification_service
Replaced text in notification service with keys for translation.
1 parent 4e50d7e commit f256918

File tree

3 files changed

+63
-45
lines changed

3 files changed

+63
-45
lines changed

assets/translations/en.json

+20
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,26 @@
219219
"disconnected": "Disconnected",
220220
"error": "Error"
221221
},
222+
"notifications": {
223+
"channel_printer_grp": "Printer {}",
224+
"channels": {
225+
"status": {
226+
"name": "Print Status Updates - {}",
227+
"desc": "Notifications regarding the print progress.",
228+
"title": "Print state of {} changed!",
229+
"body_printing": "Started to print file: \"{}\"",
230+
"body_paused": "Paused printing file: \"{}\"",
231+
"body_complete": "Finished printing: \"{}\"",
232+
"body_error": "Error while printing file: \"{}\""
233+
},
234+
"progress": {
235+
"name": "Print Progress Updates - {}",
236+
"desc": "Notifications regarding the print progress.",
237+
"title": "Print progress of {}"
238+
239+
}
240+
}
241+
},
222242
"languages": {
223243
"ab": {
224244
"name": "Abkhaz",

lib/service/notification_service.dart

+41-43
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io' show Platform;
44
import 'dart:math';
55

66
import 'package:awesome_notifications/awesome_notifications.dart';
7+
import 'package:easy_localization/easy_localization.dart';
78
import 'package:enum_to_string/enum_to_string.dart';
89
import 'package:firebase_core/firebase_core.dart';
910
import 'package:firebase_messaging/firebase_messaging.dart';
@@ -41,7 +42,7 @@ class NotificationService {
4142
await locator.allReady();
4243
await Firebase.initializeApp(
4344
options: DefaultFirebaseOptions.currentPlatform);
44-
45+
await EasyLocalization.ensureInitialized();
4546
NotificationService notificationService = locator<NotificationService>();
4647
notificationService._logger.d(
4748
"Handling a background message: ${message.messageId} with ${message.data}");
@@ -198,15 +199,17 @@ class NotificationService {
198199
return [
199200
NotificationChannel(
200201
channelKey: printerSetting.statusUpdatedChannelKey,
201-
channelName: 'Print Status Updates - ${printerSetting.name}',
202-
channelDescription: 'Notifications regarding the print progress.',
202+
channelName: tr('notifications.channels.status.name',
203+
args: [printerSetting.name]),
204+
channelDescription: tr('notifications.channels.status.desc'),
203205
channelGroupKey: printerSetting.uuid,
204206
importance: NotificationImportance.Max,
205207
defaultColor: brownish.shade500),
206208
NotificationChannel(
207209
channelKey: printerSetting.printProgressChannelKey,
208-
channelName: 'Print Progress Updates - ${printerSetting.name}',
209-
channelDescription: 'Notifications regarding the print progress.',
210+
channelName: tr('notifications.channels.progress.name',
211+
args: [printerSetting.name]),
212+
channelDescription: tr('notifications.channels.progress.desc'),
210213
channelGroupKey: printerSetting.uuid,
211214
playSound: false,
212215
enableVibration: false,
@@ -220,7 +223,8 @@ class NotificationService {
220223
PrinterSetting printerSetting) {
221224
return NotificationChannelGroup(
222225
channelGroupkey: printerSetting.uuid,
223-
channelGroupName: "Printer ${printerSetting.name}");
226+
channelGroupName: tr('notifications.channel_printer_grp',
227+
args: [printerSetting.name]));
224228
}
225229

226230
Future<void> _setupFCMOnPrinterOnceConnected(PrinterSetting setting) async {
@@ -262,57 +266,48 @@ class NotificationService {
262266
if (oldState == null && updatedState != PrintState.printing)
263267
return updatedState;
264268

269+
NotificationContent notificationContent = NotificationContent(
270+
id: Random().nextInt(20000000),
271+
channelKey: printerSetting.statusUpdatedChannelKey,
272+
title: tr('notifications.channels.status.title',
273+
args: [printerSetting.name]),
274+
notificationLayout: NotificationLayout.BigText,
275+
);
276+
265277
switch (updatedState) {
266278
case PrintState.standby:
267279
await _removePrintProgressNotification(printerSetting);
268280
break;
269281
case PrintState.printing:
270-
await _notifyAPI.createNotification(
271-
content: NotificationContent(
272-
id: Random().nextInt(20000000),
273-
channelKey: printerSetting.statusUpdatedChannelKey,
274-
title: 'Print state of ${printerSetting.name} changed!',
275-
body: 'Started to print file: "${updatedFile ?? "UNKNOWN"}"',
276-
notificationLayout: NotificationLayout.BigText,
277-
));
282+
notificationContent.body = tr(
283+
'notifications.channels.status.body_printing',
284+
args: [updatedFile ?? 'Unknown']);
278285
printerSetting.lastPrintProgress = null;
279286
break;
280287
case PrintState.paused:
281-
await _notifyAPI.createNotification(
282-
content: NotificationContent(
283-
id: Random().nextInt(20000000),
284-
channelKey: printerSetting.statusUpdatedChannelKey,
285-
title: 'Print state of ${printerSetting.name} changed!',
286-
body: 'Paused printing of file: "${updatedFile ?? "UNKNOWN"}"',
287-
notificationLayout: NotificationLayout.BigText,
288-
));
288+
notificationContent.body = tr(
289+
'notifications.channels.status.body_paused',
290+
args: [updatedFile ?? 'Unknown']);
289291
break;
290292
case PrintState.complete:
291-
await _notifyAPI.createNotification(
292-
content: NotificationContent(
293-
id: Random().nextInt(20000000),
294-
channelKey: printerSetting.statusUpdatedChannelKey,
295-
title: 'Print state of ${printerSetting.name} changed!',
296-
body: 'Finished printing "${updatedFile ?? "UNKNOWN"}"',
297-
notificationLayout: NotificationLayout.BigText,
298-
));
299-
293+
notificationContent.body = tr(
294+
'notifications.channels.status.body_complete',
295+
args: [updatedFile ?? 'Unknown']);
300296
await _removePrintProgressNotification(printerSetting);
301297
break;
302298
case PrintState.error:
303-
if (oldState == PrintState.printing)
304-
await _notifyAPI.createNotification(
305-
content: NotificationContent(
306-
id: Random().nextInt(20000000),
307-
channelKey: printerSetting.statusUpdatedChannelKey,
308-
title: 'Print state of ${printerSetting.name} changed!',
309-
body:
310-
'Error while printing file: "${updatedFile ?? "UNKNOWN"}"',
311-
notificationLayout: NotificationLayout.BigText,
312-
color: Colors.red));
299+
if (oldState == PrintState.printing) {
300+
notificationContent.body = tr(
301+
'notifications.channels.status.body_error',
302+
args: [updatedFile ?? 'Unknown']);
303+
notificationContent.color = Colors.red;
304+
}
313305
await _removePrintProgressNotification(printerSetting);
314306
break;
315307
}
308+
if (updatedState != PrintState.standby)
309+
await _notifyAPI.createNotification(content: notificationContent);
310+
316311
return updatedState;
317312
}
318313

@@ -330,7 +325,9 @@ class NotificationService {
330325
double est = printDuration / progress - printDuration;
331326
dt = DateTime.now().add(Duration(seconds: est.round()));
332327
}
333-
String eta = (dt != null) ? 'ETA:${DateFormat.Hm().format(dt)}' : '';
328+
String eta = (dt != null)
329+
? '${tr('pages.overview.general.print_card.eta')}:${DateFormat.Hm().format(dt)}'
330+
: '';
334331

335332
int index = await _machineService.indexOfMachine(printerSetting);
336333
if (index < 0) return;
@@ -340,7 +337,8 @@ class NotificationService {
340337
content: NotificationContent(
341338
id: index * 3 + 3,
342339
channelKey: printerSetting.printProgressChannelKey,
343-
title: 'Print progress of ${printerSetting.name}',
340+
title: tr('notifications.channels.progress.title',
341+
args: [printerSetting.name]),
344342
body: '$eta $progressPerc%',
345343
notificationLayout: NotificationLayout.ProgressBar,
346344
locked: true,

lib/ui/views/overview/tabs/general_tab.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ class PrintCard extends ViewModelWidget<GeneralTabViewModel> {
123123
children: [
124124
ElevatedButton(
125125
onPressed: model.onRestartKlipperPressed,
126-
child: Text('pages.overview.general.print_card.restart_klipper').tr(),
126+
child: Text('pages.overview.general.restart_klipper').tr(),
127127
),
128128
ElevatedButton(
129129
onPressed: model.onRestartMCUPressed,
130-
child: Text('pages.overview.general.print_card.restart_mcu').tr(),
130+
child: Text('pages.overview.general.restart_mcu').tr(),
131131
)
132132
],
133133
);

0 commit comments

Comments
 (0)