Repository navigation
feat(i18n): allow partial locale catalogues, with a coverage floor #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Command i18n-coverage prints how much of each catalogue every shipped language | ||
| // carries. | ||
| // | ||
| // It is a progress report, not a gate: the tests in internal/i18n are what fail a | ||
| // build. This exists because a translator needs to see 512/1324 rather than guess, and | ||
| // a reviewer needs to see whether a language is close enough to offer before reading it. | ||
| // | ||
| // Run it from the repository root: | ||
| // | ||
| // go run ./cmd/i18n-coverage | ||
| // make i18n-coverage | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/calnode/calnode/internal/i18n" | ||
| ) | ||
|
|
||
| type catalogue struct { | ||
| name string | ||
| dir string | ||
| // floor is the coverage below which the language is not offered to visitors. | ||
| // Zero means "no floor for this catalogue yet". | ||
| floor float64 | ||
| } | ||
|
|
||
| const sourceCode = "en" | ||
|
|
||
| func main() { | ||
| root := flag.String("root", ".", "repository root") | ||
| flag.Parse() | ||
|
|
||
| catalogues := []catalogue{ | ||
| {name: "booker (public surfaces)", dir: "internal/i18n/locales", floor: i18n.MinCoverage}, | ||
| {name: "admin (SPA)", dir: "frontend/src/lib/i18n", floor: 0}, | ||
| } | ||
|
|
||
| for _, c := range catalogues { | ||
| if err := report(*root, c); err != nil { | ||
| fmt.Fprintf(os.Stderr, "i18n-coverage: %s: %v\n", c.dir, err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func report(root string, c catalogue) error { | ||
| dir := filepath.Join(root, c.dir) | ||
| entries, err := os.ReadDir(dir) | ||
| if os.IsNotExist(err) { | ||
| fmt.Printf("%s\n %s does not exist yet, skipped\n\n", c.name, c.dir) | ||
| return nil | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| source, err := load(filepath.Join(dir, sourceCode+".json")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(source) == 0 { | ||
| return fmt.Errorf("%s has no strings", filepath.Join(dir, sourceCode+".json")) | ||
| } | ||
|
|
||
| type row struct { | ||
| code string | ||
| keys int | ||
| cover float64 | ||
| unknown int | ||
| } | ||
| var rows []row | ||
| for _, e := range entries { | ||
| if e.IsDir() || !strings.HasSuffix(e.Name(), ".json") { | ||
| continue | ||
| } | ||
| code := strings.TrimSuffix(e.Name(), ".json") | ||
| strings_, err := load(filepath.Join(dir, e.Name())) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| r := row{code: code, keys: len(strings_)} | ||
| for k := range strings_ { | ||
| if _, ok := source[k]; ok { | ||
| r.cover++ | ||
| } else { | ||
| r.unknown++ | ||
| } | ||
| } | ||
| r.cover = float64(r.cover) / float64(len(source)) | ||
| rows = append(rows, r) | ||
| } | ||
| sort.Slice(rows, func(i, j int) bool { return rows[i].code < rows[j].code }) | ||
|
|
||
| fmt.Printf("%s\n %s (source %s.json, %d keys)\n\n", c.name, c.dir, sourceCode, len(source)) | ||
| fmt.Printf(" %-7s %6s %8s %10s %8s\n", "code", "keys", "coverage", "status", "unknown") | ||
| for _, r := range rows { | ||
| status := "source" | ||
| switch { | ||
| case r.code == sourceCode: | ||
| case r.unknown > 0: | ||
| status = "has unknown keys" | ||
| case c.floor > 0 && r.cover < c.floor: | ||
| status = "withheld" | ||
| default: | ||
| status = "offered" | ||
| } | ||
| fmt.Printf(" %-7s %6d %7.0f%% %10s %8d\n", r.code, r.keys, r.cover*100, status, r.unknown) | ||
| } | ||
| if c.floor > 0 { | ||
| fmt.Printf("\n floor: %.0f%% (below it a language is not offered; ?lang=<code> still works)\n", c.floor*100) | ||
| } | ||
| fmt.Println() | ||
| return nil | ||
| } | ||
|
|
||
| // load reads one catalogue file and returns its flattened key set. Nested objects are | ||
| // joined with dots, so the admin catalogue's plural members count individually | ||
| // ("common.save", "teams.memberCount.one", ...). Arrays are not used in either | ||
| // catalogue and are skipped rather than guessed at. | ||
| func load(path string) (map[string]struct{}, error) { | ||
| // #nosec G304 -- path is the repository root this developer tool was invoked from | ||
| // plus a fixed catalogue name; there is no untrusted input, and this binary is never | ||
| // served. It reports on files a contributor is already editing. | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var raw map[string]any | ||
| if err := json.Unmarshal(b, &raw); err != nil { | ||
| return nil, fmt.Errorf("%s: %w", path, err) | ||
| } | ||
| out := map[string]struct{}{} | ||
| flatten("", raw, out) | ||
| return out, nil | ||
| } | ||
|
|
||
| func flatten(prefix string, v any, out map[string]struct{}) { | ||
| switch t := v.(type) { | ||
| case map[string]any: | ||
| for k, child := range t { | ||
| key := k | ||
| if prefix != "" { | ||
| key = prefix + "." + k | ||
| } | ||
| flatten(key, child, out) | ||
| } | ||
| case string: | ||
| out[prefix] = struct{}{} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Translation glossary | ||
|
|
||
| One concept, one word, per language. The point is that a booking does not become a | ||
| "rendez-vous" on one screen and a "réservation" on the next, and that a new translator does | ||
| not have to invent terminology that is already decided. | ||
|
|
||
| **How to use it** | ||
|
|
||
| - Before translating a term, look it up here. If it is missing, add it with its key. | ||
| - The values below were read from the shipped catalogues, not invented. If a catalogue | ||
| disagrees with this table, the table is wrong and should be fixed in the same PR. | ||
| - **Adding a language means adding a column.** Do not introduce a second word for a concept | ||
| that already has one. | ||
| - Two catalogues exist: the **booker** surfaces (`internal/i18n/locales`) and the **admin** | ||
| SPA (`frontend/src/lib/i18n`). They are separate systems with separate registers, and they | ||
| currently disagree in places. Those rows are marked **divergent** and are decisions, not | ||
| accidents. | ||
|
|
||
| ## Core terms | ||
|
|
||
| | Concept | English | French (booker) | French (admin) | Notes | | ||
| |---|---|---|---|---| | ||
| | booking (the object) | booking | réservation | réservation | Never "rendez-vous" for the object. | | ||
| | to book (verb) | book | réserver | réserver | | | ||
| | event type | event type | - | type de rendez-vous | **divergent**: the booker has no public name for it. | | ||
| | host | host | - | hôte | The booker addresses the booker, not the host. | | ||
| | attendee / booker | attendee | - | participant | Use "participant" in admin; the booker is "vous". | | ||
| | team | team | équipe | équipe | | | ||
| | team member | team member | membre de l'équipe | membre | | | ||
| | availability | availability | disponibilités | disponibilités | Plural in French. | | ||
| | slot / time slot | slot | créneau | créneau | | | ||
| | timezone | timezone | fuseau horaire | fuseau horaire | | | ||
| | reschedule | reschedule | reprogrammer | reprogrammer | Noun: "reprogrammation". | | ||
| | cancel | cancel | annuler | annuler | Noun: "annulation". | | ||
| | round robin | round robin | - | tourniquet | French admin uses "tourniquet"; do not calque "tourner entre eux". | | ||
| | reminder | reminder | rappel | rappel | | | ||
| | directory | directory | - | annuaire | Only the person and team pages. | | ||
|
|
||
| ## Register | ||
|
|
||
| | Surface | Register | Reason | | ||
| |---|---|---| | ||
| | Booker | Formal, second person plural (`vous`) | A customer booking a call from a business page. | | ||
| | Admin | Formal (`vous`), terse | An operator working in the tool all day. | | ||
|
|
||
| The existing booker locales are deliberately split by market: `fr`/`de`/`it`/`pt` formal, | ||
| `es`/`nl`/`sv` informal. Match the language, not the English. | ||
|
|
||
| ## Things that are not translated | ||
|
|
||
| Brand names, URLs, protocol tokens (`STARTTLS`, `RSVP`, `PII`), third-party console labels | ||
| ("APIs & Services", "Authorised redirect URIs"), and code identifiers. Translating those is | ||
| almost always a mistake; leave them byte-identical. | ||
|
|
||
| ## Placeholders | ||
|
|
||
| Never translate a placeholder. `%s`, `%d`, `%q` (booker) and `{name}` (admin) must survive | ||
| verbatim, and a missing one is a runtime bug rather than a stylistic choice. The tests check | ||
| this, but a test cannot see that you moved a word into the wrong place. | ||
|
|
||
| ## Cross-references | ||
|
|
||
| - `CONTRIBUTING.md` - how to add or extend a language | ||
| - `docs/ARCHITECTURE.md` §23 - how the catalogue system works and its limitations |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tells translators a missing key falls back to English and that
?lang=is a safe way to preview a partial file, but that only holds for server-rendered copy. The JS-driven tables on the booking page, manage page and embed widget are built fromLocale.JSON()and return the raw key on a miss, so a translator previewing a partial locale will seeconfirm_bookingrather than English. Either qualify this section (fallback applies to server-rendered strings only) or land theLocale.JSON()English merge before advertising the workflow.Technical details