-
-
Save flutter-devs/a05669afe1d21bffee3ea6c76d83a45f to your computer and use it in GitHub Desktop.
Flutter Localization Gist
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 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/foundation.dart' show SynchronousFuture; | |
class AppLocalizations { | |
AppLocalizations(this.locale); | |
final Locale locale; | |
static AppLocalizations of(BuildContext context) { | |
return Localizations.of<AppLocalizations>(context, AppLocalizations); | |
} | |
static Map<String, Map<String, String>> _localizedValues = { | |
'en': { | |
'appName': 'Localization Demo', | |
'appNameShort': 'Localization', | |
'title': 'FlutterDevs', | |
'desc': 'FlutterDevs intent to deliver Flutter apps with ' | |
'high quality. We’ve adopted Design First attitude ' | |
'which helps us deliver applications of highest quality.', | |
}, | |
'hi': { | |
'appName': 'लोकलाइजेशन डेमो', | |
'appNameShort': 'लोकलाइजेशन', | |
'title': 'फ्लूटेरडेव्स', | |
'desc': 'FlutterDevs उच्च गुणवत्ता वाले Flutter ऐप्स वितरित करने ' | |
'का इरादा रखता है। हमने डिजाइन फर्स्ट रवैया अपनाया है जो हमें ' | |
'उच्चतम गुणवत्ता के अनुप्रयोगों को वितरित करने में मदद करता है।', | |
}, | |
}; | |
String get appName { | |
return _localizedValues[locale.languageCode]['appName']; | |
} | |
String get appNameShort { | |
return _localizedValues[locale.languageCode]['appNameShort']; | |
} | |
String get title { | |
return _localizedValues[locale.languageCode]['title']; | |
} | |
String get desc { | |
return _localizedValues[locale.languageCode]['desc']; | |
} | |
} | |
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> { | |
const AppLocalizationsDelegate(); | |
@override | |
bool isSupported(Locale locale) => ['en', 'hi'].contains(locale.languageCode); | |
@override | |
Future<AppLocalizations> load(Locale locale) { | |
return SynchronousFuture<AppLocalizations>(AppLocalizations(locale)); | |
} | |
@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
import 'package:flutter_localization_app/localization/localizations.dart'; |
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
localizationsDelegates: [ | |
AppLocalizationsDelegate(), | |
GlobalMaterialLocalizations.delegate, | |
GlobalWidgetsLocalizations.delegate, | |
], | |
supportedLocales: [ | |
const Locale('en', ''), // English | |
const Locale('hi', ''), // Hindi | |
], | |
locale: locale, |
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
_fetchLocale() async { | |
var prefs = await SharedPreferences.getInstance(); | |
if (prefs.getString('languageCode') == null) { | |
return null; | |
} | |
print('_fetchLocale():' + | |
(prefs.getString('languageCode') +':' + prefs.getString('countryCode'))); | |
return Locale( | |
prefs.getString('languageCode'), prefs.getString('countryCode')); | |
} |
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
static void setLocale(BuildContext context, Locale newLocale) async { | |
_MyAppState state = context.ancestorStateOfType(TypeMatcher<_MyAppState>()); | |
state.setState(() { | |
state.locale = newLocale; | |
}); | |
} |
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
void _updateLocale(String lang, String country) async { | |
var prefs = await SharedPreferences.getInstance(); | |
prefs.setString('languageCode', lang); | |
prefs.setString('countryCode', country); | |
MyApp.setLocale(context, Locale(lang, country)); | |
} |
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
dependencies: | |
flutter: | |
sdk: flutter | |
flutter_localizations: | |
sdk: flutter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment