From 8026a452fc73576245c7563bed87dd5b39fd68a2 Mon Sep 17 00:00:00 2001 From: Renatillas Date: Fri, 12 Sep 2025 01:10:45 +0200 Subject: [PATCH] Fix bug --- CHANGELOG.md | 16 +++++++++++++ README.md | 33 +++++++++++++++++++++------ src/g18n/dev.gleam | 56 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e17ad23..a391d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.0] - 2024-12-01 + +### Added + +- **New `report` command**: Generate comprehensive translation coverage reports + - `gleam run -m g18n/dev report` - Show translation coverage report + - `gleam run -m g18n/dev report --primary ` - Set primary locale for comparison +- **Automatic format detection**: Report command automatically detects and handles flat JSON, nested JSON, and PO file formats +- **Detailed coverage analysis**: Shows coverage percentages, missing translation keys, and validation errors for each locale +- **Built-in validation**: Uses g18n's `validate_translations()` and `export_validation_report()` functions for accurate reporting + +### Fixed + +- Improved error handling for invalid locale codes and missing translation files +- Better user feedback when no translation files are found + ## [1.0.0] - 2024-12-01 ### Added diff --git a/README.md b/README.md index e2a5d27..b5973f0 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The CLI provides commands to generate Gleam translation modules from various fil ### Generate from Flat JSON Files ```sh -gleam run generate +gleam run -m g18n/dev generate ``` Place flat JSON files in `src//translations/` directory: @@ -35,7 +35,7 @@ Place flat JSON files in `src//translations/` directory: ### Generate from Nested JSON Files ```sh -gleam run generate --nested +gleam run -m g18n/dev generate --nested ``` Place nested JSON files in `src//translations/` directory: @@ -60,7 +60,7 @@ This format is compatible with popular i18n libraries like react-i18next, Vue i1 ### Generate from PO Files (gettext) ```sh -gleam run generate --po +gleam run -m g18n/dev generate --po ``` Place PO files in `src//translations/` directory: @@ -73,10 +73,29 @@ msgid "user.name" msgstr "Name" ``` +### Translation Coverage Report + +```sh +gleam run -m g18n/dev report +``` + +Generate a comprehensive translation coverage report that automatically detects your file format (flat JSON, nested JSON, or PO files) and shows: + +- Coverage percentages for each locale +- Missing translation keys +- Validation errors and warnings +- Primary locale comparison + +```sh +gleam run -m g18n/dev report --primary es +``` + +Use a specific locale as the primary reference for comparison. + ### Help ```sh -gleam run help +gleam run -m g18n/dev help ``` ## File Naming Convention @@ -114,9 +133,9 @@ pub fn main() { ## Development ```sh -gleam run # Show help -gleam test # Run the tests -gleam format # Format the code +gleam run -m g18n/dev help # Show help +gleam test # Run the tests +gleam format # Format the code ``` ## Documentation diff --git a/src/g18n/dev.gleam b/src/g18n/dev.gleam index 3936045..9464975 100644 --- a/src/g18n/dev.gleam +++ b/src/g18n/dev.gleam @@ -152,8 +152,7 @@ fn generate_po_translations() -> SnagResult(String) { fn generate_translation_report(primary_locale: String) -> SnagResult(Nil) { use project_name <- result.try(get_project_name()) - use locale_files <- result.try(find_locale_files(project_name)) - use locale_data <- result.try(load_all_locales(locale_files)) + use locale_data <- result.try(try_load_translations(project_name)) // Determine primary locale let primary = case primary_locale { @@ -202,6 +201,59 @@ fn generate_translation_report(primary_locale: String) -> SnagResult(Nil) { } } +fn try_load_translations(project_name: String) -> SnagResult(List(#(String, g18n.Translations))) { + // Try flat JSON first + case find_locale_files(project_name) { + Ok(files) -> { + case load_all_locales(files) { + Ok(data) -> { + io.println("📁 Detected format: Flat JSON") + Ok(data) + } + Error(_) -> { + // Try nested JSON + case load_all_locales_from_nested(files) { + Ok(data) -> { + io.println("📁 Detected format: Nested JSON") + Ok(data) + } + Error(_) -> { + // Try PO files + case find_po_files(project_name) { + Ok(po_files) -> { + case load_all_locales_from_po(po_files) { + Ok(data) -> { + io.println("📁 Detected format: PO files") + Ok(data) + } + Error(e) -> Error(e) + } + } + Error(_) -> snag.error("No valid translation files found. Tried flat JSON, nested JSON, and PO formats.") + } + } + } + } + } + } + Error(_) -> { + // Try PO files directly if no JSON files found + case find_po_files(project_name) { + Ok(po_files) -> { + case load_all_locales_from_po(po_files) { + Ok(data) -> { + io.println("📁 Detected format: PO files") + Ok(data) + } + Error(e) -> Error(e) + } + } + Error(_) -> snag.error("No translation files found in src/" <> project_name <> "/translations/") + } + } + } +} + fn format() { shellout.command("gleam", ["format"], in: find_root("."), opt: []) |> snag.map_error(fn(_) { "Could not format generated file" }) -- 2.51.2