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
a2b93b86
Unverified
Commit
a2b93b86
authored
Oct 29, 2020
by
Shi-Hao Hong
Committed by
GitHub
Oct 29, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[gen_l10n] Add base method code comments for improved discoverability (#69016)
parent
fa3d2e21
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
148 additions
and
8 deletions
+148
-8
gen_l10n.dart
packages/flutter_tools/lib/src/localizations/gen_l10n.dart
+9
-3
gen_l10n_templates.dart
...utter_tools/lib/src/localizations/gen_l10n_templates.dart
+6
-2
generate_localizations_test.dart
...tools/test/general.shard/generate_localizations_test.dart
+133
-3
No files found.
packages/flutter_tools/lib/src/localizations/gen_l10n.dart
View file @
a2b93b86
...
...
@@ -205,16 +205,22 @@ String generateMethod(Message message, AppResourceBundle bundle) {
.
replaceAll
(
'@(message)'
,
generateMessage
());
}
String
generateBaseClassMethod
(
Message
message
)
{
final
String
comment
=
message
.
description
??
'No description provided in @
${message.resourceId}
'
;
String
generateBaseClassMethod
(
Message
message
,
LocaleInfo
templateArbLocale
)
{
final
String
comment
=
message
.
description
??
'No description provided for @
${message.resourceId}
.'
;
final
String
templateLocaleTranslationComment
=
'''
/// In
$templateArbLocale
, this message translates to:
/// **
${generateString(message.value)}
**'''
;
if
(
message
.
placeholders
.
isNotEmpty
)
{
return
baseClassMethodTemplate
.
replaceAll
(
'@(comment)'
,
comment
)
.
replaceAll
(
'@(templateLocaleTranslationComment)'
,
templateLocaleTranslationComment
)
.
replaceAll
(
'@(name)'
,
message
.
resourceId
)
.
replaceAll
(
'@(parameters)'
,
generateMethodParameters
(
message
).
join
(
', '
));
}
return
baseClassGetterTemplate
.
replaceAll
(
'@(comment)'
,
comment
)
.
replaceAll
(
'@(templateLocaleTranslationComment)'
,
templateLocaleTranslationComment
)
.
replaceAll
(
'@(name)'
,
message
.
resourceId
);
}
...
...
@@ -993,7 +999,7 @@ class LocalizationsGenerator {
_generatedLocalizationsFile
=
fileTemplate
.
replaceAll
(
'@(header)'
,
header
)
.
replaceAll
(
'@(class)'
,
className
)
.
replaceAll
(
'@(methods)'
,
_allMessages
.
map
(
generateBaseClassMethod
).
join
(
'
\n
'
))
.
replaceAll
(
'@(methods)'
,
_allMessages
.
map
(
(
Message
message
)
=>
generateBaseClassMethod
(
message
,
_templateArbLocale
)
).
join
(
'
\n
'
))
.
replaceAll
(
'@(importFile)'
,
'
$directory
/
$outputFileName
'
)
.
replaceAll
(
'@(supportedLocales)'
,
supportedLocalesCode
.
join
(
',
\n
'
))
.
replaceAll
(
'@(supportedLanguageCodes)'
,
supportedLanguageCodes
.
join
(
', '
))
...
...
packages/flutter_tools/lib/src/localizations/gen_l10n_templates.dart
View file @
a2b93b86
...
...
@@ -180,12 +180,16 @@ class @(class) extends @(baseLanguageClassName) {
''';
const String baseClassGetterTemplate = '''
// @(comment)
/// @(comment)
///
@
(
templateLocaleTranslationComment
)
String
get
@
(
name
);
''';
const String baseClassMethodTemplate = '''
// @(comment)
/// @(comment)
///
@
(
templateLocaleTranslationComment
)
String
@
(
name
)(
@
(
parameters
));
''';
...
...
packages/flutter_tools/test/general.shard/generate_localizations_test.dart
View file @
a2b93b86
...
...
@@ -29,18 +29,18 @@ const String singleMessageArbFileString = '''
{
"title": "Title",
"@title": {
"description": "Title for the application"
"description": "Title for the application
.
"
}
}'''
;
const
String
twoMessageArbFileString
=
'''
{
"title": "Title",
"@title": {
"description": "Title for the application"
"description": "Title for the application
.
"
},
"subtitle": "Subtitle",
"@subtitle": {
"description": "Subtitle for the application"
"description": "Subtitle for the application
.
"
}
}'''
;
const
String
esArbFileName
=
'app_es.arb'
;
...
...
@@ -1071,6 +1071,136 @@ void main() {
});
group
(
'writeOutputFiles'
,
()
{
test
(
'message without placeholders - should generate code comment with description and template message translation'
,
()
{
_standardFlutterDirectoryL10nSetup
(
fs
);
final
LocalizationsGenerator
generator
=
LocalizationsGenerator
(
fs
);
try
{
generator
.
initialize
(
inputPathString:
defaultL10nPathString
,
outputPathString:
defaultL10nPathString
,
templateArbFileName:
defaultTemplateArbFileName
,
outputFileString:
defaultOutputFileString
,
classNameString:
defaultClassNameString
,
);
generator
.
loadResources
();
generator
.
writeOutputFiles
();
}
on
Exception
catch
(
e
)
{
fail
(
'Generating output files should not fail:
$e
'
);
}
final
File
baseLocalizationsFile
=
fs
.
file
(
fs
.
path
.
join
(
syntheticL10nPackagePath
,
'output-localization-file.dart'
)
);
expect
(
baseLocalizationsFile
.
existsSync
(),
isTrue
);
final
String
baseLocalizationsFileContents
=
fs
.
file
(
fs
.
path
.
join
(
syntheticL10nPackagePath
,
'output-localization-file.dart'
)
).
readAsStringSync
();
expect
(
baseLocalizationsFileContents
,
contains
(
'/// Title for the application.'
));
expect
(
baseLocalizationsFileContents
,
contains
(
'''
/// In en, this message translates to:
/// **'
Title
'**'''
));
});
test
(
'template message translation handles newline characters'
,
()
{
final
Directory
l10nDirectory
=
fs
.
currentDirectory
.
childDirectory
(
'lib'
).
childDirectory
(
'l10n'
)
..
createSync
(
recursive:
true
);
l10nDirectory
.
childFile
(
defaultTemplateArbFileName
)
.
writeAsStringSync
(
r''
'
{
"title": "Title
\n
of the application",
"@title": {
"description": "Title for the application."
}
}'''
);
l10nDirectory
.
childFile
(
esArbFileName
)
.
writeAsStringSync
(
singleEsMessageArbFileString
);
final
LocalizationsGenerator
generator
=
LocalizationsGenerator
(
fs
);
try
{
generator
.
initialize
(
inputPathString:
defaultL10nPathString
,
outputPathString:
defaultL10nPathString
,
templateArbFileName:
defaultTemplateArbFileName
,
outputFileString:
defaultOutputFileString
,
classNameString:
defaultClassNameString
,
);
generator
.
loadResources
();
generator
.
writeOutputFiles
();
}
on
Exception
catch
(
e
)
{
fail
(
'Generating output files should not fail:
$e
'
);
}
final
File
baseLocalizationsFile
=
fs
.
file
(
fs
.
path
.
join
(
syntheticL10nPackagePath
,
'output-localization-file.dart'
)
);
expect
(
baseLocalizationsFile
.
existsSync
(),
isTrue
);
final
String
baseLocalizationsFileContents
=
fs
.
file
(
fs
.
path
.
join
(
syntheticL10nPackagePath
,
'output-localization-file.dart'
)
).
readAsStringSync
();
expect
(
baseLocalizationsFileContents
,
contains
(
'/// Title for the application.'
));
expect
(
baseLocalizationsFileContents
,
contains
(
r''
'
/// In en, this message translates to:
/// **'
Title
\
n
of
the
application
'**'''
));
});
test
(
'message with placeholders - should generate code comment with description and template message translation'
,
()
{
final
Directory
l10nDirectory
=
fs
.
currentDirectory
.
childDirectory
(
'lib'
).
childDirectory
(
'l10n'
)
..
createSync
(
recursive:
true
);
l10nDirectory
.
childFile
(
defaultTemplateArbFileName
)
.
writeAsStringSync
(
r''
'
{
"price": "The price of this item is:
${price}
",
"@price": {
"description": "The price of an online shopping cart item.",
"placeholders": {
"price": {
"type": "double",
"format": "decimalPattern"
}
}
}
}'''
);
l10nDirectory
.
childFile
(
esArbFileName
)
.
writeAsStringSync
(
r''
'
{
"price": "el precio de este artículo es:
${price}
"
}'''
);
final
LocalizationsGenerator
generator
=
LocalizationsGenerator
(
fs
);
try
{
generator
.
initialize
(
inputPathString:
defaultL10nPathString
,
outputPathString:
defaultL10nPathString
,
templateArbFileName:
defaultTemplateArbFileName
,
outputFileString:
defaultOutputFileString
,
classNameString:
defaultClassNameString
,
);
generator
.
loadResources
();
generator
.
writeOutputFiles
();
}
on
Exception
catch
(
e
)
{
final
L10nException
exception
=
e
as
L10nException
;
print
(
exception
.
message
);
fail
(
'Generating output files should not fail:
$e
'
);
}
final
File
baseLocalizationsFile
=
fs
.
file
(
fs
.
path
.
join
(
syntheticL10nPackagePath
,
'output-localization-file.dart'
)
);
expect
(
baseLocalizationsFile
.
existsSync
(),
isTrue
);
final
String
baseLocalizationsFileContents
=
fs
.
file
(
fs
.
path
.
join
(
syntheticL10nPackagePath
,
'output-localization-file.dart'
)
).
readAsStringSync
();
expect
(
baseLocalizationsFileContents
,
contains
(
'/// The price of an online shopping cart item.'
));
expect
(
baseLocalizationsFileContents
,
contains
(
r''
'
/// In en, this message translates to:
/// **'
The
price
of
this
item
is
:
\$
{
price
}
'**'''
));
});
test
(
'should generate a file per language'
,
()
{
const
String
singleEnCaMessageArbFileString
=
'''
{
...
...
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