-
Notifications
You must be signed in to change notification settings - Fork 122
fix(ui_localizations): loading Traditional Chinese #128
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09fdd30
Uses the way how _WidgetsLocalizationsDelegate choose an appropriate …
LaemonT 8281e01
melos run format
LaemonT 2512870
Add trailing comma
LaemonT d20a73d
Add test cases to verify that Traditional Chinese loads correctly and…
LaemonT 7895c68
Add test cases to verify that Traditional Chinese loads correctly and…
LaemonT dc512ac
Add widget tests for the `firebase_ui_localizations` package.
LaemonT 2de574a
Code refactoring and applies `melos run format`.
LaemonT 0da35ef
Convert to block body.
LaemonT 485a34d
Execute `melos run format`
LaemonT 04eba93
formatting and naming
lesnitsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 107 additions & 38 deletions
145
packages/firebase_ui_localizations/lib/src/all_languages.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,121 @@ | ||
// Copyright 2023, the Chromium project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
import "./default_localizations.dart"; | ||
import 'dart:ui'; | ||
|
||
import "./default_localizations.dart"; | ||
import 'lang/ar.dart'; | ||
import 'lang/de.dart'; | ||
import 'lang/en.dart'; | ||
import 'lang/es.dart'; | ||
import 'lang/es_419.dart'; | ||
import 'lang/ko.dart'; | ||
import 'lang/fr.dart'; | ||
import 'lang/he.dart'; | ||
import 'lang/hi.dart'; | ||
import 'lang/hu.dart'; | ||
import 'lang/id.dart'; | ||
import 'lang/pt.dart'; | ||
import 'lang/he.dart'; | ||
import 'lang/de.dart'; | ||
import 'lang/it.dart'; | ||
import 'lang/zh.dart'; | ||
import 'lang/zh_tw.dart'; | ||
import 'lang/uk.dart'; | ||
import 'lang/th.dart'; | ||
import 'lang/ar.dart'; | ||
import 'lang/tr.dart'; | ||
import 'lang/ja.dart'; | ||
import 'lang/ko.dart'; | ||
import 'lang/nl.dart'; | ||
import 'lang/pl.dart'; | ||
import 'lang/en.dart'; | ||
import 'lang/ja.dart'; | ||
import 'lang/hi.dart'; | ||
import 'lang/pt.dart'; | ||
import 'lang/ru.dart'; | ||
import 'lang/fr.dart'; | ||
import 'lang/th.dart'; | ||
import 'lang/tr.dart'; | ||
import 'lang/uk.dart'; | ||
import 'lang/zh.dart'; | ||
import 'lang/zh_tw.dart'; | ||
|
||
final localizations = <String, FirebaseUILocalizationLabels>{ | ||
'es': const EsLocalizations(), | ||
'es_419': const Es419Localizations(), | ||
'ko': const KoLocalizations(), | ||
'hu': const HuLocalizations(), | ||
'id': const IdLocalizations(), | ||
'pt': const PtLocalizations(), | ||
'he': const HeLocalizations(), | ||
'de': const DeLocalizations(), | ||
'it': const ItLocalizations(), | ||
'zh': const ZhLocalizations(), | ||
'zh_tw': const ZhTWLocalizations(), | ||
'uk': const UkLocalizations(), | ||
'th': const ThLocalizations(), | ||
'ar': const ArLocalizations(), | ||
'tr': const TrLocalizations(), | ||
'nl': const NlLocalizations(), | ||
'pl': const PlLocalizations(), | ||
'en': const EnLocalizations(), | ||
'ja': const JaLocalizations(), | ||
'hi': const HiLocalizations(), | ||
'ru': const RuLocalizations(), | ||
'fr': const FrLocalizations(), | ||
final Set<String> kSupportedLanguages = { | ||
'ar', // Arabic | ||
'de', // German | ||
'en', // English | ||
'es', // Spanish Castilian | ||
'fr', // French | ||
'he', // Hebrew | ||
'hi', // Hindi | ||
'hu', // Hungarian | ||
'id', // Indonesian | ||
'it', // Italian | ||
'ja', // Japanese | ||
'ko', // Korean | ||
'nl', // Dutch Flemish | ||
'pl', // Polish | ||
'pt', // Portuguese | ||
'ru', // Russian | ||
'th', // Thai | ||
'tr', // Turkish | ||
'uk', // Ukrainian | ||
'zh', // Chinese | ||
}; | ||
|
||
FirebaseUILocalizationLabels getFirebaseUITranslation( | ||
Locale useLocale, [ | ||
Locale? defaultLocale, | ||
]) { | ||
final Locale locale; | ||
if (kSupportedLanguages.contains(useLocale.languageCode)) { | ||
locale = useLocale; | ||
} else { | ||
locale = defaultLocale ?? useLocale; | ||
} | ||
|
||
switch (locale.languageCode) { | ||
case 'ar': | ||
return const ArLocalizations(); | ||
case 'de': | ||
return const DeLocalizations(); | ||
case 'en': | ||
return const EnLocalizations(); | ||
case 'es': | ||
switch (locale.countryCode) { | ||
case '419': | ||
return const Es419Localizations(); | ||
} | ||
return const EsLocalizations(); | ||
case 'fr': | ||
return const FrLocalizations(); | ||
case 'he': | ||
return const HeLocalizations(); | ||
case 'hi': | ||
return const HiLocalizations(); | ||
case 'hu': | ||
return const HuLocalizations(); | ||
case 'id': | ||
return const IdLocalizations(); | ||
case 'it': | ||
return const ItLocalizations(); | ||
case 'ja': | ||
return const JaLocalizations(); | ||
case 'ko': | ||
return const KoLocalizations(); | ||
case 'nl': | ||
return const NlLocalizations(); | ||
case 'pl': | ||
return const PlLocalizations(); | ||
case 'pt': | ||
return const PtLocalizations(); | ||
case 'ru': | ||
return const RuLocalizations(); | ||
case 'th': | ||
return const ThLocalizations(); | ||
case 'tr': | ||
return const TrLocalizations(); | ||
case 'uk': | ||
return const UkLocalizations(); | ||
case 'zh': | ||
switch (locale.scriptCode) { | ||
case 'Hant': | ||
return const ZhTWLocalizations(); | ||
} | ||
switch (locale.countryCode) { | ||
case 'HK': | ||
case 'TW': | ||
return const ZhTWLocalizations(); | ||
} | ||
return const ZhLocalizations(); | ||
} | ||
|
||
throw ('getTranslationLabels() called for unsupported locale "$locale"'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
packages/firebase_ui_localizations/test/firebase_ui_localizations_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file | ||
// for details. 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:firebase_ui_localizations/firebase_ui_localizations.dart'; | ||
import 'package:firebase_ui_localizations/src/lang/zh_tw.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_localizations/flutter_localizations.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
const localeZh = Locale('zh'); | ||
const localeTW = Locale('zh', 'TW'); | ||
|
||
Future<void> main() async { | ||
late FirebaseUILocalizationDelegate delegate; | ||
|
||
group( | ||
'FirebaseUILocalization loads the appropriate Chinese translation', | ||
() { | ||
localizedText(BuildContext context) => FirebaseUILocalizations.labelsOf(context).signInWithPhoneButtonText; | ||
|
||
setUp(() async { | ||
delegate = const FirebaseUILocalizationDelegate(); | ||
}); | ||
|
||
test( | ||
'Loads the correct translation with the language tag "${localeZh.toLanguageTag()}"', | ||
() async { | ||
final localizations = await delegate.load(localeZh); | ||
expect(localizations.labels.signInWithPhoneButtonText, '使用电话号码登录'); | ||
}, | ||
); | ||
|
||
testWidgets( | ||
'UI test for the "${localeZh.toLanguageTag()}" translation', | ||
(tester) async { | ||
await tester.pumpWidget( | ||
TestMaterialApp( | ||
locale: localeZh, | ||
localizedText: localizedText, | ||
), | ||
); | ||
expect(find.text('使用电话号码登录'), findsOneWidget); | ||
}, | ||
); | ||
|
||
test( | ||
'Loads the correct translation with the language tag "${localeTW.toLanguageTag()}"', | ||
() async { | ||
final localizations = await delegate.load(localeTW); | ||
expect(localizations.labels.signInWithPhoneButtonText, '使用電話號碼登入'); | ||
}, | ||
); | ||
|
||
testWidgets( | ||
'UI test for the "${localeTW.toLanguageTag()}" translation', | ||
(tester) async { | ||
await tester.pumpWidget( | ||
TestMaterialApp( | ||
locale: localeTW, | ||
localizedText: localizedText, | ||
), | ||
); | ||
expect(find.text('使用電話號碼登入'), findsOneWidget); | ||
}, | ||
); | ||
}, | ||
); | ||
|
||
group( | ||
'Localization override', | ||
() { | ||
localizedText(BuildContext context) => FirebaseUILocalizations.labelsOf(context).verifyEmailTitle; | ||
|
||
test( | ||
'Overrides the DefaultLocalizations', | ||
() async { | ||
final localizations = await const FirebaseUILocalizationDelegate( | ||
DefaultLocalizationsOverrides(), | ||
).load(localeTW); | ||
expect(localizations.labels.verifyEmailTitle, 'Overwritten'); | ||
}, | ||
); | ||
|
||
testWidgets( | ||
'UI test for the default translation override', | ||
(tester) async { | ||
await tester.pumpWidget( | ||
TestMaterialApp( | ||
locale: localeZh, | ||
localizationsOverride: const FirebaseUILocalizationDelegate( | ||
DefaultLocalizationsOverrides(), | ||
), | ||
localizedText: localizedText, | ||
), | ||
); | ||
expect(find.text('Overwritten'), findsOneWidget); | ||
}, | ||
); | ||
|
||
test( | ||
'Overrides the DefaultLocalizations', | ||
() async { | ||
final localizations = await const FirebaseUILocalizationDelegate( | ||
ZhTWLocalizationsOverrides(), | ||
).load(localeTW); | ||
expect(localizations.labels.verifyEmailTitle, '覆寫標題'); | ||
}, | ||
); | ||
|
||
testWidgets( | ||
'UI test for the "${localeTW.toLanguageTag()}" translation override', | ||
(tester) async { | ||
await tester.pumpWidget( | ||
TestMaterialApp( | ||
locale: localeTW, | ||
localizationsOverride: const FirebaseUILocalizationDelegate( | ||
ZhTWLocalizationsOverrides(), | ||
), | ||
localizedText: localizedText, | ||
), | ||
); | ||
expect(find.text('覆寫標題'), findsOneWidget); | ||
}, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
class DefaultLocalizationsOverrides extends DefaultLocalizations { | ||
const DefaultLocalizationsOverrides(); | ||
|
||
@override | ||
String get verifyEmailTitle => 'Overwritten'; | ||
} | ||
|
||
class ZhTWLocalizationsOverrides extends ZhTWLocalizations { | ||
const ZhTWLocalizationsOverrides(); | ||
|
||
@override | ||
String get verifyEmailTitle => '覆寫標題'; | ||
} | ||
|
||
class TestMaterialApp extends StatelessWidget { | ||
final Locale locale; | ||
final LocalizationsDelegate? localizationsOverride; | ||
final String Function(BuildContext context) localizedText; | ||
LaemonT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const TestMaterialApp({ | ||
super.key, | ||
required this.locale, | ||
this.localizationsOverride, | ||
required this.localizedText, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) => MaterialApp( | ||
LaemonT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
supportedLocales: const [localeZh, localeTW], | ||
locale: locale, | ||
localizationsDelegates: [ | ||
GlobalMaterialLocalizations.delegate, | ||
GlobalCupertinoLocalizations.delegate, | ||
localizationsOverride == null ? FirebaseUILocalizations.delegate : localizationsOverride!, | ||
], | ||
home: Builder( | ||
builder: (context) => Text( | ||
localizedText.call(context), | ||
), | ||
), | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.