Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localization #2748

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/sketch_pad.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
- run: flutter analyze
- run: dart format --set-exit-if-changed .
- run: flutter build web
- run: flutter test
- run: flutter test --dart-define=SKIP_CHECK_BUILD=false
10 changes: 10 additions & 0 deletions pkgs/sketch_pad/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ flutter run -d chrome --web-browser-flag "--disable-web-security"
We pass the `--disable-web-security` flag to Chrome as we're not able to
configure the `flutter run` web server to pass CORS headers for
`AssetManifest.json`, `FontManifest.json`, and other resources.

## Localization

To add a new locale, say `ar`:
* Copy `de.arb` and rename to `ar.arb`
* Set the `"locale"` key in the file to `ar`.
* Add the translated strings to the file. Use the descriptions in `en.arb` as a reference.
* Add the locale to the `supportedLocales` in `main.dart`.
* Add `lib/ar.json` to the assets in the `pubspec.yaml`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be

Suggested change
* Add `lib/ar.json` to the assets in the `pubspec.yaml`
* Add `lib/l10n/ar.json` to the assets in the `pubspec.yaml`

* Run `dart run build_runner build -d` to regenerate the calling code.
3 changes: 2 additions & 1 deletion pkgs/sketch_pad/lib/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:flutter/material.dart';

import 'l10n/en.flutter.g.dart';
import 'theme.dart';
import 'widgets.dart';

Expand Down Expand Up @@ -77,7 +78,7 @@ class _ConsoleWidgetState extends State<ConsoleWidget> {
builder: (context, value, _) {
return MiniIconButton(
icon: Icons.playlist_remove,
tooltip: 'Clear console',
tooltip: context.messagesLocalizations!.clearConsole,
onPressed: value.text.isEmpty ? null : _clearConsole,
);
},
Expand Down
15 changes: 8 additions & 7 deletions pkgs/sketch_pad/lib/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'l10n/en.g.dart';
import 'theme.dart';

bool get _mac => defaultTargetPlatform == TargetPlatform.macOS;
Expand Down Expand Up @@ -40,13 +41,13 @@ final ShortcutActivator quickFixKeyActivator = SingleActivator(

// map of key activator names

final List<(String, ShortcutActivator)> keyBindings = [
('Code completion', codeCompletionKeyActivator),
('Find', findKeyActivator),
('Find next', findNextKeyActivator),
('Quick fixes', quickFixKeyActivator),
('Reload', reloadKeyActivator),
];
List<(String, ShortcutActivator)> keyBindings(Messages messages) => [
(messages.codeCompletion, codeCompletionKeyActivator),
(messages.find, findKeyActivator),
(messages.findNext, findNextKeyActivator),
(messages.quickFixes, quickFixKeyActivator),
(messages.reload, reloadKeyActivator),
];

extension SingleActivatorExtension on SingleActivator {
// Note that this only works in debug mode.
Expand Down
36 changes: 36 additions & 0 deletions pkgs/sketch_pad/lib/l10n/de.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"@@locale": "de",
"@@x-template": "en.arb",
"newPad": "Neu",
"samples": "Beispiele",
"installSdk": "SDK installieren",
"privacyNotice": "Datenschutzerklärung",
"feedback": "Feedback",
"channelSelection": "{channelName} channel",
"toggleBrightness": "Helligkeit umschalten",
"runButton": "Ausführen",
"clearConsole": "Konsole löschen",
"format": "Formatieren",
"noFormattingChanges": "Keine Änderungen beim Formatieren",
"formatSuccesful": "Formatieren erfolgreich",
"errorFormatting": "Fehler beim formatieren",
"compilationFailed": "Kompilierung fehlgeschlagen",
"keyboardShortcuts": "Tastenkombinationen",
"newSnippet": "Neues {type} snippet",
"switchedToDartAndFlutter": "Zu Dart {dartVersion} and Flutter {flutterVersion} gewechselt",
"sharingGuide": "Leitfaden zum Teilen",
"dartpadGithub": "DartPad auf GitHub",
"command": "Befehl",
"keyboardShortcut": "Tastenkombination",
"runtimeVersions": "Runtime-Versionen",
"basedOnDartAndFlutter": "Based on Dart SDK {dartVersion} and Flutter SDK {flutterVersion}",
"experimentsEnabled": "Experiments enabled: {experiments}",
"package": "Paket",
"version": "Version",
"createNewSnippet": "Neues {type} Snippet erstellt",
"codeCompletion": "Code Vervollständigung",
"find": "Finden",
"findNext": "Nächstes finden",
"quickFixes": "Quick-Fixes",
"reload": "Neu laden"
}
1 change: 1 addition & 0 deletions pkgs/sketch_pad/lib/l10n/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"de","fhb5q58f",0,null,["Based on Dart SDK and Flutter SDK ",[18,0],[35,1]],[" channel",[0,0]],"Konsole löschen","Code Vervollständigung","Befehl","Kompilierung fehlgeschlagen",["Neues Snippet erstellt",[6,0]],"DartPad auf GitHub","Fehler beim formatieren",["Experiments enabled: ",[21,0]],"Feedback","Finden","Nächstes finden","Formatieren","Formatieren erfolgreich","SDK installieren","Tastenkombination","Tastenkombinationen","Neu",["Neues snippet",[6,0]],"Keine Änderungen beim Formatieren","Paket","Datenschutzerklärung","Quick-Fixes","Neu laden","Ausführen","Runtime-Versionen","Beispiele","Leitfaden zum Teilen",["Zu Dart and Flutter gewechselt",[8,0],[21,1]],"Helligkeit umschalten","Version"]
169 changes: 169 additions & 0 deletions pkgs/sketch_pad/lib/l10n/en.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"@@locale": "en",
"newPad": "New",
"@newPad": {
"description": "Button to create a new pad"
},
"samples": "Samples",
"@samples": {
"description": "Button to show a list of all samples"
},
"installSdk": "Install SDK",
"@installSdk": {
"description": "Button to follow external link to webpage helping on installing the Dart SDK"
},
"privacyNotice": "Privacy Notice",
"@privacyNotice": {
"description": "Button to show privacy notice on external web page"
},
"feedback": "Feedback",
"@feedback": {
"description": "Button to give feedback on external web page"
},
"channelSelection": "{channelName} channel",
"@channelSelection": {
"description": "Text field describing the Dart SDK channel selected by the user",
"placeholders": {
"channelName": {
"type": "String",
"description": "The name of the channel",
"example": "Stable"
}
}
},
"toggleBrightness": "Toggle brightness",
"@toggleBrightness": {
"description": "Toggle brightness button tooltip"
},
"runButton": "Run",
"@runButton": {
"description": "Run button"
},
"clearConsole": "Clear console",
"@clearConsole": {
"description": "Clear Console button tooltip"
},
"format": "Format",
"@format": {
"description": "Format tooltip"
},
"noFormattingChanges": "No formatting changes",
"@noFormattingChanges": {
"description": "No formatting changes toast"
},
"formatSuccesful": "Format successful",
"@formatSuccesful": {
"description": "Format succesful toast"
},
"errorFormatting": "Error formatting code",
"@errorFormatting": {
"description": "Error formatting code toast"
},
"compilationFailed": "Compilation failed",
"@compilationFailed": {
"description": "Compilation failed"
},
"keyboardShortcuts": "Keyboard shortcuts",
"@keyboardShortcuts": {
"description": "Keyboard shortcuts tooltip"
},
"newSnippet": "New {type} snippet",
"@newSnippet": {
"description": "New snippet for type",
"placeholders": {
"type": {
"type": "String"
}
}
},
"switchedToDartAndFlutter": "Switched to Dart {dartVersion} and Flutter {flutterVersion}",
"@switchedToDartAndFlutter": {
"description": "switchedToDartAndFlutter",
"placeholders": {
"dartVersion": {
"type": "String"
},
"flutterVersion": {
"type": "String"
}
}
},
"sharingGuide": "Sharing Guide",
"@sharingGuide": {
"description": "Sharing Guide button"
},
"dartpadGithub": "DartPad on GitHub",
"@dartpadGithub": {
"description": "Dartpad on Github button"
},
"command": "Command",
"@command": {
"description": "command table header"
},
"keyboardShortcut": "Keyboard shortcut",
"@keyboardShortcut": {
"description": "Keyboard shortcut table header"
},
"runtimeVersions": "Runtime versions",
"@runtimeVersions": {
"description": "Runtime versions"
},
"basedOnDartAndFlutter": "Based on Dart SDK {dartVersion} and Flutter SDK {flutterVersion}",
"@basedOnDartAndFlutter": {
"description": "Based on Dart and Flutter title",
"placeholders": {
"dartVersion": {
"type": "String"
},
"flutterVersion": {
"type": "String"
}
}
},
"experimentsEnabled": "Experiments enabled: {experiments}",
"@experimentsEnabled": {
"description": "Experiments enabled",
"placeholders": {
"experiments": {
"type": "String"
}
}
},
"package": "Package",
"@package": {
"description": "Package table header"
},
"version": "Version",
"@version": {
"description": "Version table header"
},
"createNewSnippet": "Created new {type} snippet",
"@createNewSnippet": {
"description": "Create new snippet toast",
"placeholders": {
"type": {
"type": "String"
}
}
},
"codeCompletion": "Code completion",
"@codeCompletion": {
"description": "Code completion keyboard shortcut"
},
"find": "Find",
"@find": {
"description": "Find keyboard shortcut"
},
"findNext": "Find next",
"@findNext": {
"description": "Find next keyboard shortcut"
},
"quickFixes": "Quick fixes",
"@quickFixes": {
"description": "Quick fixes keyboard shortcut"
},
"reload": "Reload",
"@reload": {
"description": "Reload keyboard shortcut"
}
}
52 changes: 52 additions & 0 deletions pkgs/sketch_pad/lib/l10n/en.flutter.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Generated by package:messages_builder.

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:messages/package_intl_object.dart';

import 'en.g.dart';

class MessagesLocalizations {
static Iterable<LocalizationsDelegate<dynamic>> localizationsDelegates = [
delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
];

static LocalizationsDelegate<Messages> delegate =
_MessagesLocalizationsDelegate();

static List<Locale> get supportedLocales {
return Messages.knownLocales.map((e) {
final split = e.split('_');
final code = split.length > 1 ? split[1] : null;
return Locale(split.first, code);
}).toList();
}

static Messages? of(BuildContext context) =>
Localizations.of<Messages>(context, Messages);
}

class _MessagesLocalizationsDelegate extends LocalizationsDelegate<Messages> {
@override
bool isSupported(Locale locale) =>
Messages.knownLocales.contains(locale.toString());

@override
Future<Messages> load(Locale locale) async {
await messages.loadLocale(locale.toString());
return messages;
}

@override
bool shouldReload(LocalizationsDelegate<Messages> old) => false;
}

extension MessagesLocalizationsExtension on BuildContext {
Messages? get messagesLocalizations => MessagesLocalizations.of(this);
}

Messages messages = Messages(rootBundle.loadString, const OldIntlObject());
mosuem marked this conversation as resolved.
Show resolved Hide resolved