Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Update localization of shortcut labels in menus (#116681)
Browse files Browse the repository at this point in the history
* Fix Menu shortcut labels

* Remove invalid localizations

* Add more localization for Shift

* Add generated localizations

* Fix Test
  • Loading branch information
gspencergoog committed Dec 16, 2022
1 parent 3d0607b commit ecf9b2d
Show file tree
Hide file tree
Showing 116 changed files with 451 additions and 499 deletions.
6 changes: 6 additions & 0 deletions packages/flutter/lib/src/material/material_localizations.dart
Expand Up @@ -583,6 +583,9 @@ abstract class MaterialLocalizations {
/// The shortcut label for the keyboard key [LogicalKeyboardKey.select].
String get keyboardKeySelect;

/// The shortcut label for the keyboard key [LogicalKeyboardKey.shift].
String get keyboardKeyShift;

/// The shortcut label for the keyboard key [LogicalKeyboardKey.space].
String get keyboardKeySpace;

Expand Down Expand Up @@ -1321,6 +1324,9 @@ class DefaultMaterialLocalizations implements MaterialLocalizations {
@override
String get keyboardKeySelect => 'Select';

@override
String get keyboardKeyShift => 'Shift';

@override
String get keyboardKeySpace => 'Space';
}
47 changes: 35 additions & 12 deletions packages/flutter/lib/src/material/menu_anchor.dart
Expand Up @@ -1974,9 +1974,6 @@ class _LocalizedShortcutLabeler {
LogicalKeyboardKey.arrowUp: '↑',
LogicalKeyboardKey.arrowDown: '↓',
LogicalKeyboardKey.enter: '↵',
LogicalKeyboardKey.shift: '⇧',
LogicalKeyboardKey.shiftLeft: '⇧',
LogicalKeyboardKey.shiftRight: '⇧',
};

static final Set<LogicalKeyboardKey> _modifiers = <LogicalKeyboardKey>{
Expand Down Expand Up @@ -2007,15 +2004,32 @@ class _LocalizedShortcutLabeler {
/// Returns the label to be shown to the user in the UI when a
/// [MenuSerializableShortcut] is used as a keyboard shortcut.
///
/// To keep the representation short, this will return graphical key
/// representations when it can. For instance, the default
/// [LogicalKeyboardKey.shift] will return '⇧', and the arrow keys will return
/// arrows. When [defaultTargetPlatform] is [TargetPlatform.macOS] or
/// [TargetPlatform.iOS], the key [LogicalKeyboardKey.meta] will show as '⌘',
/// [LogicalKeyboardKey.control] will show as '˄', and
/// [LogicalKeyboardKey.alt] will show as '⌥'.
/// When [defaultTargetPlatform] is [TargetPlatform.macOS] or
/// [TargetPlatform.iOS], this will return graphical key representations when
/// it can. For instance, the default [LogicalKeyboardKey.shift] will return
/// '⇧', and the arrow keys will return arrows. The key
/// [LogicalKeyboardKey.meta] will show as '⌘', [LogicalKeyboardKey.control]
/// will show as '˄', and [LogicalKeyboardKey.alt] will show as '⌥'.
///
/// The keys are joined by spaces on macOS and iOS, and by "+" on other
/// platforms.
String getShortcutLabel(MenuSerializableShortcut shortcut, MaterialLocalizations localizations) {
final ShortcutSerialization serialized = shortcut.serializeForMenu();
final String keySeparator;
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
// Use "⌃ ⇧ A" style on macOS and iOS.
keySeparator = ' ';
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
// Use "Ctrl+Shift+A" style.
keySeparator = '+';
break;
}
if (serialized.trigger != null) {
final List<String> modifiers = <String>[];
final LogicalKeyboardKey trigger = serialized.trigger!;
Expand Down Expand Up @@ -2051,7 +2065,7 @@ class _LocalizedShortcutLabeler {
return <String>[
...modifiers,
if (shortcutTrigger != null && shortcutTrigger.isNotEmpty) shortcutTrigger,
].join(' ');
].join(keySeparator);
} else if (serialized.character != null) {
return serialized.character!;
}
Expand Down Expand Up @@ -2166,7 +2180,16 @@ class _LocalizedShortcutLabeler {
if (modifier == LogicalKeyboardKey.shift ||
modifier == LogicalKeyboardKey.shiftLeft ||
modifier == LogicalKeyboardKey.shiftRight) {
return _shortcutGraphicEquivalents[LogicalKeyboardKey.shift]!;
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return localizations.keyboardKeyShift;
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return '⇧';
}
}
throw ArgumentError('Keyboard key ${modifier.keyLabel} is not a modifier.');
}
Expand Down
1 change: 1 addition & 0 deletions packages/flutter/test/material/localizations_test.dart
Expand Up @@ -120,6 +120,7 @@ void main() {
expect(localizations.keyboardKeyPrintScreen, isNotNull);
expect(localizations.keyboardKeyScrollLock, isNotNull);
expect(localizations.keyboardKeySelect, isNotNull);
expect(localizations.keyboardKeyShift, isNotNull);
expect(localizations.keyboardKeySpace, isNotNull);
expect(localizations.currentDateLabel, isNotNull);

Expand Down
30 changes: 16 additions & 14 deletions packages/flutter/test/material/menu_anchor_test.dart
Expand Up @@ -1607,23 +1607,23 @@ void main() {
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
mnemonic0 = tester.widget(findMnemonic(TestMenu.subSubMenu110.label));
expect(mnemonic0.data, equals('Ctrl A'));
expect(mnemonic0.data, equals('Ctrl+A'));
mnemonic1 = tester.widget(findMnemonic(TestMenu.subSubMenu111.label));
expect(mnemonic1.data, equals('B'));
expect(mnemonic1.data, equals('Shift+B'));
mnemonic2 = tester.widget(findMnemonic(TestMenu.subSubMenu112.label));
expect(mnemonic2.data, equals('Alt C'));
expect(mnemonic2.data, equals('Alt+C'));
mnemonic3 = tester.widget(findMnemonic(TestMenu.subSubMenu113.label));
expect(mnemonic3.data, equals('Meta D'));
expect(mnemonic3.data, equals('Meta+D'));
break;
case TargetPlatform.windows:
mnemonic0 = tester.widget(findMnemonic(TestMenu.subSubMenu110.label));
expect(mnemonic0.data, equals('Ctrl A'));
expect(mnemonic0.data, equals('Ctrl+A'));
mnemonic1 = tester.widget(findMnemonic(TestMenu.subSubMenu111.label));
expect(mnemonic1.data, equals('B'));
expect(mnemonic1.data, equals('Shift+B'));
mnemonic2 = tester.widget(findMnemonic(TestMenu.subSubMenu112.label));
expect(mnemonic2.data, equals('Alt C'));
expect(mnemonic2.data, equals('Alt+C'));
mnemonic3 = tester.widget(findMnemonic(TestMenu.subSubMenu113.label));
expect(mnemonic3.data, equals('Win D'));
expect(mnemonic3.data, equals('Win+D'));
break;
case TargetPlatform.iOS:
case TargetPlatform.macOS:
Expand Down Expand Up @@ -2146,24 +2146,26 @@ void main() {
String expectedMeta;
String expectedCtrl;
String expectedAlt;
String expectedSeparator;
String expectedShift;
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
expectedCtrl = 'Ctrl';
expectedMeta = 'Meta';
expectedAlt = 'Alt';
break;
case TargetPlatform.windows:
expectedCtrl = 'Ctrl';
expectedMeta = 'Win';
expectedMeta = defaultTargetPlatform == TargetPlatform.windows ? 'Win' : 'Meta';
expectedAlt = 'Alt';
expectedShift = 'Shift';
expectedSeparator = '+';
break;
case TargetPlatform.iOS:
case TargetPlatform.macOS:
expectedCtrl = '⌃';
expectedMeta = '⌘';
expectedAlt = '⌥';
expectedShift = '⇧';
expectedSeparator = ' ';
break;
}

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

0 comments on commit ecf9b2d

Please sign in to comment.