Skip to content

Instantly share code, notes, and snippets.

@sbeitzel
Last active December 15, 2019 01:10
Show Gist options
  • Select an option

  • Save sbeitzel/10bf09ff5dc8a52734c2879fdb1a0017 to your computer and use it in GitHub Desktop.

Select an option

Save sbeitzel/10bf09ff5dc8a52734c2879fdb1a0017 to your computer and use it in GitHub Desktop.
Localizations delegates in flutter_web
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class S {
const S();
static const SLocalizationsDelegate delegate =
const SLocalizationsDelegate();
static S of(BuildContext context) =>
Localizations.of<S>(context, S);
String get appTitle => "My Swell App";
}
class en extends S {
const en();
}
class SLocalizationsDelegate extends LocalizationsDelegate<S> {
const SLocalizationsDelegate();
List<Locale> get supportedLocales {
return const <Locale>[
const Locale("en", ""),
const Locale("en", "US"),
];
}
LocaleResolutionCallback resolution({Locale fallback}) {
return (Locale locale, Iterable<Locale> supported) {
final Locale languageLocale = new Locale(locale.languageCode, "");
if (supported.contains(locale))
return locale;
else if (supported.contains(languageLocale))
return languageLocale;
else {
final Locale fallbackLocale = fallback ?? supported.first;
return fallbackLocale;
}
};
}
@override
Future<S> load(Locale locale) {
final String lang = getLang(locale);
switch (lang) {
case "en":
case "en_US":
return new SynchronousFuture<S>(const en());
default:
return new SynchronousFuture<S>(const S());
}
}
@override
bool isSupported(Locale locale) => supportedLocales.contains(locale);
@override
bool shouldReload(SLocalizationsDelegate old) => false; // when we support a second locale, this should be changed
}
String getLang(Locale l) => l.countryCode != null && l.countryCode.isEmpty
? l.languageCode
: l.toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment