1// Copyright 2017 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import 'package:flutter/foundation.dart'; 8import 'package:flutter/widgets.dart'; 9 10/// Localized values for widgets. 11/// 12/// Currently this class just maps [locale] to [textDirection]. All locales 13/// are [TextDirection.ltr] except for locales with the following 14/// [Locale.languageCode] values, which are [TextDirection.rtl]: 15/// 16/// * ar - Arabic 17/// * fa - Farsi 18/// * he - Hebrew 19/// * ps - Pashto 20/// * sd - Sindhi 21/// * ur - Urdu 22class GlobalWidgetsLocalizations implements WidgetsLocalizations { 23 /// Construct an object that defines the localized values for the widgets 24 /// library for the given `locale`. 25 /// 26 /// [LocalizationsDelegate] implementations typically call the static [load] 27 /// function, rather than constructing this class directly. 28 GlobalWidgetsLocalizations(this.locale) { 29 final String language = locale.languageCode.toLowerCase(); 30 _textDirection = _rtlLanguages.contains(language) ? TextDirection.rtl : TextDirection.ltr; 31 } 32 33 // See http://en.wikipedia.org/wiki/Right-to-left 34 static const List<String> _rtlLanguages = <String>[ 35 'ar', // Arabic 36 'fa', // Farsi 37 'he', // Hebrew 38 'ps', // Pashto 39 'ur', // Urdu 40 ]; 41 42 /// The locale for which the values of this class's localized resources 43 /// have been translated. 44 final Locale locale; 45 46 @override 47 TextDirection get textDirection => _textDirection; 48 TextDirection _textDirection; 49 50 /// Creates an object that provides localized resource values for the 51 /// lowest levels of the Flutter framework. 52 /// 53 /// This method is typically used to create a [LocalizationsDelegate]. 54 /// The [WidgetsApp] does so by default. 55 static Future<WidgetsLocalizations> load(Locale locale) { 56 return SynchronousFuture<WidgetsLocalizations>(GlobalWidgetsLocalizations(locale)); 57 } 58 59 /// A [LocalizationsDelegate] that uses [GlobalWidgetsLocalizations.load] 60 /// to create an instance of this class. 61 /// 62 /// [WidgetsApp] automatically adds this value to [WidgetApp.localizationsDelegates]. 63 static const LocalizationsDelegate<WidgetsLocalizations> delegate = _WidgetsLocalizationsDelegate(); 64} 65 66class _WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> { 67 const _WidgetsLocalizationsDelegate(); 68 69 @override 70 bool isSupported(Locale locale) => true; 71 72 @override 73 Future<WidgetsLocalizations> load(Locale locale) => GlobalWidgetsLocalizations.load(locale); 74 75 @override 76 bool shouldReload(_WidgetsLocalizationsDelegate old) => false; 77 78 @override 79 String toString() => 'GlobalWidgetsLocalizations.delegate(all locales)'; 80} 81