Skip to content

Commit

Permalink
Small fixes to package:messages (#760)
Browse files Browse the repository at this point in the history
* Small fixes to package:messages

* Add changelogs
  • Loading branch information
mosuem authored Dec 14, 2023
1 parent 92ef32a commit c84b9a6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 8 deletions.
4 changes: 4 additions & 0 deletions pkgs/messages/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.1

- Update README.

## 0.1.0

- Initial version.
36 changes: 31 additions & 5 deletions pkgs/messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ The `builder` to generate the named methods and data files from the input `arb`
The logic for serializing `arb` message files into data files.

## Example
Add `package:messages` and `package:messages_builder` to your dependencies:
```bash
dart pub add messages
dart pub add dev:messages_builder
```

Given translation message files such as these `.arb`s:

Given translation message files in two languages:
`en.arb`
This reference file is written by the developer.
```json
{
"@@locale":"en",
Expand All @@ -51,21 +57,25 @@ Given translation message files such as these `.arb`s:
}
}
```

and `fr.arb`
This translated file was created by a translator given the reference `en.arb`.
```json
{
"@@locale":"fr",
"@@context": "AboutPage",
"aboutMessage": "À propos de {website}",
}
```
you can then run

insert the message in your Dart application through
`dart run build_runner build -d`

This will generate both code to call your messages, as well as data files which will be shipped with your application. You can then use these generated files by importing the generated files:

```dart
import 'dart:io';
import 'package:example/aboutpage_arb_file.g.dart';
import 'package:example/aboutpage_arb_file.g.dart'; // <-- This was generated right now
import 'package:messages/messages_native.dart';
import 'package:messages/package_intl_object.dart';
Expand All @@ -79,4 +89,20 @@ void main() {
aboutPageMessages.loadLocale('en');
print(aboutPageMessages.aboutMessage(website: 'mywebsite.com')); // 'About mywebsite.com'
}
```

Optionally, add options to your `pubspec.yaml` by specifying any of these keys:
```yaml
package_options:
messages_builder:
generateMethods: true # whether to generate named methods
generateFindById: false # whether to allow messages to be retrieved by ID
generateFindBy: integer # {none, integer, enumerate}, the type of key to use
header: | # the custom header message. Will always be prefixed by // in the generated code.
Generated by flutter_messages.
// Some custom
// multi
// line
// header
```
2 changes: 1 addition & 1 deletion pkgs/messages/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: messages
description: A lightweight modular library for localization (l10n) functionality.
version: 0.1.0
version: 0.1.1
repository: https://github.com/dart-lang/i18n/tree/main/pkgs/messages

environment:
Expand Down
4 changes: 4 additions & 0 deletions pkgs/messages_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.1

- Add `header` to `GenerationOptions`.

## 0.1.0

- Initial version.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class LibraryGeneration extends Generation<Library> {

return [
Library((b) => b
..comments.add('Generated by package:messages_builder')
..comments.add(options.header)
..directives.addAll(imports)
..body.addAll(classes))
];
Expand Down
5 changes: 5 additions & 0 deletions pkgs/messages_builder/lib/generation_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ class GenerationOptions {
/// dart native code.
final DeserializationType deserialization;

final String header;

GenerationOptions({
required this.serialization,
required this.deserialization,
required this.messageCalls,
required this.findById,
required this.indexType,
required this.header,
});

static Future<GenerationOptions> fromPubspec(BuildStep buildStep) async {
Expand All @@ -60,6 +63,8 @@ class GenerationOptions {
type.name == messagesOptions?['generateFindBy'] as String?)
.firstOrNull ??
IndexType.integer,
header: messagesOptions?['header'] as String? ??
'Generated by package:messages_builder.',
);
return generationOptions;
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/messages_builder/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: messages_builder
description: Build the messages for consumption by package:messages
version: 0.1.0
version: 0.1.1
repository: https://github.com/dart-lang/i18n/pkgs/messages_builder

environment:
Expand Down

0 comments on commit c84b9a6

Please sign in to comment.