Skip to content

Commit dafd4d6

Browse files
committed
feat(feed_decorators): add localization extension for feed decorator types
- Create new extension on FeedDecoratorType for localized strings - Implement randomized string selection for titles, descriptions, and CTAs - Add support for multiple languages through AppLocalizations - Improve maintainability and clean up UI components by centralizing string logic
1 parent 6acd616 commit dafd4d6

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import 'dart:math';
2+
3+
import 'package:core/core.dart';
4+
import 'package:flutter_news_app_mobile_client_full_source_code/l10n/app_localizations.dart';
5+
6+
/// An extension on [FeedDecoratorType] to provide randomized, localized
7+
/// strings for decorator titles, descriptions, and calls to action.
8+
///
9+
/// This centralizes the logic for selecting from multiple string variations,
10+
/// making the UI components cleaner and the string management more maintainable.
11+
extension FeedDecoratorTypeL10n on FeedDecoratorType {
12+
/// Returns a random string from a list of string-producing functions.
13+
///
14+
/// This helper function is used to select a random variation of a
15+
/// localized string. It takes a list of functions, each returning a string,
16+
/// and executes one at random.
17+
String _randomString(List<String> options) {
18+
if (options.isEmpty) return '';
19+
final randomIndex = Random().nextInt(options.length);
20+
return options[randomIndex];
21+
}
22+
23+
/// Gets a randomized, localized title for the decorator.
24+
///
25+
/// Uses a switch statement on the decorator type to determine the correct
26+
/// set of title variations from [AppLocalizations] and then selects one
27+
/// randomly.
28+
String getRandomTitle(AppLocalizations l10n) {
29+
switch (this) {
30+
case FeedDecoratorType.linkAccount:
31+
return _randomString([
32+
l10n.decoratorLinkAccountTitle_1,
33+
l10n.decoratorLinkAccountTitle_2,
34+
]);
35+
case FeedDecoratorType.upgrade:
36+
return _randomString([
37+
l10n.decoratorUpgradeTitle_1,
38+
l10n.decoratorUpgradeTitle_2,
39+
]);
40+
case FeedDecoratorType.rateApp:
41+
return _randomString([
42+
l10n.decoratorRateAppTitle_1,
43+
l10n.decoratorRateAppTitle_2,
44+
]);
45+
case FeedDecoratorType.enableNotifications:
46+
return _randomString([
47+
l10n.decoratorEnableNotificationsTitle_1,
48+
l10n.decoratorEnableNotificationsTitle_2,
49+
]);
50+
case FeedDecoratorType.suggestedTopics:
51+
return _randomString([
52+
l10n.decoratorSuggestedTopicsTitle_1,
53+
l10n.decoratorSuggestedTopicsTitle_2,
54+
]);
55+
case FeedDecoratorType.suggestedSources:
56+
return _randomString([
57+
l10n.decoratorSuggestedSourcesTitle_1,
58+
l10n.decoratorSuggestedSourcesTitle_2,
59+
]);
60+
}
61+
}
62+
63+
/// Gets a randomized, localized description for the decorator.
64+
///
65+
/// This only applies to [FeedDecoratorCategory.callToAction] decorators.
66+
/// It returns an empty string for content collection types.
67+
String getRandomDescription(AppLocalizations l10n) {
68+
switch (this) {
69+
case FeedDecoratorType.linkAccount:
70+
return _randomString([
71+
l10n.decoratorLinkAccountDescription_1,
72+
l10n.decoratorLinkAccountDescription_2,
73+
]);
74+
case FeedDecoratorType.upgrade:
75+
return _randomString([
76+
l10n.decoratorUpgradeDescription_1,
77+
l10n.decoratorUpgradeDescription_2,
78+
]);
79+
case FeedDecoratorType.rateApp:
80+
return _randomString([
81+
l10n.decoratorRateAppDescription_1,
82+
l10n.decoratorRateAppDescription_2,
83+
]);
84+
case FeedDecoratorType.enableNotifications:
85+
return _randomString([
86+
l10n.decoratorEnableNotificationsDescription_1,
87+
l10n.decoratorEnableNotificationsDescription_2,
88+
]);
89+
case FeedDecoratorType.suggestedTopics:
90+
case FeedDecoratorType.suggestedSources:
91+
return '';
92+
}
93+
}
94+
95+
/// Gets a randomized, localized call-to-action text for the decorator.
96+
///
97+
/// This only applies to [FeedDecoratorCategory.callToAction] decorators.
98+
/// It returns an empty string for content collection types.
99+
String getRandomCtaText(AppLocalizations l10n) {
100+
switch (this) {
101+
case FeedDecoratorType.linkAccount:
102+
return _randomString([
103+
l10n.decoratorLinkAccountCta_1,
104+
l10n.decoratorLinkAccountCta_2,
105+
]);
106+
case FeedDecoratorType.upgrade:
107+
return _randomString([
108+
l10n.decoratorUpgradeCta_1,
109+
l10n.decoratorUpgradeCta_2,
110+
]);
111+
case FeedDecoratorType.rateApp:
112+
return _randomString([
113+
l10n.decoratorRateAppCta_1,
114+
l10n.decoratorRateAppCta_2,
115+
]);
116+
case FeedDecoratorType.enableNotifications:
117+
return _randomString([
118+
l10n.decoratorEnableNotificationsCta_1,
119+
l10n.decoratorEnableNotificationsCta_2,
120+
]);
121+
case FeedDecoratorType.suggestedTopics:
122+
case FeedDecoratorType.suggestedSources:
123+
return '';
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)