[ermine][l10n] Use `fuchsia_internationalization`
Some reusable files from this example have been factored out to their
own library, `fuchsia_internationalization`. This change removes the
local copies and starts relying on the library instead.
Bug: 35579
Change-Id: Iefceb3a54b832ab2301f991dd1995ca417381fc1
diff --git a/session_shells/BUILD.gn b/session_shells/BUILD.gn
index 227e5c0..20f1d2d 100644
--- a/session_shells/BUILD.gn
+++ b/session_shells/BUILD.gn
@@ -14,7 +14,6 @@
deps = [
"ermine/settings:ermine_settings_unittests($host_toolchain)",
"ermine/shell:ermine_unittests($host_toolchain)",
- "ermine/internationalization:ermine_internationalization_unittests($host_toolchain)",
"//src/experiences/lib/quickui:quickui_unittests($host_toolchain)",
]
}
diff --git a/session_shells/ermine/internationalization/BUILD.gn b/session_shells/ermine/internationalization/BUILD.gn
index eb66721..7f3f54c 100644
--- a/session_shells/ermine/internationalization/BUILD.gn
+++ b/session_shells/ermine/internationalization/BUILD.gn
@@ -16,13 +16,11 @@
disable_analysis = true
sources = [
- "current_locale.dart",
"localization/messages_all.dart",
"localization/messages_messages.dart",
"localization/messages_nl.dart",
"localization/messages_sr.dart",
"localizations_delegate.dart",
- "profile_provider.dart",
"strings.dart",
"supported_locales.dart",
]
@@ -31,20 +29,7 @@
"//third_party/dart-pkg/git/flutter/packages/flutter",
"//third_party/dart-pkg/git/flutter/packages/flutter_localizations",
"//third_party/dart/third_party/pkg/intl",
+ "//topaz/public/dart/fuchsia_internationalization_flutter",
"//topaz/public/dart/widgets:lib.widgets",
]
}
-
-flutter_test("ermine_internationalization_unittests") {
- sources = [
- "current_locale_test.dart",
- "profile_provider_test.dart",
- ]
-
- deps = [
- ":internationalization",
- "//third_party/dart-pkg/git/flutter/packages/flutter_test",
- "//third_party/dart-pkg/pub/mockito",
- "//third_party/dart-pkg/pub/test",
- ]
-}
diff --git a/session_shells/ermine/internationalization/README.md b/session_shells/ermine/internationalization/README.md
index 49ae79a..9ae74a9 100644
--- a/session_shells/ermine/internationalization/README.md
+++ b/session_shells/ermine/internationalization/README.md
@@ -30,7 +30,7 @@
To import the new dependency, add the import:
```dart
-import 'packages:internationalization/strings.dart' as strings;
+import 'packages:internationalization/strings.dart';
```
to the import section of any Dart file that needs strings.
@@ -38,7 +38,7 @@
# Referring to localized strings
You can now refer to the messages defined in [strings.dart][lib/strings.dart].
-For example, you can refer to the string `Ask` by calling `strings.ask()`.
+For example, you can refer to the string `Ask` by calling `Strings.ask()`.
This is done so that the localization system can substitute the word `Ask` for
the appropriate word in the language defined by the current locale. The approach
extends in similar ways for most strings; with slight variations depending on
@@ -46,11 +46,8 @@
# Generating localizable libraries
-Two scripts are provided in the `./scripts` directory. One is
-`./scripts/run_extract_to_arb.sh`, which produces a "translation interchange
-form" in the form of [ARB][arb] files. The ARB files can be translated
-independently of the source code, and should be checked into the code base for
-now.
+To generate the ARB files, please refer to the instructions in
+`fuchsia_internationalization`.
## Translating
@@ -63,9 +60,10 @@
## Generating Dart runtime format for the translations
-Once translated you can use the file `./scripts/run_generate_from_arb.sh` to
-generate the Dart code that wires up the translation. You will need to rebuild
-the entire project to take the new translations in.
+Once translated you can use the file `./scripts/run_generate_from_arb.sh` from
+`fuchsia_internationalization` library to generate the Dart code that wires up
+the translation. You will need to rebuild the entire project to take the new
+translations in.
# Further reading
diff --git a/session_shells/ermine/internationalization/lib/current_locale.dart b/session_shells/ermine/internationalization/lib/current_locale.dart
deleted file mode 100644
index d5a2688..0000000
--- a/session_shells/ermine/internationalization/lib/current_locale.dart
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright 2019 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-import 'dart:ui' show Locale;
-import 'package:flutter/material.dart';
-import 'package:intl/intl.dart';
-
-/// Holds the current [Locale] and notifies any listeners of value changes.
-class CurrentLocale extends ValueNotifier<Locale> {
- CurrentLocale(Locale value) : super(value);
-
- /// Returns the Unicode locale of the current locale as a string. E.g.
- /// "en_US".
- String unicode() =>
- // Locale.toString() is for debugging purposes only, but it's the
- // correct form.
- Intl.canonicalizedLocale(super.value.toString());
-}
diff --git a/session_shells/ermine/internationalization/lib/profile_provider.dart b/session_shells/ermine/internationalization/lib/profile_provider.dart
deleted file mode 100644
index d5e3f8d..0000000
--- a/session_shells/ermine/internationalization/lib/profile_provider.dart
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright 2018 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-import 'package:flutter/material.dart';
-
-import 'package:fidl/fidl.dart';
-import 'package:fidl_fuchsia_intl/fidl_async.dart';
-import 'package:fuchsia_logger/logger.dart';
-import 'package:intl/intl.dart';
-
-// The locale to use if service-based locale resolution fails.
-const _defaultLocale =
- Locale.fromSubtags(languageCode: 'en', countryCode: 'US');
-
-/// Extracts the user's preferred locale from the profile.
-Locale _fromProfile(Profile profile) {
- final String localeName =
- profile?.locales?.first?.id ?? _defaultLocale.toString();
- return Locale(Intl.canonicalizedLocale(localeName));
-}
-
-/// Encapsulates the logic to obtain the locales from the service
-/// fuchsia.intl.PropertyProvider.
-///
-/// For the time being, the first locale only is returned.
-class LocaleSource {
- final PropertyProvider _stub;
-
- const LocaleSource(this._stub);
-
- Future<Locale> initial() async {
- try {
- return _fromProfile(await _stub.getProfile());
- } on FidlError catch (e, s) {
- log.warning(
- 'Could not get locale from fuchsia.intl.ProfileProvider for the shell. '
- 'This is nonfatal, but the shell will not support any locale '
- 'except for the system default: $e: $s');
- return _defaultLocale;
- }
- }
-
- /// Returns the stream of locale changes, after the call to [initial()].
- Stream<Locale> stream() {
- return _stub.onChange.asyncMap((x) => _stub.getProfile()).map(_fromProfile);
- }
-}
diff --git a/session_shells/ermine/internationalization/scripts/run_extract_to_arb.sh b/session_shells/ermine/internationalization/scripts/run_extract_to_arb.sh
deleted file mode 100755
index d847f84..0000000
--- a/session_shells/ermine/internationalization/scripts/run_extract_to_arb.sh
+++ /dev/null
@@ -1,16 +0,0 @@
-#!/bin/bash
-# Copyright 2019 The Fuchsia Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-set -x
-
-$FUCHSIA_DIR/third_party/dart-pkg/git/flutter/bin/flutter \
- packages pub get intl_translation:extract_to_arb
-
-$FUCHSIA_DIR/third_party/dart-pkg/git/flutter/bin/flutter \
- packages pub run intl_translation:extract_to_arb \
- --output-dir=resources lib/*strings.dart
-
-rm .packages pubspec.lock
-rm -fr .dart_tool
diff --git a/session_shells/ermine/internationalization/scripts/run_generate_from_arb.sh b/session_shells/ermine/internationalization/scripts/run_generate_from_arb.sh
deleted file mode 100755
index 93f7b81..0000000
--- a/session_shells/ermine/internationalization/scripts/run_generate_from_arb.sh
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/bash
-# Copyright 2019 The Fuchsia Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-set -x
-
-$FUCHSIA_DIR/third_party/dart-pkg/git/flutter/bin/flutter \
- packages pub get intl_translation:generate_from_arb
-
-$FUCHSIA_DIR/third_party/dart-pkg/git/flutter/bin/flutter \
- packages pub run intl_translation:generate_from_arb \
- --output-dir=lib/localization \
- lib/*strings.dart \
- resources/*.arb
-
-rm .packages pubspec.lock
-rm -fr .dart_tool
diff --git a/session_shells/ermine/internationalization/test/current_locale_test.dart b/session_shells/ermine/internationalization/test/current_locale_test.dart
deleted file mode 100644
index 69c96b7..0000000
--- a/session_shells/ermine/internationalization/test/current_locale_test.dart
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright 2019 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-import 'package:flutter/material.dart';
-import 'package:flutter_test/flutter_test.dart';
-
-// ignore_for_file: implementation_imports
-import 'package:internationalization/current_locale.dart';
-
-void main() {
- testWidgets('Check en_US from subtags', (tester) async {
- final CurrentLocale current = CurrentLocale(
- Locale.fromSubtags(languageCode: 'en', countryCode: 'US'));
- expect(current.unicode(), 'en_US');
- expect(current.value.toString(), 'en_US');
- });
- testWidgets('Check sr_RS from subtags', (tester) async {
- final CurrentLocale current = CurrentLocale(
- Locale.fromSubtags(languageCode: 'sr', countryCode: 'RS'));
- expect(current.unicode(), 'sr_RS');
- expect(current.value.toString(), 'sr_RS');
- });
- testWidgets('Check sr from constructor', (tester) async {
- final CurrentLocale current = CurrentLocale(Locale('sr'));
- expect(current.unicode(), 'sr');
- expect(current.value.toString(), 'sr');
- });
- testWidgets('Check sr_RS from constructor', (tester) async {
- final CurrentLocale current = CurrentLocale(Locale('sr_RS'));
- expect(current.unicode(), 'sr_RS');
- expect(current.value.toString(), 'sr_RS');
- });
-}
diff --git a/session_shells/ermine/internationalization/test/profile_provider_test.dart b/session_shells/ermine/internationalization/test/profile_provider_test.dart
deleted file mode 100644
index 60872de..0000000
--- a/session_shells/ermine/internationalization/test/profile_provider_test.dart
+++ /dev/null
@@ -1,81 +0,0 @@
-// Copyright 2019 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-import 'package:test/test.dart';
-
-// ignore_for_file: implementation_imports
-import 'package:fidl/fidl.dart' as fidl;
-import 'package:fidl_fuchsia_intl/fidl_async.dart' as fidl_intl;
-import 'package:fuchsia_logger/logger.dart';
-import 'package:internationalization/profile_provider.dart';
-import 'package:mockito/mockito.dart';
-
-void main() {
- setupLogger();
-
- MockPropertyProvider mockPropertyProvider;
- LocaleSource source;
-
- setUp(() async {
- mockPropertyProvider = MockPropertyProvider();
- source = LocaleSource(mockPropertyProvider);
- });
-
- const _someProfile = fidl_intl.Profile(
- locales: [
- fidl_intl.LocaleId(id: 'sr_RS'),
- fidl_intl.LocaleId(id: 'en_US'),
- ],
- );
- const _otherProfile = fidl_intl.Profile(
- locales: [
- fidl_intl.LocaleId(id: 'ru_RU'),
- fidl_intl.LocaleId(id: 'sr_RS'),
- fidl_intl.LocaleId(id: 'en_US'),
- ],
- );
- const _thirdProfile = fidl_intl.Profile(
- locales: [
- fidl_intl.LocaleId(id: 'nl_NL'),
- fidl_intl.LocaleId(id: 'ru_RU'),
- fidl_intl.LocaleId(id: 'sr_RS'),
- fidl_intl.LocaleId(id: 'en_US'),
- ],
- );
-
- test('Test initial locale', () async {
- when(mockPropertyProvider.getProfile())
- .thenAnswer((_) => Future.value(_someProfile));
-
- expect((await source.initial()).toString(), equals('sr_RS'));
- });
-
- test('Test initial locale gets an exception and returns default',
- () async {
- when(mockPropertyProvider.getProfile())
- .thenThrow(fidl.FidlError('I hate you!'));
- expect((await source.initial()).toString(), equals('en_US'));
- });
-
- test('Test obtaining updates on change', () async {
- // This is how you get different return values for repeated calls of the
- // same method. It is important that 'answers is captured outside of the
- // answer closure. See
- // https://stackoverflow.com/questions/53896225/chain-multiple-calls-with-same-arguments-to-return-different-results
- var answers = [
- Future.value(_someProfile),
- Future.value(_otherProfile),
- Future.value(_thirdProfile),
- ];
- when(mockPropertyProvider.getProfile())
- .thenAnswer((_) => answers.removeAt(0));
- when(mockPropertyProvider.onChange)
- .thenAnswer((_) => Stream.fromIterable(['1', '2']));
-
- expect((await source.initial()).toString(), equals('sr_RS'));
- expect(await source.stream().join(','), equals('ru_RU,nl_NL'));
- });
-}
-
-class MockPropertyProvider extends Mock implements fidl_intl.PropertyProvider {}
diff --git a/session_shells/ermine/shell/lib/main.dart b/session_shells/ermine/shell/lib/main.dart
index d393b79..3e32321 100644
--- a/session_shells/ermine/shell/lib/main.dart
+++ b/session_shells/ermine/shell/lib/main.dart
@@ -6,11 +6,11 @@
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fidl_fuchsia_intl/fidl_async.dart';
+import 'package:fuchsia_internationalization_flutter/internationalization.dart';
import 'package:fuchsia_logger/logger.dart';
import 'package:fuchsia_services/services.dart';
import 'package:internationalization/localizations_delegate.dart'
as localizations;
-import 'package:internationalization/profile_provider.dart';
import 'package:internationalization/supported_locales.dart'
as supported_locales;
import 'package:intl/intl.dart';
@@ -27,7 +27,7 @@
final locales = LocaleSource(_intl);
final model = AppModel();
- runApp(_Localized(model, await locales.initial(), locales.stream()));
+ runApp(_Localized(model, locales.stream()));
await model.onStarted();
_intl.ctrl.close();
}
@@ -41,18 +41,15 @@
// The stream of locale updates.
final Stream<Locale> _localeStream;
- // The initial value of the locale
- final Locale _initial;
-
- const _Localized(this._model, this._initial, this._localeStream);
+ const _Localized(this._model, this._localeStream);
@override
Widget build(BuildContext context) {
return StreamBuilder<Locale>(
stream: _localeStream,
- initialData: _initial,
builder: (BuildContext context, AsyncSnapshot<Locale> snapshot) {
- final Locale locale = snapshot.data ?? _initial;
+ // There will always be a locale here.
+ final Locale locale = snapshot.data;
// This is required so app parts which don't depend on the flutter
// locale have access to it.
Intl.defaultLocale = locale.toString();