Commit 4faa0367 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Added localizations for Directionality (#11870)

parent e216004b
......@@ -28,9 +28,7 @@ class _WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocaliz
const _WidgetsLocalizationsDelegate();
@override
Future<WidgetsLocalizations> load(Locale locale) {
return new SynchronousFuture<WidgetsLocalizations>(const WidgetsLocalizations());
}
Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);
@override
bool shouldReload(_WidgetsLocalizationsDelegate old) => false;
......
......@@ -114,20 +114,13 @@ abstract class LocalizationsDelegate<T> {
/// In particular, this maps locales to a specific [Directionality] using the
/// [textDirection] property.
///
/// This class provides a default placeholder implementation that returns
/// hard-coded American English values.
class WidgetsLocalizations {
/// Create a placeholder object for the localized resources of the lowest
/// levels of the Flutter framework which only provides values for American
/// English.
const WidgetsLocalizations();
/// The locale for which the values of this class's localized resources
/// have been translated.
Locale get locale => const Locale('en', 'US');
/// See also:
///
/// * [DefaultWidgetsLocalizations], which implements this interface and
/// supports a variety of locales.
abstract class WidgetsLocalizations {
/// The reading direction for text in this locale.
TextDirection get textDirection => TextDirection.ltr;
TextDirection get textDirection;
/// The `WidgetsLocalizations` from the closest [Localizations] instance
/// that encloses the given context.
......@@ -146,6 +139,42 @@ class WidgetsLocalizations {
}
}
/// Localized values for widgets.
class DefaultWidgetsLocalizations implements WidgetsLocalizations {
DefaultWidgetsLocalizations(this.locale) {
final String language = locale.languageCode.toLowerCase();
_textDirection = _rtlLanguages.contains(language) ? TextDirection.rtl : TextDirection.ltr;
}
// See http://en.wikipedia.org/wiki/Right-to-left
static const List<String> _rtlLanguages = const <String>[
'ar', // Arabic
'dv', // Dhivehi
'fa', // Farsi
'he', // Hebrew
'ps', // Pashto
'sd', // Sindhi
'ur', // Urdu
];
/// The locale for which the values of this class's localized resources
/// have been translated.
final Locale locale;
@override
TextDirection get textDirection => _textDirection;
TextDirection _textDirection;
/// Creates an object that provides localized resource values for the
/// lowest levels of the Flutter framework.
///
/// This method is typically used to create a [LocalizationsDelegate].
/// The [WidgetsApp] does so by default.
static Future<WidgetsLocalizations> load(Locale locale) {
return new SynchronousFuture<WidgetsLocalizations>(new DefaultWidgetsLocalizations(locale));
}
}
class _LocalizationsScope extends InheritedWidget {
_LocalizationsScope ({
Key key,
......
......@@ -417,6 +417,26 @@ void main() {
expect(modifiedDelegate.shouldReloadValues, <bool>[true]);
});
testWidgets('Directionality tracks system locale', (WidgetTester tester) async {
BuildContext pageContext;
await tester.pumpWidget(
buildFrame(
buildContent: (BuildContext context) {
pageContext = context;
return const Text('Hello World');
}
)
);
await tester.binding.setLocale('en', 'GB');
await tester.pump();
expect(Directionality.of(pageContext), TextDirection.ltr);
await tester.binding.setLocale('ar', 'EG');
await tester.pump();
expect(Directionality.of(pageContext), TextDirection.rtl);
});
}
// Same as _WidgetsLocalizationsDelegate in widgets/app.dart
......@@ -424,9 +444,7 @@ class DefaultWidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsL
const DefaultWidgetsLocalizationsDelegate();
@override
Future<WidgetsLocalizations> load(Locale locale) {
return new SynchronousFuture<WidgetsLocalizations>(const WidgetsLocalizations());
}
Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);
@override
bool shouldReload(DefaultWidgetsLocalizationsDelegate old) => false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment