Remove json translations approach, add example for adding language
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -1,10 +1,35 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
|
||||
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
|
||||
import 'package:stream_chat_localizations/stream_chat_localizations.dart';
|
||||
|
||||
final chatPersistentClient = StreamChatPersistenceClient(
|
||||
logLevel: Level.INFO,
|
||||
);
|
||||
/// A custom set of localizations for the 'hi' locale.
|
||||
class StreamChatLocalizationsHi extends GlobalStreamChatLocalizations {
|
||||
const StreamChatLocalizationsHi() : super(localeName: 'hi');
|
||||
|
||||
static const LocalizationsDelegate<StreamChatLocalizations> delegate =
|
||||
_HindiStreamChatLocalizationsDelegate();
|
||||
|
||||
@override
|
||||
String get launchUrlError => 'URL लॉन्च नहीं कर सकता';
|
||||
}
|
||||
|
||||
class _HindiStreamChatLocalizationsDelegate
|
||||
extends LocalizationsDelegate<StreamChatLocalizations> {
|
||||
const _HindiStreamChatLocalizationsDelegate();
|
||||
|
||||
@override
|
||||
bool isSupported(Locale locale) => locale.languageCode == 'hi';
|
||||
|
||||
@override
|
||||
Future<StreamChatLocalizations> load(Locale locale) =>
|
||||
SynchronousFuture(const StreamChatLocalizationsHi());
|
||||
|
||||
@override
|
||||
bool shouldReload(_HindiStreamChatLocalizationsDelegate old) => false;
|
||||
}
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
@@ -14,7 +39,7 @@ void main() async {
|
||||
final client = StreamChatClient(
|
||||
's2dxdhpxd94g',
|
||||
logLevel: Level.INFO,
|
||||
)..chatPersistenceClient = chatPersistentClient;
|
||||
);
|
||||
|
||||
/// Set the current user and connect the websocket. In a production scenario, this should be done using
|
||||
/// a backend to generate a user token using our server SDK.
|
||||
@@ -58,6 +83,17 @@ class MyApp extends StatelessWidget {
|
||||
theme: ThemeData.light(),
|
||||
darkTheme: ThemeData.dark(),
|
||||
themeMode: ThemeMode.system,
|
||||
supportedLocales: [
|
||||
Locale('en', 'US'),
|
||||
Locale('hi', 'IN'),
|
||||
],
|
||||
localizationsDelegates: [
|
||||
GlobalStreamChatLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
StreamChatLocalizationsHi.delegate,
|
||||
],
|
||||
builder: (context, widget) {
|
||||
return StreamChat(
|
||||
client: client,
|
||||
|
||||
@@ -29,6 +29,8 @@ dependencies:
|
||||
# path: ../../stream_chat_flutter_core
|
||||
stream_chat_flutter:
|
||||
path: ../
|
||||
stream_chat_localizations:
|
||||
path: ../../stream_chat_localizations
|
||||
stream_chat_persistence:
|
||||
path: ../../stream_chat_persistence
|
||||
|
||||
|
||||
@@ -104,11 +104,7 @@ extension BuildContextX on BuildContext {
|
||||
double get textScaleFactor =>
|
||||
MediaQuery.maybeOf(this)?.textScaleFactor ?? 1.0;
|
||||
|
||||
String translate({
|
||||
required String key,
|
||||
required String defaultValue,
|
||||
}) =>
|
||||
StreamChatLocalizations.of(this)?.translate(key) ?? defaultValue;
|
||||
StreamChatLocalizations? get translations => StreamChatLocalizations.of(this);
|
||||
}
|
||||
|
||||
/// Extension on [BorderRadius]
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
|
||||
import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';
|
||||
import 'package:stream_chat_flutter/src/extension.dart';
|
||||
|
||||
/// Text widget to display in message
|
||||
class MessageText extends StatelessWidget {
|
||||
@@ -29,6 +30,8 @@ class MessageText extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final texts = context.translations?.launchUrlError ?? 'defaultValue';
|
||||
return Text(texts);
|
||||
final text = _replaceMentions(message.text ?? '').replaceAll('\n', '\n\n');
|
||||
|
||||
final themeData = Theme.of(context);
|
||||
|
||||
@@ -71,9 +71,6 @@ import 'package:flutter/widgets.dart';
|
||||
/// * [GlobalStreamChatLocalizations], which provides material localizations
|
||||
/// for many languages.
|
||||
abstract class StreamChatLocalizations {
|
||||
///
|
||||
String? translate(String key);
|
||||
|
||||
/// The `StreamChatLocalizations` from the closest [Localizations] instance
|
||||
/// that encloses the given context.
|
||||
///
|
||||
@@ -94,4 +91,6 @@ abstract class StreamChatLocalizations {
|
||||
context,
|
||||
StreamChatLocalizations,
|
||||
);
|
||||
|
||||
String get launchUrlError;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ issue_tracker: https://github.com/GetStream/stream-chat-flutter/issues
|
||||
|
||||
environment:
|
||||
sdk: '>=2.12.0 <3.0.0'
|
||||
flutter: ">=1.17.0"
|
||||
|
||||
dependencies:
|
||||
cached_network_image: ^3.0.0
|
||||
|
||||
Reference in New Issue
Block a user