Created
February 24, 2019 20:25
-
-
Save hyochan/e6231ce42d1e56a0099190d13c604172 to your computer and use it in GitHub Desktop.
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: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