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
5848a162
Unverified
Commit
5848a162
authored
Aug 12, 2021
by
Ahmed Ashour
Committed by
GitHub
Aug 12, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[gen_l10n] to handle arbitrary DateFormat patterns (#86844)
parent
e1485e95
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
130 additions
and
22 deletions
+130
-22
gen_l10n.dart
packages/flutter_tools/lib/src/localizations/gen_l10n.dart
+12
-4
gen_l10n_templates.dart
...utter_tools/lib/src/localizations/gen_l10n_templates.dart
+5
-0
gen_l10n_types.dart
...s/flutter_tools/lib/src/localizations/gen_l10n_types.dart
+22
-1
generate_localizations_test.dart
...tools/test/general.shard/generate_localizations_test.dart
+91
-17
No files found.
packages/flutter_tools/lib/src/localizations/gen_l10n.dart
View file @
5848a162
...
...
@@ -111,17 +111,25 @@ String generateDateFormattingLogic(Message message) {
'date formats.'
);
}
if
(!
placeholder
.
hasValidDateFormat
)
{
final
bool
?
isCustomDateFormat
=
placeholder
.
isCustomDateFormat
;
if
(!
placeholder
.
hasValidDateFormat
&&
(
isCustomDateFormat
==
null
||
!
isCustomDateFormat
))
{
throw
L10nException
(
'Date format "
$placeholderFormat
" for placeholder '
'
${placeholder.name}
does not have a corresponding DateFormat '
"constructor
\n
. Check the intl library's DateFormat class "
'constructors for allowed date formats.'
'constructors for allowed date formats, or set "isCustomDateFormat" attribute '
'to "true".'
);
}
return
dateFormatTemplate
if
(
placeholder
.
hasValidDateFormat
)
{
return
dateFormatTemplate
.
replaceAll
(
'@(placeholder)'
,
placeholder
.
name
)
.
replaceAll
(
'@(format)'
,
placeholderFormat
);
}
return
dateFormatCustomTemplate
.
replaceAll
(
'@(placeholder)'
,
placeholder
.
name
)
.
replaceAll
(
'@(format)'
,
placeholderFormat
);
.
replaceAll
(
'@(format)'
,
generateString
(
placeholderFormat
)
);
});
return
formatStatements
.
isEmpty
?
'@(none)'
:
formatStatements
.
join
(
''
);
...
...
packages/flutter_tools/lib/src/localizations/gen_l10n_templates.dart
View file @
5848a162
...
...
@@ -126,6 +126,11 @@ const String dateFormatTemplate = '''
final
String
@
(
placeholder
)
String
=
@
(
placeholder
)
DateFormat
.
format
(
@
(
placeholder
));
''';
const String dateFormatCustomTemplate = '''
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
);
''';
...
...
packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart
View file @
5848a162
...
...
@@ -193,7 +193,8 @@ class Placeholder {
example
=
_stringAttribute
(
resourceId
,
name
,
attributes
,
'example'
),
type
=
_stringAttribute
(
resourceId
,
name
,
attributes
,
'type'
)
??
'Object'
,
format
=
_stringAttribute
(
resourceId
,
name
,
attributes
,
'format'
),
optionalParameters
=
_optionalParameters
(
resourceId
,
name
,
attributes
);
optionalParameters
=
_optionalParameters
(
resourceId
,
name
,
attributes
),
isCustomDateFormat
=
_boolAttribute
(
resourceId
,
name
,
attributes
,
'isCustomDateFormat'
);
final
String
resourceId
;
final
String
name
;
...
...
@@ -201,6 +202,7 @@ class Placeholder {
final
String
?
type
;
final
String
?
format
;
final
List
<
OptionalParameter
>
optionalParameters
;
final
bool
?
isCustomDateFormat
;
bool
get
requiresFormatting
=>
<
String
>[
'DateTime'
,
'double'
,
'num'
].
contains
(
type
)
||
(
type
==
'int'
&&
format
!=
null
);
bool
get
isNumber
=>
<
String
>[
'double'
,
'int'
,
'num'
].
contains
(
type
);
...
...
@@ -228,6 +230,25 @@ class Placeholder {
return
value
;
}
static
bool
?
_boolAttribute
(
String
resourceId
,
String
name
,
Map
<
String
,
Object
?>
attributes
,
String
attributeName
,
)
{
final
Object
?
value
=
attributes
[
attributeName
];
if
(
value
==
null
)
{
return
null
;
}
if
(
value
!=
'true'
&&
value
!=
'false'
)
{
throw
L10nException
(
'The "
$attributeName
" value of the "
$name
" placeholder in message
$resourceId
'
'must be a boolean value.'
,
);
}
return
value
==
'true'
;
}
static
List
<
OptionalParameter
>
_optionalParameters
(
String
resourceId
,
String
name
,
...
...
packages/flutter_tools/test/general.shard/generate_localizations_test.dart
View file @
5848a162
...
...
@@ -1356,13 +1356,13 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
"@@locale": "en",
"springBegins": "Spring begins on {springStartDate}",
"@springBegins": {
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "yMd"
}
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "yMd"
}
}
}
}'''
;
fs
.
currentDirectory
.
childDirectory
(
'lib'
).
childDirectory
(
'l10n'
)..
createSync
(
recursive:
true
)
...
...
@@ -1391,13 +1391,13 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
"@@locale": "en",
"springBegins": "Spring begins on {springStartDate}",
"@springBegins": {
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "asdf"
}
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "asdf"
}
}
}
}'''
;
final
Directory
l10nDirectory
=
fs
.
currentDirectory
.
childDirectory
(
'lib'
).
childDirectory
(
'l10n'
)
...
...
@@ -1429,17 +1429,91 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
);
});
testWithoutContext
(
'use standard date format whenever possible'
,
()
{
const
String
singleDateMessageArbFileString
=
'''
{
"@@locale": "en",
"springBegins": "Spring begins on {springStartDate}",
"@springBegins": {
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "yMd",
"isCustomDateFormat": "true"
}
}
}
}'''
;
final
Directory
l10nDirectory
=
fs
.
currentDirectory
.
childDirectory
(
'lib'
).
childDirectory
(
'l10n'
)
..
createSync
(
recursive:
true
);
l10nDirectory
.
childFile
(
defaultTemplateArbFileName
)
.
writeAsStringSync
(
singleDateMessageArbFileString
);
LocalizationsGenerator
(
fileSystem:
fs
,
inputPathString:
defaultL10nPathString
,
templateArbFileName:
defaultTemplateArbFileName
,
outputFileString:
defaultOutputFileString
,
classNameString:
defaultClassNameString
,
)
..
loadResources
()
..
writeOutputFiles
(
BufferLogger
.
test
());
final
String
localizationsFile
=
fs
.
file
(
fs
.
path
.
join
(
syntheticL10nPackagePath
,
'output-localization-file_en.dart'
),
).
readAsStringSync
();
expect
(
localizationsFile
,
contains
(
'DateFormat.yMd(localeName)'
));
});
testWithoutContext
(
'handle arbitrary formatted date'
,
()
{
const
String
singleDateMessageArbFileString
=
'''
{
"@@locale": "en",
"springBegins": "Spring begins on {springStartDate}",
"@springBegins": {
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "asdf o'
clock
",
"
isCustomDateFormat
": "
true
"
}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(singleDateMessageArbFileString);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
)
..loadResources()
..writeOutputFiles(BufferLogger.test());
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'),
).readAsStringSync();
expect(localizationsFile, contains(r"
DateFormat
(
'asdf o
\'
clock'
,
localeName
)
"));
});
testWithoutContext('throws an exception when no format attribute is passed in', () {
const String singleDateMessageArbFileString = '''
{
"
springBegins
": "
Spring
begins
on
{
springStartDate
}
",
"
@springBegins
": {
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime"
}
"
description
": "
The
first
day
of
spring
",
"
placeholders
": {
"
springStartDate
": {
"
type
": "
DateTime
"
}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
...
...
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