forked from ilteoood/flutter_i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_example.dart
83 lines (75 loc) · 2.3 KB
/
namespace_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
Future main() async {
final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate(
translationLoader: NamespaceFileTranslationLoader(
namespaces: ["common", "home"],
useCountryCode: false,
fallbackDir: 'en',
basePath: 'assets/i18n_namespace',
forcedLocale: Locale('it'),
),
missingTranslationHandler: (key, locale) {
print("--- Missing Key: $key, languageCode: ${locale!.languageCode}");
},
);
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp(flutterI18nDelegate));
}
class MyApp extends StatelessWidget {
final FlutterI18nDelegate flutterI18nDelegate;
MyApp(this.flutterI18nDelegate);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.deepPurple,
),
home: MyHomePage(),
localizationsDelegates: [
flutterI18nDelegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
);
}
}
class MyHomePage extends StatefulWidget {
@override
MyHomeState createState() => MyHomeState();
}
class MyHomeState extends State<MyHomePage> {
changeLanguage() async {
final currentLang = FlutterI18n.currentLocale(context)!;
final nextLang =
currentLang.languageCode == 'ua' ? Locale('en') : Locale('ua');
await FlutterI18n.refresh(context, nextLang);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:
AppBar(title: Text(FlutterI18n.translate(context, "common.title"))),
body: Builder(builder: (BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
I18nText("home.label.main", child: Text("")),
I18nText("home.missing", child: Text("")),
ElevatedButton(
onPressed: () async {
await changeLanguage();
},
child: Text("Change language"))
],
),
);
}),
);
}
}