Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
144 lines
4.3 KiB
Go
144 lines
4.3 KiB
Go
package i18ngen
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/egoist/mygo/i18n"
|
|
)
|
|
|
|
// TS returns the TypeScript module of a catalog, which pages import: the
|
|
// messages of every language, which mygo-runtime formats in the
|
|
// language the page shows (its locale()), with a typed function per key,
|
|
// as the Go accessors have.
|
|
//
|
|
// The messages are in the module, not fetched: the frontend's build
|
|
// bundles them, so the first paint is in the right language without a
|
|
// round trip, also in a page served by a dev server or opened in a
|
|
// browser.
|
|
func TS(c *Catalog) ([]byte, error) {
|
|
var b bytes.Buffer
|
|
w := func(format string, args ...any) { fmt.Fprintf(&b, format, args...) }
|
|
w("// Code generated by mygo generate from the catalogs of the app. DO NOT EDIT.\n")
|
|
w("import { createCatalog } from \"mygo-runtime/i18n\";\n\n")
|
|
w("/** The messages of the app in one language. */\nexport interface Messages {\n")
|
|
for _, k := range c.Keys {
|
|
var params []string
|
|
for _, p := range k.Params {
|
|
params = append(params, p.TS+": "+types[p.Type][1])
|
|
}
|
|
w("%s %s(%s): string;\n", tsDoc(k), k.Name, strings.Join(params, ", "))
|
|
}
|
|
w("}\n\n")
|
|
|
|
// What the runtime needs: the parameters of each key in order, the
|
|
// languages each falls back along, and the messages.
|
|
params := map[string][]string{}
|
|
var keys []string
|
|
for _, k := range c.Keys {
|
|
keys = append(keys, k.Name)
|
|
names := []string{}
|
|
for _, p := range k.Params {
|
|
names = append(names, p.Name)
|
|
}
|
|
params[k.Name] = names
|
|
}
|
|
w("const catalog = createCatalog<Messages>({\n")
|
|
w(" source: %s,\n", jsString(c.Source))
|
|
w(" languages: %s,\n", jsList(c.Languages))
|
|
w(" fallbacks: {\n")
|
|
for _, tag := range c.Languages {
|
|
w(" %s: %s,\n", jsString(tag), jsList(Fallbacks(c, tag)))
|
|
}
|
|
w(" },\n params: {\n")
|
|
for _, k := range keys {
|
|
w(" %s: %s,\n", jsString(k), jsList(params[k]))
|
|
}
|
|
w(" },\n messages: {\n")
|
|
for _, tag := range c.Languages {
|
|
f := c.Files[tag]
|
|
w(" %s: {\n", jsString(tag))
|
|
for _, k := range keys {
|
|
if _, ok := f.Parsed[k]; ok {
|
|
w(" %s: %s,\n", jsString(k), jsString(f.Messages[k]))
|
|
}
|
|
}
|
|
w(" },\n")
|
|
}
|
|
w(" },\n});\n\n")
|
|
w(`/**
|
|
* The messages in the language the page shows: the app's, else the
|
|
* browser's. Each call says its message in the language of the moment.
|
|
*/
|
|
export const messages: Messages = catalog.messages();
|
|
|
|
/** Returns the messages in the language of locale, as "de" or "zh-Hant". */
|
|
export function messagesIn(locale: string): Messages {
|
|
return catalog.messagesIn(locale);
|
|
}
|
|
|
|
/** The languages of the catalogs: the source language, then the others. */
|
|
export const languages: readonly string[] = catalog.languages;
|
|
`)
|
|
return b.Bytes(), nil
|
|
}
|
|
|
|
// Fallbacks returns the languages of the catalog a message in tag is
|
|
// looked up in: tag, its CLDR parents the catalog has, then the source
|
|
// language.
|
|
func Fallbacks(c *Catalog, tag string) []string {
|
|
var out []string
|
|
for t := tag; t != ""; t = i18n.Parent(t) {
|
|
if slices.Contains(c.Languages, t) && !slices.Contains(out, t) {
|
|
out = append(out, t)
|
|
}
|
|
}
|
|
if !slices.Contains(out, c.Source) {
|
|
out = append(out, c.Source)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func tsDoc(k *Key) string {
|
|
var b strings.Builder
|
|
b.WriteString(" /**\n")
|
|
for _, line := range strings.Split(strings.TrimSpace(k.Description), "\n") {
|
|
if line = strings.TrimSpace(line); line != "" {
|
|
b.WriteString(" * " + strings.ReplaceAll(line, "*/", "*\\/") + "\n")
|
|
}
|
|
}
|
|
if strings.TrimSpace(k.Description) != "" {
|
|
b.WriteString(" *\n")
|
|
}
|
|
b.WriteString(" * `" + strings.NewReplacer("*/", "*\\/", "`", "\\`", "\n", " ").Replace(k.Message) + "`\n */\n")
|
|
return b.String()
|
|
}
|
|
|
|
// jsList writes a list of strings as a JavaScript array.
|
|
func jsList(list []string) string {
|
|
var items []string
|
|
for _, s := range list {
|
|
items = append(items, jsString(s))
|
|
}
|
|
return "[" + strings.Join(items, ", ") + "]"
|
|
}
|
|
|
|
// jsString writes s as a JavaScript string literal.
|
|
func jsString(s string) string { return jsJSON(s) }
|
|
|
|
// jsJSON writes v as JSON, which is JavaScript (encoding/json escapes the
|
|
// line separators older JavaScript did not allow in strings), without
|
|
// escaping HTML.
|
|
func jsJSON(v any) string {
|
|
var b bytes.Buffer
|
|
enc := json.NewEncoder(&b)
|
|
enc.SetEscapeHTML(false)
|
|
if err := enc.Encode(v); err != nil {
|
|
panic(err)
|
|
}
|
|
return strings.TrimSuffix(b.String(), "\n")
|
|
}
|