Use common v0.8.0 UI, transport, and translation APIs

This commit is contained in:
Storm Dragon committed 2026-11-16 20:10:47 -05:00
1 parent 317303adb7
commit 92fd479336
344 files changed
+4102 -4973

No files matched your search

-3
View File
@@ -51,9 +51,6 @@
"most",
"all"
],
"internal/emoji/catalog.go": [
"All"
],
"internal/punctuation/text_processing.go": [
"number"
],
+35 -221
View File
@@ -2,256 +2,70 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Run from the repository root: go run ./tools/translations [check|update].
// Extraction, merging and checks are shared with Cthulhu through
// screenreader-common's catalogtool; this file holds Fenrir's rules.
package main
import (
"bytes"
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"slices"
"strconv"
"strings"
commonemoji "git.stormux.org/storm/screenreader-common/emoji"
"git.stormux.org/storm/screenreader-common/internalui"
"git.stormux.org/storm/screenreader-common/quicksettings"
"git.stormux.org/storm/screenreader-common/voiceprofiles"
"github.com/leonelquinteros/gotext"
"git.stormux.org/storm/screenreader-common/i18n/catalogtool"
"github.com/pelletier/go-toml/v2"
)
const catalogDir = "internal/i18n/locale"
var config = catalogtool.Config{
Domain: "fenrir",
Project: "Fenrir",
Command: "go run ./tools/translations",
CatalogDir: "internal/i18n/locale",
SourceRoot: "internal",
Rules: catalogtool.Rules{
Calls: []string{"i18n.T", "i18n.Format", ".say", ".failed"},
Plural: []string{"i18n.N"},
Keys: []string{"Label", "Title"},
AllValueFiles: []string{"internal/command/descriptions.go", "internal/punctuation/emoticons.go"},
Tags: []string{"label", "quick"},
Vars: func(name string) bool {
return name == "MenuTitle" || strings.HasPrefix(name, "tutorial") && name != "tutorialMode"
},
},
Shared: catalogtool.Shared{Emoji: true, QuickSettings: true, InternalUI: true, VoiceProfiles: true},
DynamicFile: "tools/translations/dynamic.json",
Extra: punctuationNames,
}
func main() {
if err := run(); err != nil {
mode := ""
if len(os.Args) > 1 {
mode = os.Args[1]
}
if err := catalogtool.Run(config, mode, os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
messages, err := extract()
if err != nil {
return err
}
var pot bytes.Buffer
pot.WriteString("# Fenrir reader messages. Generated by go run ./tools/translations.\nmsgid \"\"\nmsgstr \"\"\n\"Project-Id-Version: fenrir\\n\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n\"Content-Transfer-Encoding: 8bit\\n\"\n")
ids := make([]string, 0, len(messages))
for id := range messages {
ids = append(ids, id)
}
slices.Sort(ids)
for _, id := range ids {
refs := messages[id]
slices.Sort(refs)
fmt.Fprintf(&pot, "\n#: %s\nmsgid %s\nmsgstr \"\"\n", strings.Join(slices.Compact(refs), " "), strconv.Quote(id))
}
mode := "extract"
if len(os.Args) > 1 {
mode = os.Args[1]
}
potPath := filepath.Join(catalogDir, "fenrir.pot")
if mode == "check" {
existing, err := os.ReadFile(potPath)
if err != nil {
return err
}
if !bytes.Equal(existing, pot.Bytes()) {
return fmt.Errorf("%s is stale; run go run ./tools/translations update", potPath)
}
} else if mode == "extract" || mode == "update" {
if err := os.WriteFile(potPath, pot.Bytes(), 0644); err != nil {
return err
}
} else {
return fmt.Errorf("usage: go run ./tools/translations [check|update]")
}
files, err := filepath.Glob(catalogDir + "/*/LC_MESSAGES/fenrir.po")
if err != nil {
return err
}
if len(files) == 0 {
return fmt.Errorf("no language catalogs")
}
fields := regexp.MustCompile(`\{(?:[0-9]+|[a-z_]+)?\}`)
for _, file := range files {
if mode == "update" {
output, err := exec.Command("msgmerge", "--update", "--backup=none", "--no-fuzzy-matching", file, potPath).CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %s: %w", file, output, err)
}
}
if mode != "check" {
continue
}
data, err := os.ReadFile(file)
if err != nil {
return err
}
if regexp.MustCompile(`(?m)^#,.*\bfuzzy\b`).Match(data) {
return fmt.Errorf("%s: fuzzy translations require review", file)
}
output, err := exec.Command("msgfmt", "--check", "--check-format", "-o", os.DevNull, file).CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %s: %w", file, output, err)
}
po := gotext.NewPo()
po.ParseFile(file)
for _, id := range ids {
if !po.IsTranslated(id) {
return fmt.Errorf("%s: untranslated %q", file, id)
}
expected, actual := fields.FindAllString(id, -1), fields.FindAllString(po.Get(id), -1)
slices.Sort(expected)
slices.Sort(actual)
if !slices.Equal(expected, actual) {
return fmt.Errorf("%s: changed fields in %q", file, id)
}
translated := po.Get(id)
if strings.HasPrefix(id, " ") != strings.HasPrefix(translated, " ") || strings.HasSuffix(id, " ") != strings.HasSuffix(translated, " ") {
return fmt.Errorf("%s: changed boundary whitespace in %q", file, id)
}
}
fmt.Printf("%s: %d/%d translated\n", file, len(ids), len(ids))
}
fmt.Printf("%d reader message IDs, %d languages\n", len(ids), len(files))
return nil
}
func extract() (map[string][]string, error) {
messages := map[string][]string{}
add := func(id, ref string) {
if id != "" {
messages[id] = append(messages[id], ref)
}
}
allStrings := map[string]map[string]bool{}
err := filepath.WalkDir("internal", func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
fs := token.NewFileSet()
file, err := parser.ParseFile(fs, path, nil, 0)
if err != nil {
return err
}
literals := map[string]bool{}
allStrings[path] = literals
value := func(expr ast.Expr) string {
if lit, ok := expr.(*ast.BasicLit); ok && lit.Kind == token.STRING {
text, _ := strconv.Unquote(lit.Value)
return text
}
return ""
}
ref := func(n ast.Node) string { return fmt.Sprintf("%s:%d", path, fs.Position(n.Pos()).Line) }
ast.Inspect(file, func(n ast.Node) bool {
switch node := n.(type) {
case *ast.BasicLit:
if node.Kind == token.STRING {
literals[value(node)] = true
}
case *ast.CallExpr:
if fn, ok := node.Fun.(*ast.SelectorExpr); ok && len(node.Args) > 0 {
owner, _ := fn.X.(*ast.Ident)
if owner != nil && owner.Name == "i18n" && (fn.Sel.Name == "T" || fn.Sel.Name == "Format" || fn.Sel.Name == "N") || fn.Sel.Name == "say" || fn.Sel.Name == "failed" {
add(value(node.Args[0]), ref(node))
if fn.Sel.Name == "N" {
// Plural messages need plural POT entries and checks; fail
// explicitly until the first reader call site is introduced.
err = fmt.Errorf("plural extraction required at %s", ref(node))
}
}
}
case *ast.KeyValueExpr:
if key, ok := node.Key.(*ast.Ident); ok && (key.Name == "Label" || key.Name == "Title") {
add(value(node.Value), ref(node))
}
if path == "internal/command/descriptions.go" || path == "internal/punctuation/emoticons.go" {
add(value(node.Value), ref(node))
}
case *ast.Field:
if node.Tag != nil {
tag := reflect.StructTag(value(node.Tag))
for _, name := range []string{"label", "quick"} {
add(tag.Get(name), ref(node))
}
}
case *ast.ValueSpec:
for index, name := range node.Names {
if index < len(node.Values) && (name.Name == "MenuTitle" || strings.HasPrefix(name.Name, "tutorial") && name.Name != "tutorialMode") {
add(value(node.Values[index]), ref(node))
}
}
}
return true
})
return err
})
if err != nil {
return nil, err
}
// Shared picker data and toolkit messages live in screenreader-common, outside the reader's
// internal/ tree scanned above. Its English labels remain Fenrir message IDs.
for _, category := range commonemoji.Categories() {
add(category.Label, "screenreader-common/emoji/catalog.go")
for _, entry := range category.Entries {
add(entry.Label, "screenreader-common/emoji/catalog.go")
}
}
for _, id := range quicksettings.MessageIDs() {
add(id, "screenreader-common/quicksettings/menu.go")
}
for _, id := range internalui.MessageIDs() {
add(id, "screenreader-common/internalui")
}
for _, id := range voiceprofiles.MessageIDs() {
add(id, "screenreader-common/voiceprofiles/editor.go")
}
// Dynamic labels without a directly extractable lookup. Each must still
// occur in its source, so removals/renames cannot silently leave stale IDs.
data, err := os.ReadFile("tools/translations/dynamic.json")
if err != nil {
return nil, err
}
var dynamic map[string][]string
if err := json.Unmarshal(data, &dynamic); err != nil {
return nil, err
}
for path, ids := range dynamic {
for _, id := range ids {
if !allStrings[path][id] {
return nil, fmt.Errorf("stale dynamic message %q in %s", id, path)
}
add(id, path)
}
}
// punctuationNames adds symbol names from punctuation profiles marked for
// translation.
func punctuationNames(add func(id, ref string)) error {
profiles, err := filepath.Glob("config/punctuation/*.toml")
if err != nil {
return nil, err
return err
}
for _, path := range profiles {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
return err
}
var profile struct {
Translate bool
Symbols map[string]string
}
if err := toml.Unmarshal(data, &profile); err != nil {
return nil, err
return err
}
if profile.Translate {
for _, name := range profile.Symbols {
@@ -259,5 +73,5 @@ func extract() (map[string][]string, error) {
}
}
}
return messages, nil
return nil
}