dev
dart 160 lines 4 KB
Raw
1 const textDirectionDeclaration = """
2
3 @override
4 TextDirection get textDirection => TextDirection.ltr;
5
6 """;
7
8 const classDeclaration = """
9 class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
10 const GeneratedLocalizationsDelegate();
11
12 List<Locale> get supportedLocales {
13 return const <Locale>[
14 """;
15
16 const part1 = """
17 import \'dart:async\';
18 import \'package:flutter/foundation.dart\';
19 import \'package:flutter/material.dart\';
20
21 class S implements WidgetsLocalizations {
22 const S();
23
24 static late S current;
25
26 static const GeneratedLocalizationsDelegate delegate =
27 GeneratedLocalizationsDelegate();
28
29 static S of(BuildContext context) => Localizations.of<S>(context, S)!;
30
31 @override
32 String get reorderItemToStart => "reorderItemToStart";
33
34 @override
35 String get reorderItemToEnd => "reorderItemToEnd";
36
37 @override
38 String get reorderItemUp => "reorderItemUp";
39
40 @override
41 String get reorderItemDown => "reorderItemDown";
42
43 @override
44 String get reorderItemLeft => "reorderItemLeft";
45
46 @override
47 String get reorderItemRight => "reorderItemRight";
48
49 @override
50 String get copyButtonLabel => "copyButtonLabel";
51
52 @override
53 String get cutButtonLabel => "cutButtonLabel";
54
55 @override
56 String get lookUpButtonLabel => "lookUpButtonLabel";
57
58 @override
59 String get pasteButtonLabel => "pasteButtonLabel";
60
61 @override
62 String get searchWebButtonLabel => "searchWebButtonLabel";
63
64 @override
65 String get selectAllButtonLabel => "selectAllButtonLabel";
66
67 @override
68 String get shareButtonLabel => "shareButtonLabel";
69
70 @override
71 String get noResultsFound => "noResultsFound";
72
73 @override
74 String get radioButtonUnselectedLabel => "radioButtonUnselectedLabel";
75
76 @override
77 String get searchResultsFound => "searchResultsFound";
78 """;
79
80 const part2 = """
81 ];
82 }
83
84 LocaleListResolutionCallback listResolution({required Locale fallback, bool withCountry = true}) {
85 return (List<Locale>? locales, Iterable<Locale> supported) {
86 if (locales == null || locales.isEmpty) {
87 return fallback ?? supported.first;
88 } else {
89 return _resolve(locales.first, fallback, supported, withCountry);
90 }
91 };
92 }
93
94 LocaleResolutionCallback resolution({required Locale fallback, bool withCountry = true}) {
95 return (Locale? locale, Iterable<Locale> supported) {
96 return _resolve(locale, fallback, supported, withCountry);
97 };
98 }
99
100 @override
101 Future<S> load(Locale locale) {
102 final String lang = getLang(locale);
103 if (lang != null) {
104 switch (lang) {
105 """;
106
107 const part3 = """
108 default:
109 }
110 }
111 S.current = const S();
112 return SynchronousFuture<S>(S.current);
113 }
114
115 @override
116 bool isSupported(Locale locale) => _isSupported(locale, true);
117
118 @override
119 bool shouldReload(GeneratedLocalizationsDelegate old) => false;
120
121 Locale _resolve(Locale? locale, Locale fallback, Iterable<Locale> supported, bool withCountry) {
122 if (locale == null || !_isSupported(locale, withCountry)) {
123 return fallback ?? supported.first;
124 }
125
126 final Locale languageLocale = Locale(locale.languageCode, "");
127 if (supported.contains(locale)) {
128 return locale;
129 } else if (supported.contains(languageLocale)) {
130 return languageLocale;
131 } else {
132 final Locale fallbackLocale = fallback ?? supported.first;
133 return fallbackLocale;
134 }
135 }
136
137 bool _isSupported(Locale locale, bool withCountry) {
138 if (locale != null) {
139 for (Locale supportedLocale in supportedLocales) {
140 if (supportedLocale.languageCode != locale.languageCode) {
141 continue;
142 }
143 if (supportedLocale.countryCode == locale.countryCode) {
144 return true;
145 }
146 if (true != withCountry && (supportedLocale.countryCode == null || supportedLocale.countryCode!.isEmpty)) {
147 return true;
148 }
149 }
150 }
151 return false;
152 }
153 }
154
155 String getLang(Locale l) => l == null
156 ? throw Exception('Incorrect local')
157 : l.countryCode != null && l.countryCode!.isEmpty
158 ? l.languageCode
159 : l.toString();
160 """;