81 lines
2.9 KiB
Go
81 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.yuntuo.hk/k2safe/go-omni/i18n"
|
|
)
|
|
|
|
func TestI18nCommandsAreReadOnlyAndCheckMissing(t *testing.T) {
|
|
dir := t.TempDir()
|
|
files := map[string]string{
|
|
"config.json": `{"sourceLocale":"en","defaultLocale":"en","locales":["en","fr"]}`,
|
|
"en.json": `{"version":1,"namespace":"ui","locale":"en","messages":{"hello":{"params":{"type":"string"},"value":{"text":"Hello {type}"}}}}`,
|
|
"fr.json": `{"version":1,"namespace":"ui","locale":"fr","messages":{"hello":{"value":{"text":"Bonjour {type}"}}}}`,
|
|
"use.go": "package demo\nimport words \"git.yuntuo.hk/k2safe/go-omni/i18n\"\nvar key=words.Ref{Namespace:\"ui\",Key:\"hello\"}\n",
|
|
}
|
|
for path, data := range files {
|
|
if err := os.WriteFile(filepath.Join(dir, path), []byte(data), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
base := []string{"--config", filepath.Join(dir, "config.json"), "--resource", filepath.Join(dir, "en.json"), "--resource", filepath.Join(dir, "fr.json")}
|
|
var out bytes.Buffer
|
|
args := append([]string{"check"}, base...)
|
|
args = append(args, "--go", filepath.Join(dir, "use.go"))
|
|
if err := runI18nTo(args, &out); err != nil || strings.TrimSpace(out.String()) != "[]" {
|
|
t.Fatal(err, out.String())
|
|
}
|
|
out.Reset()
|
|
args = append([]string{"export"}, base...)
|
|
args = append(args, "--locale", "fr", "--namespace", "ui")
|
|
if err := runI18nTo(args, &out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var pack i18n.Pack
|
|
if err := json.Unmarshal(out.Bytes(), &pack); err != nil || pack.Locale != "fr" || len(pack.Documents) != 2 {
|
|
t.Fatal(err, pack)
|
|
}
|
|
out.Reset()
|
|
args = append([]string{"types"}, base...)
|
|
args = append(args, "--namespace", "ui")
|
|
if err := runI18nTo(args, &out); err != nil || !strings.Contains(out.String(), `"type": string`) {
|
|
t.Fatal(err, out.String())
|
|
}
|
|
out.Reset()
|
|
if err := runI18nTo([]string{"extract", "--go", filepath.Join(dir, "use.go")}, &out); err != nil || !strings.Contains(out.String(), `"key": "hello"`) {
|
|
t.Fatal(err, out.String())
|
|
}
|
|
for path, want := range files {
|
|
got, _ := os.ReadFile(filepath.Join(dir, path))
|
|
if string(got) != want {
|
|
t.Fatal("command mutated", path)
|
|
}
|
|
}
|
|
out.Reset()
|
|
args = append([]string{"check"}, base[:4]...)
|
|
if err := runI18nTo(args, &out); err == nil || !strings.Contains(out.String(), "direct translation missing") {
|
|
t.Fatal(err, out.String())
|
|
}
|
|
}
|
|
|
|
func TestI18nCatalogExtractionPreservesLiteralBraces(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "errors.json")
|
|
if err := os.WriteFile(path, []byte(`{"definitions":{"FIXED":{"code":1,"message":"${NAME}"}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out bytes.Buffer
|
|
if err := runI18nTo([]string{"extract", "--catalog", path, "--namespace", "errors", "--locale", "en"}, &out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var d i18n.Document
|
|
if err := i18n.DecodeJSON(out.Bytes(), &d); err != nil || !d.Messages["FIXED"].Value.Literal || d.Messages["FIXED"].Value.Text != "${NAME}" {
|
|
t.Fatal(err, d)
|
|
}
|
|
}
|