Skip to content

Commit

Permalink
feat(fronted): add catalyst_voices_localization pub (#155)
Browse files Browse the repository at this point in the history
* feat(fronted): add catalyst_voices_localization pub

* Delete l10n.dart

* feat: update localization

* Update project.dic

* wip

* feat: update localization pub

* Update README.md

* Update README.md
  • Loading branch information
minikin authored Nov 23, 2023
1 parent e82e3c8 commit a17bd81
Show file tree
Hide file tree
Showing 25 changed files with 506 additions and 164 deletions.
2 changes: 2 additions & 0 deletions .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ auditability
bluefireteam
BROTLI
cardano
Catalyst
CEST
cfbundle
chromedriver
Expand All @@ -17,6 +18,7 @@ COCOAPODS
codepoints
coti
cryptoxide
Cupertino
dbsync
delegators
dockerhub
Expand Down
108 changes: 0 additions & 108 deletions catalyst_voices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
* [Getting Started](#getting-started)
* [Bootstrapping](#bootstrapping)
* [Running Tests](#running-tests)
* [Working with Translations](#working-with-translations)
* [Adding Strings](#adding-strings)
* [Adding Supported Locales](#adding-supported-locales)
* [Adding Translations](#adding-translations)

## Requirements

Expand Down Expand Up @@ -80,107 +76,3 @@ genhtml coverage/lcov.info -o coverage/
# Open Coverage Report
open coverage/index.html
```

## Working with Translations

This project relies on [flutter_localizations](https://github.com/flutter/flutter/tree/master/packages/flutter_localizations).
It follows the [official internationalization guide for Flutter][flutter-intl-guide].

### Adding Strings

1. To add a new localizable string, open the `app_en.arb` file at `lib/l10n/arb/app_en.arb`.

```arb
{
"@@locale": "en",
"counterAppBarTitle": "Counter",
"@counterAppBarTitle": {
"description": "Text shown in the AppBar of the Counter Page"
}
}
```

2. Then add a new key/value and description

```arb
{
"@@locale": "en",
"counterAppBarTitle": "Counter",
"@counterAppBarTitle": {
"description": "Text shown in the AppBar of the Counter Page"
},
"helloWorld": "Hello World",
"@helloWorld": {
"description": "Hello World Text"
}
}
```

3. Use the new string

```dart
import 'package:catalyst_voices/l10n/l10n.dart';
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Text(l10n.helloWorld);
}
```

### Adding Supported Locales

Update the `CFBundleLocalizations` array in the `Info.plist` at `ios/Runner/Info.plist` to include the new locale.

```xml
...

<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>es</string>
</array>

...
```

### Adding Translations

1. For each supported locale, add a new ARB file in `lib/l10n/arb`.

```tree
├── l10n
│ ├── arb
│ │ ├── app_en.arb
│ │ └── app_es.arb
```

2. Add the translated strings to each `.arb` file:

`app_en.arb`

```arb
{
"@@locale": "en",
"counterAppBarTitle": "Counter",
"@counterAppBarTitle": {
"description": "Text shown in the AppBar of the Counter Page"
}
}
```

<!-- cspell: words Contador Texto mostrado página -->

`app_es.arb`

```arb
{
"@@locale": "es",
"counterAppBarTitle": "Contador",
"@counterAppBarTitle": {
"description": "Texto mostrado en la AppBar de la página del contador"
}
}
```

[flutter-intl-guide]: https://docs.flutter.dev/development/accessibility-and-localization/internationalization
4 changes: 0 additions & 4 deletions catalyst_voices/l10n.yaml

This file was deleted.

16 changes: 11 additions & 5 deletions catalyst_voices/lib/app/view/app.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import 'package:catalyst_voices/dummy/dummy.dart';
import 'package:catalyst_voices/l10n/l10n.dart';
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localized_locales/flutter_localized_locales.dart';

final class App extends StatelessWidget {
const App({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: LoginPage(),
return MaterialApp(
restorationScopeId: 'rootVoices',
localizationsDelegates: const [
...VoicesLocalizations.localizationsDelegates,
LocaleNamesLocalizationsDelegate(),
],
supportedLocales: VoicesLocalizations.supportedLocales,
localeListResolutionCallback: basicLocaleListResolution,
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.l10n.homeScreenText,
style: const TextStyle(
color: VoicesColors.purpleGradientStart,
fontFamily: VoicesFonts.sFPro,
fontSize: 32,
Expand Down
28 changes: 14 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 @@ -23,6 +20,7 @@ final class _LoginPageState extends State<LoginPage> {

@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Scaffold(
key: WidgetKeys.loginScreen,
body: Center(
Expand All @@ -39,9 +37,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: l10n.loginScreenUsernameLabelText,
),
),
),
Expand All @@ -51,9 +49,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: l10n.loginScreenPasswordLabelText,
),
),
),
Expand All @@ -62,7 +60,7 @@ final class _LoginPageState extends State<LoginPage> {
child: ElevatedButton(
key: WidgetKeys.loginButton,
onPressed: () async => _loginButtonPressed(context),
child: const Text(_Constants.loginButtonText),
child: Text(l10n.loginScreenLoginButtonText),
),
),
],
Expand Down Expand Up @@ -106,16 +104,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.l10n.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;
}
}
7 changes: 0 additions & 7 deletions catalyst_voices/lib/l10n/arb/app_en.arb

This file was deleted.

7 changes: 0 additions & 7 deletions catalyst_voices/lib/l10n/arb/app_es.arb

This file was deleted.

8 changes: 0 additions & 8 deletions catalyst_voices/lib/l10n/l10n.dart

This file was deleted.

44 changes: 44 additions & 0 deletions catalyst_voices/packages/catalyst_voices_localization/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# VSCode related
.vscode/*

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/
pubspec.lock

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Test related
coverage
Loading

0 comments on commit a17bd81

Please sign in to comment.