Skip to content

Instantly share code, notes, and snippets.

@hyochan
Created February 24, 2019 20:25
Show Gist options
  • Save hyochan/e6231ce42d1e56a0099190d13c604172 to your computer and use it in GitHub Desktop.
Save hyochan/e6231ce42d1e56a0099190d13c604172 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Localization {
Localization(this._locale);
final Locale _locale;
static Localization of(BuildContext context) {
return Localizations.of<Localization>(context, Localization);
}
Map<String, dynamic> _sentences;
Future<bool> load() async {
String data = await rootBundle.loadString('res/langs/${_locale.languageCode}.json');
this._sentences = json.decode(data);
return true;
}
String trans(String key) {
if (key == null) {
return '...';
}
return this._sentences[key];
}
}
class LocalizationDelegate extends LocalizationsDelegate<Localization> {
const LocalizationDelegate();
@override
bool isSupported(Locale locale) => ['en', 'ko'].contains(locale.languageCode);
@override
Future<Localization> load(Locale locale) async {
Localization localizations = new Localization(locale);
await localizations.load();
return localizations;
}
@override
bool shouldReload(LocalizationDelegate old) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment