Skip to content

Commit cfaf664

Browse files
committed
chore(i18n): port the base TS fixup script to go
- and run it as part of the standard `make compile-translations` subtarget, in order to keep the base Lokalise TS file in sync with the other TS files - script ported from Python to go so that we don't have to introduce another hard build time dependency - include the re-generated TS files as well Fixes #18934
1 parent 64a5e71 commit cfaf664

File tree

5 files changed

+15131
-10801
lines changed

5 files changed

+15131
-10801
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ log-update-translations:
538538
update-translations: | log-update-translations
539539
cmake -S $(TS_SOURCE_DIR) -B $(TS_BUILD_DIR) -Wno-dev $(HANDLE_OUTPUT)
540540
cmake --build $(TS_BUILD_DIR) --target update_application_translations $(HANDLE_OUTPUT)
541-
# + cd scripts/translationScripts && ./fixup-base-ts-for-lokalise.py $(HANDLE_OUTPUT)
541+
+ cd scripts/translationScripts && go run fixup-base-ts-for-lokalise.go $(HANDLE_OUTPUT)
542542

543543
log-compile-translations:
544544
echo -e "\033[92mCompiling:\033[39m translations"
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package main
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
// TS represents the root TS element
11+
type TS struct {
12+
XMLName xml.Name `xml:"TS"`
13+
Language string `xml:"language,attr"`
14+
SourceLanguage string `xml:"sourcelanguage,attr"`
15+
Contexts []Context `xml:"context"`
16+
}
17+
18+
// Context represents a context element
19+
type Context struct {
20+
Name string `xml:"name"`
21+
Messages []Message `xml:"message"`
22+
}
23+
24+
// Message represents a message element
25+
type Message struct {
26+
Numerus string `xml:"numerus,attr,omitempty"`
27+
Source string `xml:"source"`
28+
Comment string `xml:"comment,omitempty"`
29+
Translation Translation `xml:"translation"`
30+
Extra []xml.Token `xml:",any"` // For any extra elements like unfinished
31+
}
32+
33+
// Translation represents a translation element, handling numerus forms
34+
type Translation struct {
35+
Type string `xml:"type,attr,omitempty"`
36+
Text string `xml:",chardata"`
37+
NumerusForms []NumerusForm `xml:"numerusform,omitempty"`
38+
}
39+
40+
// NumerusForm represents a numerusform element
41+
type NumerusForm struct {
42+
Text string `xml:",chardata"`
43+
}
44+
45+
func main() {
46+
// Relative paths from project root
47+
baseFile := "../../ui/i18n/qml_base_en.ts"
48+
pluralFile := "../../ui/i18n/qml_en.ts"
49+
outputFile := "../../ui/i18n/qml_base_lokalise_en.ts"
50+
51+
// Parse the base TS file
52+
baseData, err := os.ReadFile(baseFile)
53+
if err != nil {
54+
fmt.Fprintf(os.Stderr, "Error reading %s: %v\n", baseFile, err)
55+
os.Exit(1)
56+
}
57+
var baseTS TS
58+
if err := xml.Unmarshal(baseData, &baseTS); err != nil {
59+
fmt.Fprintf(os.Stderr, "Error parsing %s: %v\n", baseFile, err)
60+
os.Exit(1)
61+
}
62+
63+
// Parse the plural TS file
64+
pluralData, err := os.ReadFile(pluralFile)
65+
if err != nil {
66+
fmt.Fprintf(os.Stderr, "Error reading %s: %v\n", pluralFile, err)
67+
os.Exit(1)
68+
}
69+
var pluralTS TS
70+
if err := xml.Unmarshal(pluralData, &pluralTS); err != nil {
71+
fmt.Fprintf(os.Stderr, "Error parsing %s: %v\n", pluralFile, err)
72+
os.Exit(1)
73+
}
74+
75+
// Create a dictionary for quick lookup of plural translations by source
76+
pluralLookup := make(map[string]Translation)
77+
for _, context := range pluralTS.Contexts {
78+
for _, message := range context.Messages {
79+
if message.Source != "" {
80+
pluralLookup[message.Source] = message.Translation
81+
}
82+
}
83+
}
84+
85+
// Process each message in the base file
86+
for i := range baseTS.Contexts {
87+
context := &baseTS.Contexts[i]
88+
contextName := context.Name
89+
for j := range context.Messages {
90+
message := &context.Messages[j]
91+
// Add comment if missing
92+
if message.Comment == "" {
93+
message.Comment = contextName
94+
}
95+
96+
if message.Numerus == "yes" && message.Source != "" {
97+
if pluralTrans, exists := pluralLookup[message.Source]; exists {
98+
message.Translation = pluralTrans
99+
// Clean up whitespace in text and numerus forms
100+
message.Translation.Text = strings.TrimSpace(message.Translation.Text)
101+
for k := range message.Translation.NumerusForms {
102+
message.Translation.NumerusForms[k].Text = strings.TrimSpace(message.Translation.NumerusForms[k].Text)
103+
}
104+
// Remove unfinished type
105+
if message.Translation.Type == "unfinished" {
106+
message.Translation.Type = ""
107+
}
108+
}
109+
} else {
110+
// Set translation to source, trimmed
111+
message.Translation.Text = strings.TrimSpace(message.Source)
112+
message.Translation.NumerusForms = nil // Clear numerus forms
113+
// Remove unfinished type
114+
if message.Translation.Type == "unfinished" {
115+
message.Translation.Type = ""
116+
}
117+
}
118+
}
119+
}
120+
121+
// Set language attributes
122+
baseTS.Language = "en"
123+
baseTS.SourceLanguage = "en"
124+
125+
// Write the modified XML to output file
126+
outputData, err := xml.MarshalIndent(baseTS, "", " ")
127+
if err != nil {
128+
fmt.Fprintf(os.Stderr, "Error marshaling XML: %v\n", err)
129+
os.Exit(1)
130+
}
131+
// Add XML declaration
132+
outputData = []byte(xml.Header + string(outputData))
133+
if err := os.WriteFile(outputFile, outputData, 0644); err != nil {
134+
fmt.Fprintf(os.Stderr, "Error writing to %s: %v\n", outputFile, err)
135+
os.Exit(1)
136+
}
137+
fmt.Printf("Successfully transformed %s to %s\n", baseFile, outputFile)
138+
}

scripts/translationScripts/fixup-base-ts-for-lokalise.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)