diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..e64ddad --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,53 @@ +# Information for AI Agents + +This repository provides `arb_utils`, a CLI tool for working with `.arb` files in Flutter. + +## Key Commands for Agents + +### 1. `keys`: List and search existing keys +Use this command to discover if a translation key already exists or to find similar keys. +```sh +arb_utils keys +``` +Example: `arb_utils keys ^welcome` + +### 2. `add`: Add multiple translations at once +Use this command to add a new key with translations for all locales in one go. + +#### Simple syntax +```sh +arb_utils add --description '' [locale:value...] +``` +Example: +```sh +arb_utils add welcome --description 'Welcome message' en:'Welcome!' es:'¡Bienvenido!' +``` + +#### Complex syntax (Plurals, Select, etc.) +For complex ARB values, use the `--json` flag with the `$VAL$` placeholder. +```sh +arb_utils add --json '{"messageCount": "$VAL$", "@messageCount": {"description": "Plural message example", "placeholders": {"count": {"type": "int","example":42}}}}' en:'{count, plural, =0{No messages} one{1 message} other{{count} messages}}' es:'{count, plural, =0{No hay mensajes} one{1 mensaje} other{{count} mensajes}}' +``` + +Another example with `select`: +```sh +arb_utils add --json '{"genderSelect": "$VAL$", "@genderSelect": {"description": "Select example", "placeholders": {"gender": {"type": "String"}}}}' en:'{gender, select, male{He} female{She} other{They}}' es:'{gender, select, male{Él} female{Ella} other{Ellos}}' +``` + +## Agent Skills + +### Skill: Safe Translation Addition +Before adding a new translation, an agent should: +1. Search for existing similar keys using `arb_utils keys`. +2. If no suitable key exists, add the new one using `arb_utils add`. + +Example Workflow: +1. Agent needs to add a "login" button label. +2. Run `arb_utils keys login`. +3. If "loginButton" exists, use it. +4. If not, run `arb_utils add loginButton --description 'Label for login button' en:'Login' es:'Iniciar sesión'`. + +### Important Notes +1. **Auto-Discovery**: Commands recursively find all `.arb` files. Use `--files` to limit scope. +2. **Strict Locale Check**: `add` fails if any locale is missing from the command. +3. **Order**: `add` appends to the end by default. Use `--sort` to re-sort after adding. diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b06a79..3b08ff6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # CHANGELOG +## 0.11.0 + +- Added improved `add` command with human-friendly and JSON template syntax. +- Added support for multiple translations in a single command. +- Added recursive auto-discovery for .arb files. +- Added `AGENTS.md` for AI agent instructions. +- Fixed pretty-printing for merge and add operations. + ## 0.10.1 - Adjust dcli dependency to allow >=6.0.0 and <=9.0.0 diff --git a/README.md b/README.md index 7e37a48..5377d38 100644 --- a/README.md +++ b/README.md @@ -319,18 +319,49 @@ arb_utils sort where `` is a path to the input file. -Also, there are 3 flags for different sorting. +Also, there are 3 flags for different sorting: ++ **Case insensitive** (`--case-insensitive` / `-i`): Whether to distinguish between lowercase and uppercase while ordering the keys. ++ **Natural ordering** (`--natural-ordering` / `-n`): Whether to take numbers as a single character and order them according to their numeric value (instead of ASCII one). ++ **Descending** (`--descending` / `-d`): Sort in descending order. -+ --case-insensitive / -i -+ --natural-ordering / -n -+ --descending / -d - -For example, to sort with case insensitive and in descending order, use +For example, to sort with case insensitive and in descending order, use: ```sh arb_utils sort -i -d ``` +##### Add Entries + +To programmatically add new entries to one or more arb files in one go, use + +```sh +arb_utils add --description '' [locale:value...] +``` + +Example: +```sh +arb_utils add welcome --description 'Welcome message' en:'Welcome!' es:'¡Bienvenido!' +``` + +Or using a JSON template: +```sh +arb_utils add --json '{"welcome": "$VAL$", "@welcome": {"description": "Welcome message"}}' en:'Welcome!' es:'¡Bienvenido!' +``` + +The command will recursively find all `.arb` files in the current directory and its subdirectories by default. To specify specific files, use the `--files` (or `-f`) flag. +New entries are added to the tail of the files by default. Use the `--sort` (or `-s`) flag to sort the files after adding. +Note: The command will fail if values for all locales present in the `.arb` files are not provided. + +##### List Keys + +To list keys matching a regexp, use + +```sh +arb_utils keys +``` + +This will search in all `.arb` files found recursively by default. You can also specify files with `--files` flag. + ##### Add Missing Default Metadata To easily add missing metadata to an arb and update original file, use diff --git a/bin/arb_utils.dart b/bin/arb_utils.dart index a7f86c6..7b1131a 100755 --- a/bin/arb_utils.dart +++ b/bin/arb_utils.dart @@ -1,3 +1,5 @@ +import 'package:arb_utils/src/commands/add.dart'; +import 'package:arb_utils/src/commands/keys.dart'; import 'package:arb_utils/src/commands/generate_meta.dart'; import 'package:arb_utils/src/commands/merge.dart'; import 'package:arb_utils/src/commands/sort.dart'; @@ -8,7 +10,9 @@ void main(List args) async { final runner = CommandRunner('arb_utils', 'A set of utilities for working with arb files.') ..addCommand(GenerateMetaCommand()) ..addCommand(SortCommand()) - ..addCommand(MergeCommand()); + ..addCommand(MergeCommand()) + ..addCommand(AddCommand()) + ..addCommand(KeysCommand()); try { await runner.run(args); diff --git a/lib/src/commands/add.dart b/lib/src/commands/add.dart new file mode 100644 index 0000000..66e1f71 --- /dev/null +++ b/lib/src/commands/add.dart @@ -0,0 +1,204 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:arb_utils/arb_utils.dart'; +import 'package:args/command_runner.dart'; +import 'package:dcli/dcli.dart'; + +class AddCommand extends Command { + @override + String get name => 'add'; + @override + String get description => + 'Add entries to one or more arb files. By default it appends entries to the end of the files.'; + + @override + String get invocation => + '${super.invocation} [locale:value...] OR ${super.invocation} --json \'