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
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.
11 changes: 11 additions & 0 deletions pkgs/sketch_pad/lib/l10n/de.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"@@locale": "de",
"@@x-template": "en.arb",
"newPad": "Neu",
"samples": "Beispiele",
"installSdk": "SDK installieren",
"privacyNotice": "Datenschutzerklärung",
"feedback": "Feedback",
"channelSelection": "{channelName} channel",
"switchedLocaleTo": "Zu {locale} gewechselt"
}
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","0O1C0hUg",0,null,[" channel",[0,0]],"Feedback","SDK installieren","Neu","Datenschutzerklärung","Beispiele",["Zu gewechselt",[3,0]]]
44 changes: 44 additions & 0 deletions pkgs/sketch_pad/lib/l10n/en.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"@@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"
}
}
},
"switchedLocaleTo": "Switched locale to {locale}",
"@switchedLocaleTo": {
"description": "Message toast to display after switching to a new locale",
"placeholders": {
"locale": {
"type": "String",
"description": "The displayname of the new locale."
}
}
}
}
48 changes: 48 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,48 @@
// 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> {
mosuem marked this conversation as resolved.
Show resolved Hide resolved
@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;
}

Messages messages = Messages(rootBundle.loadString, const OldIntlObject());
mosuem marked this conversation as resolved.
Show resolved Hide resolved
69 changes: 69 additions & 0 deletions pkgs/sketch_pad/lib/l10n/en.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Generated by package:messages_builder.

import 'package:messages/messages_json.dart';

class Messages {
Messages(
this._fileLoader,
this.intlObject,
);

final Future<String> Function(String id) _fileLoader;

String _currentLocale = 'en';

final Map<String, MessageList> _messages = {};

static const carbs = {
mosuem marked this conversation as resolved.
Show resolved Hide resolved
'en': ('lib/l10n/en.json', '+6SmU+Pe'),
'de': ('lib/l10n/de.json', '0O1C0hUg')
};

IntlObject intlObject;

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;

static Iterable<String> get knownLocales => carbs.keys;

Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final info = carbs[locale];
final carb = info?.$1;
if (carb == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = await _fileLoader(carb);
final messageList = MessageListJson.fromString(data, intlObject);
if (messageList.preamble.hash != info?.$2) {
throw ArgumentError('''
Messages file for locale $locale has different hash "${messageList.preamble.hash}" than generated code "${info?.$2}".''');
}
_messages[locale] = messageList;
}
_currentLocale = locale;
}

void loadAllLocales() {
for (final locale in knownLocales) {
loadLocale(locale);
}
}

String channelSelection(String channelName) =>
_currentMessages.generateStringAtIndex(0, [channelName]);

String get feedback => _currentMessages.generateStringAtIndex(1, []);

String get installSdk => _currentMessages.generateStringAtIndex(2, []);

String get newPad => _currentMessages.generateStringAtIndex(3, []);

String get privacyNotice => _currentMessages.generateStringAtIndex(4, []);

String get samples => _currentMessages.generateStringAtIndex(5, []);

String switchedLocaleTo(String locale) =>
_currentMessages.generateStringAtIndex(6, [locale]);
}
1 change: 1 addition & 0 deletions pkgs/sketch_pad/lib/l10n/en.json
mosuem marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0,"en","+6SmU+Pe",0,null,[" channel",[0,0]],"Feedback","Install SDK","New","Privacy Notice","Samples",["Switched locale to ",[19,0]]]