tools: align locale diagnostics and generate native language names
Validate / Validate (push) Successful in 2m46s
Validate / Validate (push) Successful in 2m46s
This commit is contained in:
1 parent
b8089e8a7e
commit
d99c5d1dc5
13 files changed
+170
-46
No files matched your search
+14
-13
@@ -20,16 +20,17 @@ import (
|
||||
)
|
||||
|
||||
type PreviewResult struct {
|
||||
Rendered map[string]map[string]map[string]string
|
||||
Registry map[string]map[string]string
|
||||
Params map[string]map[string]string
|
||||
Languages []string
|
||||
Themes []string
|
||||
Upstream string
|
||||
Commit string
|
||||
Logo []byte
|
||||
Fallback map[string][]string
|
||||
Warnings map[string]map[string][]string
|
||||
Rendered map[string]map[string]map[string]string
|
||||
Registry map[string]map[string]string
|
||||
Params map[string]map[string]string
|
||||
Languages []string
|
||||
LanguageNames map[string]string
|
||||
Themes []string
|
||||
Upstream string
|
||||
Commit string
|
||||
Logo []byte
|
||||
Fallback map[string][]string
|
||||
Warnings map[string]map[string][]string
|
||||
}
|
||||
|
||||
func DiscoverThemes(dir string) ([]string, error) { return builder.Discover(dir) }
|
||||
@@ -92,6 +93,7 @@ func RenderAll(themesDir string, cfg *config.TemplatesConfig, filter map[string]
|
||||
return nil, err
|
||||
}
|
||||
result := &PreviewResult{Rendered: map[string]map[string]map[string]string{}, Registry: config.Registry(cfg), Params: map[string]map[string]string{}, Languages: s.Languages(), Upstream: s.Lock.Tag, Fallback: missing, Warnings: map[string]map[string][]string{}}
|
||||
result.LanguageNames = languageNames(result.Languages)
|
||||
for id, t := range cfg.Templates {
|
||||
result.Params[id] = config.FlattenParams(t.Params)
|
||||
}
|
||||
@@ -236,6 +238,7 @@ func WriteRenderedJS(result *PreviewResult, output string) error {
|
||||
return err
|
||||
}
|
||||
manifest := map[string]any{"schema": 1, "upstream": result.Upstream, "languages": result.Languages, "themes": result.Themes, "registry": result.Registry, "params": result.Params, "fallback": result.Fallback, "warnings": result.Warnings}
|
||||
manifest["language_names"] = result.LanguageNames
|
||||
manifest["logo"] = "data:image/png;base64," + base64.StdEncoding.EncodeToString(result.Logo)
|
||||
manifest["upstream_commit"] = result.Commit
|
||||
data, err := json.Marshal(manifest)
|
||||
@@ -267,10 +270,8 @@ func PrintDetailedSummary(result *PreviewResult, _ string, _ *config.TemplatesCo
|
||||
logging.Warn("Upstream", "%s", strings.TrimPrefix(warning, "[UPSTREAM-WARN] "))
|
||||
}
|
||||
}
|
||||
if keys := result.Fallback[lang]; len(keys) > 0 {
|
||||
logging.Warn("Locale", "%s -> en-US: %s", lang, strings.Join(keys, ", "))
|
||||
}
|
||||
}
|
||||
logging.WarnMissingTranslations(result.Fallback)
|
||||
}
|
||||
|
||||
func markSafeHTML(data map[string]any) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package preview
|
||||
|
||||
import (
|
||||
"golang.org/x/text/language"
|
||||
"golang.org/x/text/language/display"
|
||||
)
|
||||
|
||||
// languageNames derives native names from CLDR for the generated preview locales.
|
||||
func languageNames(locales []string) map[string]string {
|
||||
names := make(map[string]string, len(locales))
|
||||
for _, locale := range locales {
|
||||
name := locale
|
||||
if tag, err := language.Parse(locale); err == nil {
|
||||
if native := display.Self.Name(tag); native != "" {
|
||||
name = native
|
||||
}
|
||||
}
|
||||
names[locale] = name
|
||||
}
|
||||
return names
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package preview
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea-mail-templates/tools/upstream"
|
||||
)
|
||||
|
||||
func TestNativeLanguageNames(t *testing.T) {
|
||||
snapshot, err := upstream.LoadPinned(filepath.Join("..", ".."))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
locales := snapshot.Languages()
|
||||
names := languageNames(locales)
|
||||
if len(names) != len(locales) {
|
||||
t.Fatal("language names do not match preview locales")
|
||||
}
|
||||
for _, locale := range locales {
|
||||
if names[locale] == "" || names[locale] == locale {
|
||||
t.Errorf("missing native name for %s", locale)
|
||||
}
|
||||
}
|
||||
for locale, want := range map[string]string{"ga-IE": "Gaeilge", "is-IS": "íslenska"} {
|
||||
if names[locale] != want {
|
||||
t.Errorf("%s: got %q, want %q", locale, names[locale], want)
|
||||
}
|
||||
}
|
||||
if got := languageNames([]string{"!invalid"})["!invalid"]; got != "!invalid" {
|
||||
t.Fatalf("invalid locale fallback: %q", got)
|
||||
}
|
||||
}
|
||||
@@ -294,6 +294,7 @@ func TestSubjectSplitAndMissingFixture(t *testing.T) {
|
||||
|
||||
func TestPreviewBundles(t *testing.T) {
|
||||
result := &PreviewResult{Rendered: map[string]map[string]map[string]string{"en-US": {"test": {"activate": "<body>hello</body>"}}, "zh-CN": {"test": {"activate": "<body>你好</body>"}}}, Languages: []string{"en-US", "zh-CN"}, Themes: []string{"test"}, Registry: map[string]map[string]string{}, Upstream: "v28.0.0"}
|
||||
result.LanguageNames = languageNames(result.Languages)
|
||||
output := filepath.Join(t.TempDir(), "rendered.js")
|
||||
if err := WriteRenderedJS(result, output); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -305,6 +306,9 @@ func TestPreviewBundles(t *testing.T) {
|
||||
if bytes.Contains(manifest, []byte("<body>")) {
|
||||
t.Fatal("manifest embeds mail payload")
|
||||
}
|
||||
if !bytes.Contains(manifest, []byte(`"language_names":`)) || !bytes.Contains(manifest, []byte(fmt.Sprintf("%q:%q", "en-US", result.LanguageNames["en-US"]))) {
|
||||
t.Fatal("manifest does not include generated native language names")
|
||||
}
|
||||
for _, lang := range result.Languages {
|
||||
data, err := os.ReadFile(filepath.Join(filepath.Dir(output), "rendered", lang+".js"))
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user