Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
13e1336e
Unverified
Commit
13e1336e
authored
4 years ago
by
Hans Muller
Committed by
GitHub
4 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generate message lookup in gen_l10n (#50733)
parent
9b3754d5
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
688 additions
and
770 deletions
+688
-770
gen_l10n.dart
dev/tools/localization/bin/gen_l10n.dart
+5
-26
gen_l10n.dart
dev/tools/localization/gen_l10n.dart
+244
-412
gen_l10n_templates.dart
dev/tools/localization/gen_l10n_templates.dart
+106
-82
gen_l10n_types.dart
dev/tools/localization/gen_l10n_types.dart
+275
-0
gen_l10n_test.dart
dev/tools/test/localization/gen_l10n_test.dart
+58
-250
No files found.
dev/tools/localization/bin/gen_l10n.dart
View file @
13e1336e
...
@@ -63,9 +63,8 @@ Future<void> main(List<String> arguments) async {
...
@@ -63,9 +63,8 @@ Future<void> main(List<String> arguments) async {
exit
(
0
);
exit
(
0
);
}
}
final
String
flutterRoot
=
Platform
.
environment
[
'FLUTTER_ROOT'
];
await
precacheLanguageAndRegionTags
();
final
String
flutterBin
=
Platform
.
isWindows
?
'flutter.bat'
:
'flutter'
;
final
String
flutterPath
=
flutterRoot
==
null
?
flutterBin
:
path
.
join
(
flutterRoot
,
'bin'
,
flutterBin
);
final
String
arbPathString
=
results
[
'arb-dir'
]
as
String
;
final
String
arbPathString
=
results
[
'arb-dir'
]
as
String
;
final
String
outputFileString
=
results
[
'output-localization-file'
]
as
String
;
final
String
outputFileString
=
results
[
'output-localization-file'
]
as
String
;
final
String
templateArbFileName
=
results
[
'template-arb-file'
]
as
String
;
final
String
templateArbFileName
=
results
[
'template-arb-file'
]
as
String
;
...
@@ -74,6 +73,7 @@ Future<void> main(List<String> arguments) async {
...
@@ -74,6 +73,7 @@ Future<void> main(List<String> arguments) async {
const
local
.
LocalFileSystem
fs
=
local
.
LocalFileSystem
();
const
local
.
LocalFileSystem
fs
=
local
.
LocalFileSystem
();
final
LocalizationsGenerator
localizationsGenerator
=
LocalizationsGenerator
(
fs
);
final
LocalizationsGenerator
localizationsGenerator
=
LocalizationsGenerator
(
fs
);
try
{
try
{
localizationsGenerator
localizationsGenerator
..
initialize
(
..
initialize
(
...
@@ -83,9 +83,8 @@ Future<void> main(List<String> arguments) async {
...
@@ -83,9 +83,8 @@ Future<void> main(List<String> arguments) async {
classNameString:
classNameString
,
classNameString:
classNameString
,
preferredSupportedLocaleString:
preferredSupportedLocaleString
,
preferredSupportedLocaleString:
preferredSupportedLocaleString
,
)
)
..
parseArbFiles
()
..
loadResources
()
..
generateClassMethods
()
..
writeOutputFile
();
..
generateOutputFile
();
}
on
FileSystemException
catch
(
e
)
{
}
on
FileSystemException
catch
(
e
)
{
exitWithError
(
e
.
message
);
exitWithError
(
e
.
message
);
}
on
FormatException
catch
(
e
)
{
}
on
FormatException
catch
(
e
)
{
...
@@ -93,24 +92,4 @@ Future<void> main(List<String> arguments) async {
...
@@ -93,24 +92,4 @@ Future<void> main(List<String> arguments) async {
}
on
L10nException
catch
(
e
)
{
}
on
L10nException
catch
(
e
)
{
exitWithError
(
e
.
message
);
exitWithError
(
e
.
message
);
}
}
final
ProcessResult
pubGetResult
=
await
Process
.
run
(
flutterPath
,
<
String
>[
'pub'
,
'get'
]);
if
(
pubGetResult
.
exitCode
!=
0
)
{
stderr
.
write
(
pubGetResult
.
stderr
);
exit
(
1
);
}
final
ProcessResult
generateFromArbResult
=
await
Process
.
run
(
flutterPath
,
<
String
>[
'pub'
,
'run'
,
'intl_translation:generate_from_arb'
,
'--output-dir=
${localizationsGenerator.l10nDirectory.path}
'
,
'--no-use-deferred-loading'
,
localizationsGenerator
.
outputFile
.
path
,
...
localizationsGenerator
.
arbPathStrings
,
]);
if
(
generateFromArbResult
.
exitCode
!=
0
)
{
stderr
.
write
(
generateFromArbResult
.
stderr
);
exit
(
1
);
}
}
}
This diff is collapsed.
Click to expand it.
dev/tools/localization/gen_l10n.dart
View file @
13e1336e
This diff is collapsed.
Click to expand it.
dev/tools/localization/gen_l10n_templates.dart
View file @
13e1336e
...
@@ -2,80 +2,29 @@
...
@@ -2,80 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// found in the LICENSE file.
const
String
getterMethodTemplate
=
'''
const
String
fileTemplate
=
'''
String get @(methodName) {
return Intl.message(
@(message),
locale: _localeName,
@(intlMethodArgs)
);
}
'''
;
const
String
simpleMethodTemplate
=
'''
String @(methodName)(@(methodParameters)) {
return Intl.message(
@(message),
locale: _localeName,
@(intlMethodArgs)
);
}
'''
;
const
String
formatMethodTemplate
=
'''
String @(methodName)(@(methodParameters)) {@(dateFormatting)@(numberFormatting)
String @(methodName)(@(innerMethodParameters)) {
return Intl.message(
@(message),
locale: _localeName,
@(intlMethodArgs)
);
}
return @(methodName)(@(innerMethodArgs));
}
'''
;
const
String
pluralMethodTemplate
=
'''
String @(methodName)(@(methodParameters)) {@(dateFormatting)@(numberFormatting)
return Intl.plural(
@(intlMethodArgs)
);
}
'''
;
const
String
pluralFormatMethodTemplate
=
'''
String @(methodName)(@(methodParameters)) {@(dateFormatting)@(numberFormatting)
String @(methodName)(@(innerMethodParameters)) {
return Intl.plural(
@(intlMethodArgs)
);
}
return @(methodName)(@(innerMethodArgs));
}
'''
;
const
String
defaultFileTemplate
=
'''
import '
dart:
async
';
import '
dart:
async
';
import '
package:
flutter
/
foundation
.
dart
';
import '
package:
flutter
/
widgets
.
dart
';
import '
package:
flutter
/
widgets
.
dart
';
import '
package:
flutter_localizations
/
flutter_localizations
.
dart
';
import '
package:
flutter_localizations
/
flutter_localizations
.
dart
';
import '
package:
intl
/
intl
.
dart
';
import '
package:
intl
/
intl
.
dart
'
as intl
;
import '
messages_all
.
dart
';
// ignore_for_file: unnecessary_brace_in_string_interps
/// Callers can lookup localized strings with an instance of @(class
Name
) returned
/// Callers can lookup localized strings with an instance of @(class) returned
/// by `@(class
Name
).of(context)`.
/// by `@(class).of(context)`.
///
///
/// Applications need to include `@(class
Name).delegate()` in their app
'
s
/// Applications need to include `@(class
).delegate()` in their app
\
'
s
/// localizationDelegates list, and the locales they support in the app's
/// localizationDelegates list, and the locales they support in the app
\
'
s
/// supportedLocales list. For example:
/// supportedLocales list. For example:
///
///
/// ```
/// ```
/// import '
@
(
importFile
)
';
/// import '
@
(
importFile
)
';
///
///
/// return MaterialApp(
/// return MaterialApp(
/// localizationsDelegates: @(class
Name
).localizationsDelegates,
/// localizationsDelegates: @(class).localizationsDelegates,
/// supportedLocales: @(class
Name
).supportedLocales,
/// supportedLocales: @(class).supportedLocales,
/// home: MyApplicationHome(),
/// home: MyApplicationHome(),
/// );
/// );
/// ```
/// ```
...
@@ -113,26 +62,18 @@ import 'messages_all.dart';
...
@@ -113,26 +62,18 @@ import 'messages_all.dart';
/// Select and expand the newly-created Localizations item then, for each
/// Select and expand the newly-created Localizations item then, for each
/// locale your application supports, add a new item and select the locale
/// locale your application supports, add a new item and select the locale
/// you wish to add from the pop-up menu in the Value field. This list should
/// you wish to add from the pop-up menu in the Value field. This list should
/// be consistent with the languages listed in the @(class
Name
).supportedLocales
/// be consistent with the languages listed in the @(class).supportedLocales
/// property.
/// property.
abstract class @(class) {
// ignore_for_file: unnecessary_brace_in_string_interps
@(class)(String locale) : assert(locale != null), _localeName = intl.Intl.canonicalizedLocale(locale.toString());
class
@(
className
)
{
@
(
className
)(
Locale
locale
)
:
_localeName
=
Intl
.
canonicalizedLocale
(
locale
.
toString
());
final String _localeName;
final String _localeName;
static
Future
<
@
(
className
)>
load
(
Locale
locale
)
{
static @(class) of(BuildContext context) {
return
initializeMessages
(
locale
.
toString
())
return Localizations.of<@(class)>(context, @(class));
.
then
<
@
(
className
)>((
_
)
=>
@
(
className
)(
locale
));
}
static
@
(
className
)
of
(
BuildContext
context
)
{
return
Localizations
.
of
<
@
(
className
)>(
context
,
@
(
className
));
}
}
static
const
LocalizationsDelegate
<
@
(
class
Name
)>
delegate
=
_
@
(
className
)
Delegate
();
static const LocalizationsDelegate<@(class
)> delegate = _@(class
)Delegate();
/// A list of this localizations delegate along with the default localizations
/// A list of this localizations delegate along with the default localizations
/// delegates.
/// delegates.
...
@@ -152,21 +93,104 @@ class @(className) {
...
@@ -152,21 +93,104 @@ class @(className) {
];
];
/// A list of this localizations delegate'
s
supported
locales
.
/// A list of this localizations delegate'
s
supported
locales
.
@
(
supportedLocales
)
static
const
List
<
Locale
>
supportedLocales
=
<
Locale
>[
@
(
supportedLocales
)
];
@
(
classMethods
)
@
(
methods
)}
}
class
_
@
(
class
Name
)
Delegate
extends
LocalizationsDelegate
<
@
(
className
)>
{
class
_
@
(
class
)
Delegate
extends
LocalizationsDelegate
<
@
(
class
)>
{
const
_
@
(
class
Name
)
Delegate
();
const
_
@
(
class
)
Delegate
();
@override
@override
Future
<
@
(
className
)>
load
(
Locale
locale
)
=>
@
(
className
).
load
(
locale
);
Future
<
@
(
class
)>
load
(
Locale
locale
)
{
return
SynchronousFuture
<
@
(
class
)>(@(
lookupName
)(
locale
));
}
@override
@override
bool
isSupported
(
Locale
locale
)
=>
<
String
>[
@
(
supportedLanguageCodes
)].
contains
(
locale
.
languageCode
);
bool
isSupported
(
Locale
locale
)
=>
<
String
>[
@
(
supportedLanguageCodes
)].
contains
(
locale
.
languageCode
);
@override
@override
bool
shouldReload
(
_
@
(
class
Name
)
Delegate
old
)
=>
false
;
bool
shouldReload
(
_
@
(
class
)
Delegate
old
)
=>
false
;
}
}
@
(
allMessagesClasses
)
@
(
class
)
@(
lookupName
)(
Locale
locale
)
{
switch
(
locale
.
languageCode
)
{
@
(
lookupBody
)
}
assert
(
false
,
'@(class).delegate failed to load unsupported locale "
\
$locale
"'
);
return
null
;
}
''';
const String numberFormatTemplate = '''
final
intl
.
NumberFormat
@
(
placeholder
)
NumberFormat
=
intl
.
NumberFormat
.
@
(
format
)(
locale:
_localeName
,
@
(
parameters
)
);
final
String
@
(
placeholder
)
String
=
@
(
placeholder
)
NumberFormat
.
format
(
@
(
placeholder
));
''';
const String dateFormatTemplate = '''
final
intl
.
DateFormat
@
(
placeholder
)
DateFormat
=
intl
.
DateFormat
.
@
(
format
)(
_localeName
);
final
String
@
(
placeholder
)
String
=
@
(
placeholder
)
DateFormat
.
format
(
@
(
placeholder
));
''';
const String getterTemplate = '''
@override
String
get
@
(
name
)
=>
@
(
message
);
''';
const String methodTemplate = '''
@override
String
@
(
name
)(
@
(
parameters
))
{
return
@
(
message
);
}
''';
const String formatMethodTemplate = '''
@override
String
@
(
name
)(
@
(
parameters
))
{
@
(
dateFormatting
)
@
(
numberFormatting
)
return
@
(
message
);
}
''';
const String pluralMethodTemplate = '''
@override
String
@
(
name
)(
@
(
parameters
))
{
@
(
dateFormatting
)
@
(
numberFormatting
)
return
intl
.
Intl
.
pluralLogic
(
@
(
count
),
locale:
_localeName
,
@
(
pluralLogicArgs
),
);
}
''';
const String classTemplate = '''
/// The translations for @(language) (`@(localeName)`).
class
@(
class
)
extends
@
(
baseClass
)
{
@
(
class
)([
String
locale
=
'@(localeName)'
])
:
super
(
locale
);
@
(
methods
)
}
''';
const String baseClassGetterTemplate = '''
// @(comment)
String
get
@
(
name
);
''';
''';
const String baseClassMethodTemplate = '''
// @(comment)
String
@
(
name
)(
@
(
parameters
));
''';
const String switchClauseTemplate = '''
case
'@(case)'
:
return
@
(
class
)();''';
const
String
countryCodeSwitchTemplate
=
'''case '
@
(
languageCode
)
': {
switch (locale.countryCode) {
@(switchClauses)
}
return @(class)();
}'''
;
This diff is collapsed.
Click to expand it.
dev/tools/localization/gen_l10n_types.dart
View file @
13e1336e
This diff is collapsed.
Click to expand it.
dev/tools/test/localization/gen_l10n_test.dart
View file @
13e1336e
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment