Skip to content

Commit

Permalink
feat: update localization pub
Browse files Browse the repository at this point in the history
  • Loading branch information
minikin committed Nov 22, 2023
1 parent 0add6e3 commit f4288f6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 70 deletions.
1 change: 1 addition & 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 Down
2 changes: 1 addition & 1 deletion catalyst_voices/lib/dummy/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class HomeScreen extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
context.l.homeScreenText,
context.l10n.homeScreenText,
style: const TextStyle(
color: VoicesColors.purpleGradientStart,
fontFamily: VoicesFonts.sFPro,
Expand Down
9 changes: 5 additions & 4 deletions catalyst_voices/lib/dummy/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,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 @@ -38,7 +39,7 @@ final class _LoginPageState extends State<LoginPage> {
controller: usernameTextController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: context.l.loginScreenUsernameLabelText,
labelText: l10n.loginScreenUsernameLabelText,
),
),
),
Expand All @@ -50,7 +51,7 @@ final class _LoginPageState extends State<LoginPage> {
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: context.l.loginScreenPasswordLabelText,
labelText: l10n.loginScreenPasswordLabelText,
),
),
),
Expand All @@ -59,7 +60,7 @@ final class _LoginPageState extends State<LoginPage> {
child: ElevatedButton(
key: WidgetKeys.loginButton,
onPressed: () async => _loginButtonPressed(context),
child: Text(context.l.loginScreenLoginButtonText),
child: Text(l10n.loginScreenLoginButtonText),
),
),
],
Expand Down Expand Up @@ -105,7 +106,7 @@ final class _LoginPageState extends State<LoginPage> {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
key: WidgetKeys.loginErrorSnackbar,
content: Text(context.l.loginScreenErrorMessage),
content: Text(context.l10n.loginScreenErrorMessage),
),
);
}
Expand Down
129 changes: 65 additions & 64 deletions catalyst_voices/packages/catalyst_voices_localization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ This package contains the localization files for the Catalyst Voices app.
* [Catalyst Voices Localization](#catalyst-voices-localization)
* [Working with Translations](#working-with-translations)
* [Creating New Locale Messages](#creating-new-locale-messages)
* [Generating VoicesLocalizations](#generating-voiceslocalizations)
* [Adding Strings](#adding-strings)
* [Adding Supported Locales](#adding-supported-locales)
* [Adding Translations](#adding-translations)
* [Adding Supported Locales](#adding-supported-locales)
* [Generating VoicesLocalizations](#generating-voiceslocalizations)

## Working with Translations

Expand Down Expand Up @@ -36,105 +36,106 @@ Once you've updated `intl_en.arb` with the new message, regenerate the `VoicesLo
immediate use of the English message in your application code through the localizations delegate,
bypassing the need to wait for translation completion.

## Generating VoicesLocalizations

### Adding Strings

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

```arb
{
"@@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"
}
}
```

2. Then add a new key/value and description
* 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"
}
"@@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"
}
}
```

3. Use the new string
### Adding Translations

```dart
import 'package:catalyst_voices/l10n/l10n.dart';
* For each supported locale, add a new ARB file in `lib/l10n/`.

@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Text(l10n.helloWorld);
```tree
├── l10n
│ ├── intl_en.arb
│ ├── intl_es.arb
```

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

`intl_en.arb`

```arb
{
"@@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"
}
}
```

`app_es.arb`

<!-- cspell: words Contador Texto mostrado página -->
```arb
{
"@@locale": "es",
"loginScreenUsernameLabelText": "Nombre de usuario",
"loginScreenPasswordLabelText": "Contraseña",
}
```

### Adding Supported Locales

Update the `CFBundleLocalizations` array in the `Info.plist` at `ios/Runner/Info.plist` to include the new locale.
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:
## Generating VoicesLocalizations

`app_en.arb`
* Run the following command in `catalyst_voices_localization` package to
generate the `VoicesLocalizations` class:

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

<!-- cspell: words Contador Texto mostrado página -->
* Use the new string

`app_es.arb`
```dart
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
```arb
{
"@@locale": "es",
"counterAppBarTitle": "Contador",
"@counterAppBarTitle": {
"description": "Texto mostrado en la AppBar de la página del contador"
}
@override
Widget build(BuildContext context) {
return Text(context.l10n.loginScreenPasswordLabelText);
}
```

```shell
flutter gen-l10n
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import 'package:catalyst_voices_localization/generated/catalyst_voices_localizat
import 'package:flutter/widgets.dart';

extension BuildContextLocalizationExtension on BuildContext {
VoicesLocalizations get l => VoicesLocalizations.of(this)!;
VoicesLocalizations get l10n => VoicesLocalizations.of(this)!;
}

0 comments on commit f4288f6

Please sign in to comment.