Skip to content

Commit e3b210f

Browse files
authored
fix: event languages (#1335)
* fix: event languages * fix: notifier * fix: import * fix: missed changes * fix: wording
1 parent a2a81ca commit e3b210f

3 files changed

Lines changed: 64 additions & 6 deletions

File tree

lib/components/chat_history/twitch/subscription_event.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class TwitchSubscriptionEventWidget extends StatelessWidget {
2828

2929
@override
3030
Widget build(BuildContext context) {
31+
final userModel = Provider.of<UserModel>(context);
32+
final channelLocale = userModel.getChannelLocale(context);
33+
final localizations = lookupAppLocalizations(channelLocale);
34+
3135
return DecoratedEventWidget.icon(
3236
icon: Icons.star,
3337
child: StyledText(
34-
text: AppLocalizations.of(context)!.subscriptionEvent(
38+
text: localizations.subscriptionEvent(
3539
model.subscriberUserName, model.tier.replaceAll("000", "")),
3640
tags: {
3741
'b': StyledTextTag(style: Theme.of(context).textTheme.titleSmall),
@@ -52,10 +56,14 @@ class TwitchSubscriptionGiftEventWidget extends StatelessWidget {
5256

5357
@override
5458
Widget build(BuildContext context) {
59+
final userModel = Provider.of<UserModel>(context);
60+
final channelLocale = userModel.getChannelLocale(context);
61+
final localizations = lookupAppLocalizations(channelLocale);
62+
5563
return DecoratedEventWidget.icon(
5664
icon: Icons.redeem,
5765
child: StyledText(
58-
text: AppLocalizations.of(context)!.subscriptionGiftEvent(
66+
text: localizations.subscriptionGiftEvent(
5967
model.gifterUserName,
6068
model.total,
6169
model.tier.replaceAll("000", ""),
@@ -126,11 +134,15 @@ class TwitchSubscriptionMessageEventWidget extends StatelessWidget {
126134
@override
127135
Widget build(BuildContext context) {
128136
final textTheme = Theme.of(context).textTheme.titleSmall;
137+
final userModel = Provider.of<UserModel>(context);
138+
final channelLocale = userModel.getChannelLocale(context);
139+
final localizations = lookupAppLocalizations(channelLocale);
140+
129141
return DecoratedEventWidget.icon(
130142
icon: Icons.star,
131143
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
132144
StyledText(
133-
text: AppLocalizations.of(context)!.subscriptionMessageEvent(
145+
text: localizations.subscriptionMessageEvent(
134146
model.subscriberUserName,
135147
model.cumulativeMonths,
136148
model.tier.replaceAll("000", ""),

lib/models/channels.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ class Channel {
66
final String provider;
77
final String channelId;
88
final String displayName;
9+
final String? language;
910

10-
Channel(this.provider, this.channelId, this.displayName);
11+
Channel(this.provider, this.channelId, this.displayName, {this.language});
1112

1213
@override
1314
bool operator ==(other) =>
1415
other is Channel &&
1516
other.provider == provider &&
16-
other.channelId == channelId;
17+
other.channelId == channelId &&
18+
other.displayName == displayName;
1719

1820
@override
1921
int get hashCode => provider.hashCode ^ channelId.hashCode;
@@ -35,6 +37,7 @@ class Channel {
3537
'provider': provider,
3638
'channelId': channelId,
3739
'displayName': displayName,
40+
'language': language,
3841
};
3942
}
4043
}

lib/models/user.dart

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'dart:async';
33
import 'package:firebase_analytics/firebase_analytics.dart';
44
import 'package:firebase_auth/firebase_auth.dart';
55
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
6-
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/cupertino.dart';
77
import 'package:rtchat/models/adapters/profiles.dart';
88
import 'package:rtchat/models/channels.dart';
99

@@ -16,6 +16,7 @@ class UserModel extends ChangeNotifier {
1616
User? _user;
1717
Channel? _userChannel;
1818
Channel? _activeChannel;
19+
Locale? _channelLocale;
1920

2021
UserModel() {
2122
_userSubscription =
@@ -65,9 +66,51 @@ class UserModel extends ChangeNotifier {
6566

6667
set activeChannel(Channel? channel) {
6768
_activeChannel = channel;
69+
setChannelLanguage(channel?.language);
6870
notifyListeners();
6971
}
7072

73+
void setChannelLanguage(String? languageCode) {
74+
if (languageCode != null) {
75+
_channelLocale = Locale(normalizeLanguageCode(languageCode));
76+
} else {
77+
_channelLocale = null;
78+
}
79+
notifyListeners();
80+
}
81+
82+
Locale getChannelLocale(BuildContext context) {
83+
if (_activeChannel?.language != null) {
84+
return _channelLocale ??
85+
Locale(normalizeLanguageCode(_activeChannel!.language!));
86+
}
87+
// Fallback to the device locale
88+
return Localizations.localeOf(context);
89+
}
90+
91+
static String normalizeLanguageCode(String? twitchLang) {
92+
final mappings = {
93+
'en': 'en',
94+
'es': 'es',
95+
'fr': 'fr',
96+
'de': 'de',
97+
'it': 'it',
98+
'ar': 'ar',
99+
'bn': 'bn',
100+
'ja': 'ja',
101+
'ko': 'ko',
102+
'nl': 'nl',
103+
'pl': 'pl',
104+
'pt': 'pt',
105+
'ru': 'ru',
106+
'sv': 'sv',
107+
'uk': 'uk',
108+
'zh': 'zh',
109+
'zh-Hant': 'zh',
110+
};
111+
return mappings[twitchLang] ?? 'en';
112+
}
113+
71114
Uri? get activityFeedUri {
72115
final channel = _userChannel;
73116
if (channel == null) {

0 commit comments

Comments
 (0)