feat(home): localize greetings and address profile names

This commit is contained in:
zarzet committed 2026-10-18 03:11:44 +07:00
1 parent 1a6dc7366e
commit 4cee16e9e8
5 files changed
+132 -3

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
import 'dart:math';
import 'package:spotiflac_android/l10n/app_localizations.dart';
/// Picked once per launch: the greeting holds still while browsing and
/// changes the next time the app opens.
final int _sessionVariant = Random().nextInt(1 << 16);
/// A time-of-day Home greeting, addressed by the profile's first name when
/// one is set.
String homeGreeting(
AppLocalizations l10n, {
String name = '',
DateTime? now,
int? variant,
}) {
final hour = (now ?? DateTime.now()).hour;
final options = switch (hour) {
>= 5 && < 12 => [
l10n.homeGreetingMorning1,
l10n.homeGreetingMorning2,
l10n.homeGreetingMorning3,
],
>= 12 && < 17 => [
l10n.homeGreetingAfternoon1,
l10n.homeGreetingAfternoon2,
l10n.homeGreetingAfternoon3,
],
>= 17 && < 21 => [
l10n.homeGreetingEvening1,
l10n.homeGreetingEvening2,
l10n.homeGreetingEvening3,
],
_ => [
l10n.homeGreetingNight1,
l10n.homeGreetingNight2,
l10n.homeGreetingNight3,
],
};
final phrase = options[(variant ?? _sessionVariant) % options.length];
final trimmed = name.trim();
if (trimmed.isEmpty) return phrase;
// A full name reads stiff in a headline.
return l10n.homeGreetingNamed(phrase, trimmed.split(RegExp(r'\s+')).first);
}