Skip to content

Commit

Permalink
feat: update localization
Browse files Browse the repository at this point in the history
  • Loading branch information
minikin committed Nov 21, 2023
1 parent 0bd21d5 commit a4e2f47
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 37 deletions.
6 changes: 3 additions & 3 deletions catalyst_voices/lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ final class App extends StatelessWidget {

@override
Widget build(BuildContext context) {
return const MaterialApp(
return MaterialApp(
restorationScopeId: 'rootVoices',
localizationsDelegates: [
localizationsDelegates: const [
...VoicesLocalizations.localizationsDelegates,
LocaleNamesLocalizationsDelegate(),
],
supportedLocales: VoicesLocalizations.supportedLocales,
localeListResolutionCallback: basicLocaleListResolution,
home: LoginPage(),
home: isUserLoggedIn ? const HomeScreen() : const LoginPage(),
);
}
}
2 changes: 2 additions & 0 deletions catalyst_voices/lib/dummy/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/foundation.dart';

bool isUserLoggedIn = false;

abstract class WidgetKeys {
static const loginScreen = Key('loginScreen');
static const usernameTextController = Key('usernameTextController');
Expand Down
7 changes: 4 additions & 3 deletions catalyst_voices/lib/dummy/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:catalyst_voices/dummy/dummy.dart';
import 'package:catalyst_voices_assets/catalyst_voices_assets.dart';
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:flutter/material.dart';

final class HomeScreen extends StatelessWidget {
Expand All @@ -13,9 +14,9 @@ final class HomeScreen extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Catalyst Voices',
style: TextStyle(
Text(
context.l.homeScreenText,
style: const TextStyle(
color: VoicesColors.purpleGradientStart,
fontFamily: VoicesFonts.sFPro,
fontSize: 32,
Expand Down
27 changes: 13 additions & 14 deletions catalyst_voices/lib/dummy/login_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:catalyst_voices/dummy/dummy.dart';
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:flutter/material.dart';

final class LoginPage extends StatefulWidget {
Expand All @@ -9,12 +10,8 @@ final class LoginPage extends StatefulWidget {
}

abstract class _Constants {
static const usernameLabelText = 'Username';
static const passwordLabelText = 'Password';
static const username = 'robot';
static const password = '1234';
static const errorMessage = 'Wrong credentials';
static const loginButtonText = 'Login';
}

final class _LoginPageState extends State<LoginPage> {
Expand All @@ -39,9 +36,9 @@ final class _LoginPageState extends State<LoginPage> {
child: TextFormField(
key: WidgetKeys.usernameTextController,
controller: usernameTextController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: _Constants.usernameLabelText,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: context.l.loginScreenUsernameLabelText,
),
),
),
Expand All @@ -51,9 +48,9 @@ final class _LoginPageState extends State<LoginPage> {
key: WidgetKeys.passwordTextController,
controller: passwordTextController,
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: _Constants.passwordLabelText,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: context.l.loginScreenPasswordLabelText,
),
),
),
Expand All @@ -62,7 +59,7 @@ final class _LoginPageState extends State<LoginPage> {
child: ElevatedButton(
key: WidgetKeys.loginButton,
onPressed: () async => _loginButtonPressed(context),
child: const Text(_Constants.loginButtonText),
child: Text(context.l.loginScreenLoginButtonText),
),
),
],
Expand Down Expand Up @@ -106,16 +103,18 @@ final class _LoginPageState extends State<LoginPage> {

void _showError(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
SnackBar(
key: WidgetKeys.loginErrorSnackbar,
content: Text(_Constants.errorMessage),
content: Text(context.l.loginScreenErrorMessage),
),
);
}

bool _validateCredentials() {
final username = usernameTextController.text;
final password = passwordTextController.text;
return username == _Constants.username && password == _Constants.password;

return isUserLoggedIn =
username == _Constants.username && password == _Constants.password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,35 @@ abstract class VoicesLocalizations {
Locale('es')
];

/// Text shown in the AppBar of the Counter Page
/// Text shown in the login screen for the username field
///
/// In en, this message translates to:
/// **'Counter'**
String get counterAppBarTitle;
/// **'Username'**
String get loginScreenUsernameLabelText;

/// Text shown in the login screen for the password field
///
/// In en, this message translates to:
/// **'Password'**
String get loginScreenPasswordLabelText;

/// Text shown in the login screen when the user enters wrong credentials
///
/// In en, this message translates to:
/// **'Wrong credentials'**
String get loginScreenErrorMessage;

/// Text shown in the login screen for the login button
///
/// In en, this message translates to:
/// **'Login'**
String get loginScreenLoginButtonText;

/// Text shown in the home screen
///
/// In en, this message translates to:
/// **'Catalyst Voices'**
String get homeScreenText;
}

class _VoicesLocalizationsDelegate extends LocalizationsDelegate<VoicesLocalizations> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@ class VoicesLocalizationsEn extends VoicesLocalizations {
VoicesLocalizationsEn([String locale = 'en']) : super(locale);

@override
String get counterAppBarTitle => 'Counter';
String get loginScreenUsernameLabelText => 'Username';

@override
String get loginScreenPasswordLabelText => 'Password';

@override
String get loginScreenErrorMessage => 'Wrong credentials';

@override
String get loginScreenLoginButtonText => 'Login';

@override
String get homeScreenText => 'Catalyst Voices';
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@ class VoicesLocalizationsEs extends VoicesLocalizations {
VoicesLocalizationsEs([String locale = 'es']) : super(locale);

@override
String get counterAppBarTitle => 'Contador';
String get loginScreenUsernameLabelText => 'Nombre de usuario';

@override
String get loginScreenPasswordLabelText => 'Contraseña';

@override
String get loginScreenErrorMessage => 'Credenciales incorrectas';

@override
String get loginScreenLoginButtonText => 'Acceso';

@override
String get homeScreenText => 'Catalyst Voices';
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
{
"@@locale": "en",
"counterAppBarTitle": "Counter",
"@counterAppBarTitle": {
"description": "Text shown in the AppBar of the Counter Page"
}
}
"@@locale": "en",
"loginScreenUsernameLabelText": "Username",
"@loginScreenUsernameLabelText": {
"description": "Text shown in the login screen for the username field"
},
"loginScreenPasswordLabelText": "Password",
"@loginScreenPasswordLabelText": {
"description": "Text shown in the login screen for the password field"
},
"loginScreenErrorMessage": "Wrong credentials",
"@loginScreenErrorMessage": {
"description": "Text shown in the login screen when the user enters wrong credentials"
},
"loginScreenLoginButtonText": "Login",
"@loginScreenLoginButtonText": {
"description": "Text shown in the login screen for the login button"
},
"homeScreenText": "Catalyst Voices",
"@homeScreenText": {
"description": "Text shown in the home screen"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"@@locale": "es",
"counterAppBarTitle": "Contador",
"@counterAppBarTitle": {
"description": "Texto mostrado en la AppBar de la página del contador"
}
}
"@@locale": "es",
"loginScreenUsernameLabelText": "Nombre de usuario",
"loginScreenPasswordLabelText": "Contraseña",
"loginScreenErrorMessage": "Credenciales incorrectas",
"loginScreenLoginButtonText": "Acceso",
"homeScreenLoginButtonText": "Catalyst Voices - Inicio 🇪🇸"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:catalyst_voices_localization/generated/catalyst_voices_localizations.dart';
import 'package:flutter/widgets.dart';

extension BuildContextLocalizationExtension on BuildContext {
VoicesLocalizations get l => VoicesLocalizations.of(this)!;
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'package:catalyst_voices_localization/generated/catalyst_voices_localizations.dart';
export 'package:catalyst_voices_localization/src/build_context_localization_extension.dart';

0 comments on commit a4e2f47

Please sign in to comment.