Skip to content

Commit

Permalink
Language change at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Oct 29, 2018
1 parent b9f6e40 commit a7d0ef9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .packages
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by pub on 2018-09-21 20:30:26.757096.
# Generated by pub on 2018-10-29 21:46:18.459763.
analyzer:file:///home/teo/snap/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.32.4/lib/
args:file:///home/teo/snap/flutter/.pub-cache/hosted/pub.dartlang.org/args-1.5.0/lib/
async:file:///home/teo/snap/flutter/.pub-cache/hosted/pub.dartlang.org/async-2.0.8/lib/
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ Where:
From version ***0.2.0*** *flutter_i18n* manage strings that contain parameters; an example can be: "Hello, ***{user}***!"
For a correct translation, you must use the third parameter of the *translate* method, a *Map<String, String>* where:
- the keys are the placeholders used in the *.json* file (i.e. user)
- the values are what you want to display
- the values are what you want to display

From version ***0.3.0*** *flutter_i18n* supports language change at runtime. To use it, you ***must*** invoke the method
```sh
await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
```

***NOTE***: *countryCode* is optional.
10 changes: 10 additions & 0 deletions example/assets/flutter_i18n/it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"title": "App di test",
"label":{
"main": "Ciao {user}!"
},
"button":{
"clickMe": "Premi qui"
},
"toastMessage": "Hai premuto un bottone!"
}
28 changes: 22 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@ class MyApp extends StatelessWidget {
}
}

class MyHomePage extends StatelessWidget {
class MyHomePage extends StatefulWidget {
@override
MyHomeState createState() => new MyHomeState();
}

class MyHomeState extends State<MyHomePage> {
String currentLang = 'en';

switchLang() {
setState(() {
currentLang = currentLang == 'en' ? 'it' : 'en';
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
Expand All @@ -34,13 +47,16 @@ class MyHomePage extends StatelessWidget {
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(FlutterI18n.translate(context, "label.main", Map.fromIterables(["user"], ["Flutter lover"]))),
new Text(FlutterI18n.translate(context, "label.main",
Map.fromIterables(["user"], ["Flutter lover"]))),
new FlatButton(
onPressed: () {
onPressed: () async {
switchLang();
await FlutterI18n.refresh(context, currentLang);
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text(
FlutterI18n.translate(context, "toastMessage")),
));
content: new Text(
FlutterI18n.translate(context, "toastMessage")),
));
},
child: new Text(
FlutterI18n.translate(context, "button.clickMe")))
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.3"
version: "0.3.0"
flutter_localizations:
dependency: transitive
description: flutter
Expand Down
17 changes: 14 additions & 3 deletions lib/flutter_i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class FlutterI18n {
return true;
}

Future _loadCurrentTranslation() async {
this.locale = await _findCurrentLocale();
Future _loadCurrentTranslation([final Locale locale]) async {
this.locale = locale != null ? locale : await _findCurrentLocale();
await _loadFile(_composeFileName());
}

Expand All @@ -52,6 +52,13 @@ class FlutterI18n {
() => Locale(systemLocaleSplitted[0], systemLocaleSplitted[1]));
}

static Future refresh(final BuildContext context, final String languageCode,
{final String countryCode}) async {
final Locale forcedLocale = new Locale(languageCode, countryCode);
final FlutterI18n currentInstance = retrieveCurrentInstance(context);
await currentInstance._loadCurrentTranslation(forcedLocale);
}

static String translate(final BuildContext context, final String key,
[final Map<String, String> translationParams]) {
String translation = _translateWithKeyFallback(context, key);
Expand All @@ -72,7 +79,7 @@ class FlutterI18n {

static String _translateWithKeyFallback(BuildContext context, String key) {
final Map<String, dynamic> decodedStrings =
Localizations.of<FlutterI18n>(context, FlutterI18n).decodedMap;
retrieveCurrentInstance(context).decodedMap;
final List<String> splittedKey = key.split(".");
String translation = _decodeFromMap(decodedStrings, splittedKey);
if (translation == null) {
Expand All @@ -81,6 +88,10 @@ class FlutterI18n {
return translation;
}

static FlutterI18n retrieveCurrentInstance(BuildContext context) {
return Localizations.of<FlutterI18n>(context, FlutterI18n);
}

static String _decodeFromMap(
Map<String, dynamic> decodedStrings, final List<String> splittedKey) {
for (final String keyPart in splittedKey) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_i18n
description: i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json file!
version: 0.2.0
version: 0.3.0
author: Matteo Pietro Dazzi <[email protected]>
homepage: https://github.com/ilteoood/flutter_i18n

Expand Down

0 comments on commit a7d0ef9

Please sign in to comment.