|
|
@@ -2,6 +2,8 @@ package main
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
"sort"
|
|
|
@@ -14,8 +16,8 @@ import (
|
|
|
)
|
|
|
|
|
|
type LanguageFilev1 struct {
|
|
|
- SchemaVersion int `json:"schemaVersion"`
|
|
|
- Translations map[string]string `json:"translations"`
|
|
|
+ SchemaVersion yaml.Node `json:"schemaVersion" yaml:"schemaVersion"`
|
|
|
+ Translations map[string]string `json:"translations" yaml:"translations"`
|
|
|
}
|
|
|
|
|
|
type CombinedTranslationsOutput struct {
|
|
|
@@ -24,10 +26,14 @@ type CombinedTranslationsOutput struct {
|
|
|
}
|
|
|
|
|
|
func main() {
|
|
|
- combinedContent := getCombinedLanguageContent()
|
|
|
+ combinedContent, err := getCombinedLanguageContent()
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalf("Error loading language files: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
sortedContent := sortTranslations(combinedContent)
|
|
|
|
|
|
- err := writeTranslationFiles(sortedContent.Messages)
|
|
|
+ err = writeTranslationFiles(sortedContent.Messages)
|
|
|
if err != nil {
|
|
|
log.Fatalf("Error writing language content: %v", err)
|
|
|
}
|
|
|
@@ -115,7 +121,7 @@ func getLanguageDir() string {
|
|
|
return dir
|
|
|
}
|
|
|
|
|
|
-func getCombinedLanguageContent() *CombinedTranslationsOutput {
|
|
|
+func getCombinedLanguageContent() (*CombinedTranslationsOutput, error) {
|
|
|
output := &CombinedTranslationsOutput{
|
|
|
Comment: "This file is generated. Please re-generate this file using 'make' when you update a translation.",
|
|
|
Messages: make(map[string]map[string]string),
|
|
|
@@ -124,40 +130,59 @@ func getCombinedLanguageContent() *CombinedTranslationsOutput {
|
|
|
languageDir := getLanguageDir()
|
|
|
|
|
|
files, err := os.ReadDir(languageDir)
|
|
|
-
|
|
|
if err != nil {
|
|
|
- log.Errorf("Error reading language directory %s: %v", languageDir, err)
|
|
|
- return output
|
|
|
+ return nil, fmt.Errorf("reading language directory %s: %w", languageDir, err)
|
|
|
}
|
|
|
|
|
|
for _, file := range filterLanguageFiles(files) {
|
|
|
- languageName := strings.Replace(file.Name(), ".yaml", "", 1)
|
|
|
-
|
|
|
- fullPath := filepath.Join(languageDir, file.Name())
|
|
|
- log.Infof("Loading language file: %s", fullPath)
|
|
|
-
|
|
|
- content, err := os.ReadFile(fullPath)
|
|
|
+ languageName := strings.TrimSuffix(file.Name(), ".yaml")
|
|
|
|
|
|
+ translations, err := loadLanguageFile(filepath.Join(languageDir, file.Name()))
|
|
|
if err != nil {
|
|
|
- log.Errorf("Error reading language file %s: %v", fullPath, err)
|
|
|
- continue
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- var yamlData LanguageFilev1
|
|
|
+ output.Messages[languageName] = translations
|
|
|
+ }
|
|
|
|
|
|
- err = yaml.Unmarshal(content, &yamlData)
|
|
|
+ return output, validateTranslations(output)
|
|
|
+}
|
|
|
|
|
|
- if err != nil {
|
|
|
- log.Errorf("Error reading language file %s: %v", fullPath, err)
|
|
|
- continue
|
|
|
- }
|
|
|
+func loadLanguageFile(fullPath string) (map[string]string, error) {
|
|
|
+ log.Infof("Loading language file: %s", fullPath)
|
|
|
+
|
|
|
+ content, err := os.ReadFile(fullPath)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("reading language file %s: %w", fullPath, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ var yamlData LanguageFilev1
|
|
|
+
|
|
|
+ err = yaml.Unmarshal(content, &yamlData)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("parsing language file %s: %w", fullPath, err)
|
|
|
+ }
|
|
|
|
|
|
- output.Messages[languageName] = yamlData.Translations
|
|
|
+ err = validateSchemaVersion(&yamlData.SchemaVersion)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("language file %s: %w", fullPath, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(yamlData.Translations) == 0 {
|
|
|
+ return nil, fmt.Errorf("language file %s has no translations", fullPath)
|
|
|
}
|
|
|
|
|
|
- validateTranslations(output)
|
|
|
+ return yamlData.Translations, nil
|
|
|
+}
|
|
|
+
|
|
|
+// validateSchemaVersion checks the raw YAML scalar, because decoding straight
|
|
|
+// into an int silently truncates values such as 1.5 to 1.
|
|
|
+func validateSchemaVersion(node *yaml.Node) error {
|
|
|
+ if node.Kind != yaml.ScalarNode || node.ShortTag() != "!!int" || node.Value != "1" {
|
|
|
+ return fmt.Errorf("schemaVersion is %q, expected 1", node.Value)
|
|
|
+ }
|
|
|
|
|
|
- return output
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
// getReferenceKeys returns the keys from the "en" translation as the reference set.
|
|
|
@@ -196,29 +221,45 @@ func findExtraKeys(referenceKeys map[string]bool, translations map[string]string
|
|
|
return extra
|
|
|
}
|
|
|
|
|
|
-// validateTranslations checks all translations against the "en" reference and prints warnings for missing and extra keys.
|
|
|
-func validateTranslations(output *CombinedTranslationsOutput) {
|
|
|
+// validateTranslations checks all translations against the "en" reference.
|
|
|
+// Missing keys are only warnings, so that new English strings can be merged
|
|
|
+// before every language has been translated. Extra keys are errors, as they
|
|
|
+// are usually typos and are never displayed.
|
|
|
+func validateTranslations(output *CombinedTranslationsOutput) error {
|
|
|
referenceKeys := getReferenceKeys(output.Messages)
|
|
|
if referenceKeys == nil {
|
|
|
- log.Warnf("No 'en' translation found, skipping validation")
|
|
|
- return
|
|
|
+ return errors.New("no 'en' translation found to validate against")
|
|
|
}
|
|
|
|
|
|
+ invalidLanguages := make([]string, 0)
|
|
|
+
|
|
|
for langName, translations := range output.Messages {
|
|
|
- if langName == "en" {
|
|
|
- continue
|
|
|
+ if !checkLanguageKeys(langName, referenceKeys, translations) {
|
|
|
+ invalidLanguages = append(invalidLanguages, langName)
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- missing := findMissingKeys(referenceKeys, translations)
|
|
|
- if len(missing) > 0 {
|
|
|
- log.Warnf("Translation '%s' is missing %d key(s): %v", langName, len(missing), missing)
|
|
|
- }
|
|
|
+ if len(invalidLanguages) > 0 {
|
|
|
+ sort.Strings(invalidLanguages)
|
|
|
+ return fmt.Errorf("translations with keys that are not in 'en': %v", invalidLanguages)
|
|
|
+ }
|
|
|
|
|
|
- extra := findExtraKeys(referenceKeys, translations)
|
|
|
- if len(extra) > 0 {
|
|
|
- log.Warnf("Translation '%s' has %d extra key(s) not in 'en': %v", langName, len(extra), extra)
|
|
|
- }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func checkLanguageKeys(langName string, referenceKeys map[string]bool, translations map[string]string) bool {
|
|
|
+ missing := findMissingKeys(referenceKeys, translations)
|
|
|
+ if len(missing) > 0 {
|
|
|
+ log.Warnf("Translation '%s' is missing %d key(s): %v", langName, len(missing), missing)
|
|
|
+ }
|
|
|
+
|
|
|
+ extra := findExtraKeys(referenceKeys, translations)
|
|
|
+ if len(extra) > 0 {
|
|
|
+ log.Errorf("Translation '%s' has %d extra key(s) not in 'en': %v", langName, len(extra), extra)
|
|
|
+ return false
|
|
|
}
|
|
|
+
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
func filterLanguageFiles(files []os.DirEntry) []os.DirEntry {
|