Last active
September 3, 2018 13:51
-
-
Save rohan20/01e8c1a8dbc0a49326183ffe0eab0098 to your computer and use it in GitHub Desktop.
flutter-localizations
This file contains hidden or 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 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart' show rootBundle; | |
class AppTranslations { | |
Locale locale; | |
static Map<dynamic, dynamic> _localisedValues; | |
AppTranslations(Locale locale) { | |
this.locale = locale; | |
_localisedValues = null; | |
} | |
static AppTranslations of(BuildContext context) { | |
return Localizations.of<AppTranslations>(context, AppTranslations); | |
} | |
static Future<AppTranslations> load(Locale locale) async { | |
AppTranslations appTranslations = AppTranslations(locale); | |
String jsonContent = | |
await rootBundle.loadString("assets/locale/localization_${locale.languageCode}.json"); | |
_localisedValues = json.decode(jsonContent); | |
return appTranslations; | |
} | |
get currentLanguage => locale.languageCode; | |
String text(String key) { | |
return _localisedValues[key] ?? "$key not found"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment