Last active
June 19, 2020 09:59
-
-
Save flutter-clutter/d9481a1c47801498cbbc7c6cdf68e568 to your computer and use it in GitHub Desktop.
Simple app localization with JSON as input format. Used on https://www.flutterclutter.dev/flutter/tutorials/internationalization-i18n-in-flutter-an-easy-and-quick-approach/2020/213/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
class AppLocalizations { | |
AppLocalizations(this.locale); | |
static AppLocalizations of(BuildContext context) { | |
return Localizations.of<AppLocalizations>(context, AppLocalizations); | |
} | |
static const LocalizationsDelegate<AppLocalizations> delegate = | |
_AppLocalizationsDelegate(); | |
final Locale locale; | |
Map<String, String> _localizedStrings; | |
Future<void> load() async { | |
String jsonString = await rootBundle.loadString('assets/translations/${locale.languageCode}.json'); | |
Map<String, dynamic> jsonMap = json.decode(jsonString); | |
_localizedStrings = jsonMap.map((key, value) { | |
return MapEntry(key, value.toString()); | |
}); | |
return null; | |
} | |
String translate(String key) { | |
return _localizedStrings[key]; | |
} | |
} | |
class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> { | |
const _AppLocalizationsDelegate(); | |
@override | |
bool isSupported(Locale locale) { | |
return true; | |
} | |
@override | |
Future<AppLocalizations> load(Locale locale) async { | |
AppLocalizations localizations = new AppLocalizations(locale); | |
await localizations.load(); | |
return localizations; | |
} | |
@override | |
bool shouldReload(_AppLocalizationsDelegate old) => false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"string": "Beeindruckend!", | |
"string_with_interpolation": "Hey $name, wie geht es dir?" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"string": "Impressive!", | |
"string_with_interpolation": "Hey $name, how are you?" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_localizations/flutter_localizations.dart'; | |
import 'package:flutterclutter/app_localizations.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
supportedLocales: [ | |
const Locale('en', 'US'), | |
const Locale('de', 'DE'), | |
], | |
localizationsDelegates: [ | |
AppLocalizations.delegate, | |
GlobalMaterialLocalizations.delegate, | |
], | |
home: MyHomePage(title: "Flutterclutter: i18n",), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: Builder( | |
builder: (context) { | |
return Padding( | |
padding: EdgeInsets.all(16), | |
child: Center( | |
child: Text( | |
AppLocalizations.of(context).translate( | |
'string', | |
{'name': 'Test'} | |
), | |
style: TextStyle( | |
fontSize: 32 | |
), | |
) | |
) | |
); | |
} | |
) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
# To add assets to your application, add an assets section, like this: | |
assets: | |
- assets/ | |
- assets/translations/de.json # <--- THIS LINE WAS ADDED | |
- assets/translations/en.json # <--- THIS LINE WAS ADDED | |
# - images/a_dot_burr.jpeg | |
# - images/a_dot_ham.jpeg | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment