Skip to content

Instantly share code, notes, and snippets.

@tianhaoz95
Created March 19, 2020 04:24
Show Gist options
  • Save tianhaoz95/425bd6f941d82f0fb85cf6245a14e0cd to your computer and use it in GitHub Desktop.
Save tianhaoz95/425bd6f941d82f0fb85cf6245a14e0cd to your computer and use it in GitHub Desktop.
example i18n interface
import 'package:flutter/widgets.dart';
import 'package:intl/intl.dart';
import 'package:photochatapp/services/i18n/wrappers/messages_all.dart';
class AppLocalizations {
AppLocalizations(this.localeName);
static Future<AppLocalizations> load(Locale locale) {
String name = locale.toString();
if (locale != null &&
locale.countryCode != null &&
locale.countryCode.isNotEmpty) {
name = locale.languageCode;
}
String localeName = Intl.canonicalizedLocale(name);
return initializeMessages(localeName).then((_) {
return AppLocalizations(localeName);
});
}
static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
final String localeName;
String get title {
return Intl.message(
'Mini Donkey',
name: 'title',
desc: 'Title for the application',
locale: localeName,
);
}
String get decodeResultScreenTitle {
return Intl.message(
'Here is Your Message!',
name: 'decodeResultScreenTitle',
desc: 'Text for the title of the decode result screen',
locale: localeName,
);
}
}
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
const AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) => ['en', 'zh'].contains(locale.languageCode);
@override
Future<AppLocalizations> load(Locale locale) => AppLocalizations.load(locale);
@override
bool shouldReload(AppLocalizationsDelegate old) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment