Skip to content

Commit ecf9b2d

Browse files
authored
Update localization of shortcut labels in menus (#116681)
* Fix Menu shortcut labels * Remove invalid localizations * Add more localization for Shift * Add generated localizations * Fix Test
1 parent 3d0607b commit ecf9b2d

File tree

116 files changed

+451
-499
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+451
-499
lines changed

packages/flutter/lib/src/material/material_localizations.dart

+6
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ abstract class MaterialLocalizations {
583583
/// The shortcut label for the keyboard key [LogicalKeyboardKey.select].
584584
String get keyboardKeySelect;
585585

586+
/// The shortcut label for the keyboard key [LogicalKeyboardKey.shift].
587+
String get keyboardKeyShift;
588+
586589
/// The shortcut label for the keyboard key [LogicalKeyboardKey.space].
587590
String get keyboardKeySpace;
588591

@@ -1321,6 +1324,9 @@ class DefaultMaterialLocalizations implements MaterialLocalizations {
13211324
@override
13221325
String get keyboardKeySelect => 'Select';
13231326

1327+
@override
1328+
String get keyboardKeyShift => 'Shift';
1329+
13241330
@override
13251331
String get keyboardKeySpace => 'Space';
13261332
}

packages/flutter/lib/src/material/menu_anchor.dart

+35-12
Original file line numberDiff line numberDiff line change
@@ -1974,9 +1974,6 @@ class _LocalizedShortcutLabeler {
19741974
LogicalKeyboardKey.arrowUp: '↑',
19751975
LogicalKeyboardKey.arrowDown: '↓',
19761976
LogicalKeyboardKey.enter: '↵',
1977-
LogicalKeyboardKey.shift: '⇧',
1978-
LogicalKeyboardKey.shiftLeft: '⇧',
1979-
LogicalKeyboardKey.shiftRight: '⇧',
19801977
};
19811978

19821979
static final Set<LogicalKeyboardKey> _modifiers = <LogicalKeyboardKey>{
@@ -2007,15 +2004,32 @@ class _LocalizedShortcutLabeler {
20072004
/// Returns the label to be shown to the user in the UI when a
20082005
/// [MenuSerializableShortcut] is used as a keyboard shortcut.
20092006
///
2010-
/// To keep the representation short, this will return graphical key
2011-
/// representations when it can. For instance, the default
2012-
/// [LogicalKeyboardKey.shift] will return '⇧', and the arrow keys will return
2013-
/// arrows. When [defaultTargetPlatform] is [TargetPlatform.macOS] or
2014-
/// [TargetPlatform.iOS], the key [LogicalKeyboardKey.meta] will show as '⌘',
2015-
/// [LogicalKeyboardKey.control] will show as '˄', and
2016-
/// [LogicalKeyboardKey.alt] will show as '⌥'.
2007+
/// When [defaultTargetPlatform] is [TargetPlatform.macOS] or
2008+
/// [TargetPlatform.iOS], this will return graphical key representations when
2009+
/// it can. For instance, the default [LogicalKeyboardKey.shift] will return
2010+
/// '⇧', and the arrow keys will return arrows. The key
2011+
/// [LogicalKeyboardKey.meta] will show as '⌘', [LogicalKeyboardKey.control]
2012+
/// will show as '˄', and [LogicalKeyboardKey.alt] will show as '⌥'.
2013+
///
2014+
/// The keys are joined by spaces on macOS and iOS, and by "+" on other
2015+
/// platforms.
20172016
String getShortcutLabel(MenuSerializableShortcut shortcut, MaterialLocalizations localizations) {
20182017
final ShortcutSerialization serialized = shortcut.serializeForMenu();
2018+
final String keySeparator;
2019+
switch (defaultTargetPlatform) {
2020+
case TargetPlatform.iOS:
2021+
case TargetPlatform.macOS:
2022+
// Use "⌃ ⇧ A" style on macOS and iOS.
2023+
keySeparator = ' ';
2024+
break;
2025+
case TargetPlatform.android:
2026+
case TargetPlatform.fuchsia:
2027+
case TargetPlatform.linux:
2028+
case TargetPlatform.windows:
2029+
// Use "Ctrl+Shift+A" style.
2030+
keySeparator = '+';
2031+
break;
2032+
}
20192033
if (serialized.trigger != null) {
20202034
final List<String> modifiers = <String>[];
20212035
final LogicalKeyboardKey trigger = serialized.trigger!;
@@ -2051,7 +2065,7 @@ class _LocalizedShortcutLabeler {
20512065
return <String>[
20522066
...modifiers,
20532067
if (shortcutTrigger != null && shortcutTrigger.isNotEmpty) shortcutTrigger,
2054-
].join(' ');
2068+
].join(keySeparator);
20552069
} else if (serialized.character != null) {
20562070
return serialized.character!;
20572071
}
@@ -2166,7 +2180,16 @@ class _LocalizedShortcutLabeler {
21662180
if (modifier == LogicalKeyboardKey.shift ||
21672181
modifier == LogicalKeyboardKey.shiftLeft ||
21682182
modifier == LogicalKeyboardKey.shiftRight) {
2169-
return _shortcutGraphicEquivalents[LogicalKeyboardKey.shift]!;
2183+
switch (defaultTargetPlatform) {
2184+
case TargetPlatform.android:
2185+
case TargetPlatform.fuchsia:
2186+
case TargetPlatform.linux:
2187+
case TargetPlatform.windows:
2188+
return localizations.keyboardKeyShift;
2189+
case TargetPlatform.iOS:
2190+
case TargetPlatform.macOS:
2191+
return '⇧';
2192+
}
21702193
}
21712194
throw ArgumentError('Keyboard key ${modifier.keyLabel} is not a modifier.');
21722195
}

packages/flutter/test/material/localizations_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ void main() {
120120
expect(localizations.keyboardKeyPrintScreen, isNotNull);
121121
expect(localizations.keyboardKeyScrollLock, isNotNull);
122122
expect(localizations.keyboardKeySelect, isNotNull);
123+
expect(localizations.keyboardKeyShift, isNotNull);
123124
expect(localizations.keyboardKeySpace, isNotNull);
124125
expect(localizations.currentDateLabel, isNotNull);
125126

packages/flutter/test/material/menu_anchor_test.dart

+16-14
Original file line numberDiff line numberDiff line change
@@ -1607,23 +1607,23 @@ void main() {
16071607
case TargetPlatform.fuchsia:
16081608
case TargetPlatform.linux:
16091609
mnemonic0 = tester.widget(findMnemonic(TestMenu.subSubMenu110.label));
1610-
expect(mnemonic0.data, equals('Ctrl A'));
1610+
expect(mnemonic0.data, equals('Ctrl+A'));
16111611
mnemonic1 = tester.widget(findMnemonic(TestMenu.subSubMenu111.label));
1612-
expect(mnemonic1.data, equals('B'));
1612+
expect(mnemonic1.data, equals('Shift+B'));
16131613
mnemonic2 = tester.widget(findMnemonic(TestMenu.subSubMenu112.label));
1614-
expect(mnemonic2.data, equals('Alt C'));
1614+
expect(mnemonic2.data, equals('Alt+C'));
16151615
mnemonic3 = tester.widget(findMnemonic(TestMenu.subSubMenu113.label));
1616-
expect(mnemonic3.data, equals('Meta D'));
1616+
expect(mnemonic3.data, equals('Meta+D'));
16171617
break;
16181618
case TargetPlatform.windows:
16191619
mnemonic0 = tester.widget(findMnemonic(TestMenu.subSubMenu110.label));
1620-
expect(mnemonic0.data, equals('Ctrl A'));
1620+
expect(mnemonic0.data, equals('Ctrl+A'));
16211621
mnemonic1 = tester.widget(findMnemonic(TestMenu.subSubMenu111.label));
1622-
expect(mnemonic1.data, equals('B'));
1622+
expect(mnemonic1.data, equals('Shift+B'));
16231623
mnemonic2 = tester.widget(findMnemonic(TestMenu.subSubMenu112.label));
1624-
expect(mnemonic2.data, equals('Alt C'));
1624+
expect(mnemonic2.data, equals('Alt+C'));
16251625
mnemonic3 = tester.widget(findMnemonic(TestMenu.subSubMenu113.label));
1626-
expect(mnemonic3.data, equals('Win D'));
1626+
expect(mnemonic3.data, equals('Win+D'));
16271627
break;
16281628
case TargetPlatform.iOS:
16291629
case TargetPlatform.macOS:
@@ -2146,24 +2146,26 @@ void main() {
21462146
String expectedMeta;
21472147
String expectedCtrl;
21482148
String expectedAlt;
2149+
String expectedSeparator;
2150+
String expectedShift;
21492151
switch (defaultTargetPlatform) {
21502152
case TargetPlatform.android:
21512153
case TargetPlatform.fuchsia:
21522154
case TargetPlatform.linux:
2153-
expectedCtrl = 'Ctrl';
2154-
expectedMeta = 'Meta';
2155-
expectedAlt = 'Alt';
2156-
break;
21572155
case TargetPlatform.windows:
21582156
expectedCtrl = 'Ctrl';
2159-
expectedMeta = 'Win';
2157+
expectedMeta = defaultTargetPlatform == TargetPlatform.windows ? 'Win' : 'Meta';
21602158
expectedAlt = 'Alt';
2159+
expectedShift = 'Shift';
2160+
expectedSeparator = '+';
21612161
break;
21622162
case TargetPlatform.iOS:
21632163
case TargetPlatform.macOS:
21642164
expectedCtrl = '⌃';
21652165
expectedMeta = '⌘';
21662166
expectedAlt = '⌥';
2167+
expectedShift = '⇧';
2168+
expectedSeparator = ' ';
21672169
break;
21682170
}
21692171

@@ -2174,7 +2176,7 @@ void main() {
21742176
shift: true,
21752177
alt: true,
21762178
);
2177-
final String allExpected = '$expectedAlt $expectedCtrl $expectedMeta ⇧ A';
2179+
final String allExpected = <String>[expectedAlt, expectedCtrl, expectedMeta, expectedShift, 'A'].join(expectedSeparator);
21782180
const CharacterActivator charShortcuts = CharacterActivator('ñ');
21792181
const String charExpected = 'ñ';
21802182
await tester.pumpWidget(

0 commit comments

Comments
 (0)