Skip to content

Instantly share code, notes, and snippets.

@flutter-clutter
Last active June 19, 2020 09:59
Show Gist options
  • Save flutter-clutter/d9481a1c47801498cbbc7c6cdf68e568 to your computer and use it in GitHub Desktop.
Save flutter-clutter/d9481a1c47801498cbbc7c6cdf68e568 to your computer and use it in GitHub Desktop.
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;
}
{
"string": "Beeindruckend!",
"string_with_interpolation": "Hey $name, wie geht es dir?"
}
{
"string": "Impressive!",
"string_with_interpolation": "Hey $name, how are you?"
}
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
),
)
)
);
}
)
);
}
}
...
# 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