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.
243 lines
7.4 KiB
Go
243 lines
7.4 KiB
Go
// Package i18n reads the languages people read and picks, of those an app
|
||
// is written in, the one that suits them best, and says an app's messages
|
||
// in it.
|
||
//
|
||
// A language is a BCP 47 tag, as "en", "en-GB", "zh-Hant" or "sr-Latn-RS".
|
||
// Normalize reads the other forms desktops report, as the POSIX locales of
|
||
// Linux ("zh_CN.UTF-8", "sr_RS@latin"). Match compares tags by the
|
||
// distances of the Unicode CLDR, through golang.org/x/text/language, so
|
||
// that zh-TW gets zh-Hant and not zh-Hans, es-MX gets es-419 before es, and
|
||
// nb gets no.
|
||
//
|
||
// An app's messages are in catalogs (Catalog), a JSON file per language,
|
||
// of messages of ICU MessageFormat (Message), whose plural forms follow
|
||
// the plural rules of CLDR (PluralCategory). `mygo generate` writes typed
|
||
// Go and TypeScript functions for them, which say them in the language
|
||
// the app shows (Locale). The data of CLDR is generated from a pinned
|
||
// release (CLDRVersion) by `go generate`.
|
||
//
|
||
// FormatNumber and FormatDate write numbers, compact ones too ("1.2K"),
|
||
// amounts of money ("$1,842.00", "1.842,00 €"), dates and times in the
|
||
// way of a locale, and FormatDateRange ranges of
|
||
// them ("Jan 5 – 9, 2026"), and FormatRelativeTime relative times
|
||
// ("3 minutes ago", "tomorrow"), by CLDR's data of the locales of its modern
|
||
// coverage, as ICU and Intl write them; Months, Weekdays and DayPeriods
|
||
// give their names, FirstWeekday and HourCycle a region's week and clock,
|
||
// and Autonym the name of a language in itself. The data is linked into
|
||
// an app only when it formats something.
|
||
package i18n
|
||
|
||
import (
|
||
"encoding/binary"
|
||
"strings"
|
||
"sync"
|
||
"sync/atomic"
|
||
|
||
"golang.org/x/text/language"
|
||
)
|
||
|
||
// Normalize returns locale as a BCP 47 tag in its canonical form, or ""
|
||
// when it names no language, as "C", "POSIX" or "". It reads BCP 47 tags in
|
||
// any case ("en-us" is "en-US"), POSIX locales ("zh_CN.UTF-8" is "zh-CN",
|
||
// "sr_RS@latin" is "sr-Latn-RS") and the old codes of a language ("iw" is
|
||
// "he", "in" is "id").
|
||
func Normalize(locale string) string {
|
||
if tag, ok := normalized.Load(locale); ok {
|
||
return tag.(string)
|
||
}
|
||
var tag string
|
||
if t, ok := parse(locale); ok {
|
||
tag = t.String()
|
||
}
|
||
if cached.Add(1) <= maxNormalized {
|
||
normalized.Store(locale, tag)
|
||
}
|
||
return tag
|
||
}
|
||
|
||
// normalized caches Normalize, which formatting calls for every message,
|
||
// for the first maxNormalized locales asked for: an app normalizing what
|
||
// pages or users send would keep every string otherwise.
|
||
var (
|
||
normalized sync.Map // string -> string
|
||
cached atomic.Int64
|
||
)
|
||
|
||
const maxNormalized = 256
|
||
|
||
// parse reads locale as Normalize does.
|
||
func parse(locale string) (language.Tag, bool) {
|
||
locale = strings.TrimSpace(locale)
|
||
// A POSIX locale: language[_territory][.codeset][@modifier].
|
||
var modifier string
|
||
if i := strings.IndexByte(locale, '@'); i >= 0 {
|
||
locale, modifier = locale[:i], locale[i+1:]
|
||
}
|
||
if i := strings.IndexByte(locale, '.'); i >= 0 {
|
||
locale = locale[:i]
|
||
}
|
||
switch locale {
|
||
case "", "C", "POSIX":
|
||
return language.Und, false
|
||
}
|
||
t, err := language.Parse(strings.ReplaceAll(locale, "_", "-"))
|
||
if err != nil || t == language.Und {
|
||
return language.Und, false
|
||
}
|
||
if script, ok := modifierScripts[strings.ToLower(modifier)]; ok {
|
||
if s, err := language.ParseScript(script); err == nil {
|
||
if b, conf := t.Base(); conf != language.No {
|
||
r, _ := t.Region()
|
||
if with, err := language.Compose(b, s, r); err == nil {
|
||
t = with
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return t, true
|
||
}
|
||
|
||
// modifierScripts are the scripts the modifiers of POSIX locales name.
|
||
var modifierScripts = map[string]string{
|
||
"latin": "Latn",
|
||
"cyrillic": "Cyrl",
|
||
"arabic": "Arab",
|
||
"devanagari": "Deva",
|
||
"hebrew": "Hebr",
|
||
}
|
||
|
||
// Match returns the language of supported that best suits a reader of
|
||
// preferred, the languages they read, most preferred first, or "" when
|
||
// none suits them. It returns the tag as supported spells it.
|
||
//
|
||
// A supported language suits a reader when CLDR finds it close to one they
|
||
// read: another region of the same language and script, as en-GB for
|
||
// en-AU, pt-BR for pt-PT or es-ES for es-MX, or the language without a
|
||
// region. A language written in another script does not suit them: zh-Hans
|
||
// is no match for zh-TW, nor sr-Latn for sr-Cyrl. A match of a language
|
||
// they prefer more wins over a closer match of one they prefer less: fr
|
||
// for fr-CA wins over de for de.
|
||
func Match(preferred, supported []string) string {
|
||
// The key: each tag after its length, preferred then supported.
|
||
var buf [256]byte
|
||
key := buf[:0]
|
||
for _, list := range [2][]string{preferred, supported} {
|
||
key = binary.AppendUvarint(key, uint64(len(list)))
|
||
for _, t := range list {
|
||
key = binary.AppendUvarint(key, uint64(len(t)))
|
||
key = append(key, t...)
|
||
}
|
||
}
|
||
matched.mu.Lock()
|
||
m, ok := matched.m[string(key)]
|
||
matched.mu.Unlock()
|
||
if ok {
|
||
return m
|
||
}
|
||
m = match(preferred, supported)
|
||
matched.mu.Lock()
|
||
if matched.m == nil {
|
||
matched.m = map[string]string{}
|
||
}
|
||
if len(matched.m) < maxMatched {
|
||
matched.m[string(key)] = m
|
||
}
|
||
matched.mu.Unlock()
|
||
return m
|
||
}
|
||
|
||
// matched caches Match for the first maxMatched lists asked for. Parsing
|
||
// the tags and building a matcher cost more than saying a message, and an
|
||
// app works the language it shows out of the same lists for every message
|
||
// it says before it runs, as in its tests.
|
||
var matched struct {
|
||
mu sync.Mutex
|
||
m map[string]string
|
||
}
|
||
|
||
const maxMatched = 256
|
||
|
||
// match is Match, uncached.
|
||
func match(preferred, supported []string) string {
|
||
var tags []language.Tag
|
||
var kept []string
|
||
for _, s := range supported {
|
||
if t, ok := parse(s); ok {
|
||
tags = append(tags, t)
|
||
kept = append(kept, s)
|
||
}
|
||
}
|
||
if len(tags) == 0 {
|
||
return ""
|
||
}
|
||
var want []language.Tag
|
||
for _, p := range preferred {
|
||
if t, ok := parse(p); ok {
|
||
want = append(want, t)
|
||
}
|
||
}
|
||
if len(want) == 0 {
|
||
return ""
|
||
}
|
||
_, i, conf := language.NewMatcher(tags).Match(want...)
|
||
if conf < language.High {
|
||
return ""
|
||
}
|
||
return kept[i]
|
||
}
|
||
|
||
// Dir is the direction a language is written in.
|
||
type Dir uint8
|
||
|
||
const (
|
||
LeftToRight Dir = iota
|
||
RightToLeft
|
||
)
|
||
|
||
func (d Dir) String() string {
|
||
if d == RightToLeft {
|
||
return "rtl"
|
||
}
|
||
return "ltr"
|
||
}
|
||
|
||
// Direction returns the direction tag is written in, that of its script:
|
||
// right to left for Arabic, Hebrew and the other scripts written so, where
|
||
// a tag names no script, the one its language is most likely written in
|
||
// (ar is Arab, ar-XB, the pseudo locale, too). A tag that names no language
|
||
// is left to right.
|
||
func Direction(tag string) Dir {
|
||
t, ok := parse(tag)
|
||
if !ok {
|
||
return LeftToRight
|
||
}
|
||
s, _ := t.Script()
|
||
if rtlScripts[s.String()] {
|
||
return RightToLeft
|
||
}
|
||
return LeftToRight
|
||
}
|
||
|
||
// rtlScripts are the scripts of Unicode written right to left.
|
||
var rtlScripts = map[string]bool{
|
||
"Adlm": true, "Arab": true, "Aran": true, "Armi": true, "Avst": true,
|
||
"Chrs": true, "Cprt": true, "Elym": true, "Hatr": true, "Hebr": true,
|
||
"Hung": true, "Khar": true, "Lydi": true, "Mand": true, "Mani": true,
|
||
"Mend": true, "Merc": true, "Mero": true, "Narb": true, "Nbat": true,
|
||
"Nkoo": true, "Orkh": true, "Ougr": true, "Palm": true, "Phli": true,
|
||
"Phlp": true, "Phnx": true, "Prti": true, "Rohg": true, "Samr": true,
|
||
"Sarb": true, "Sogd": true, "Sogo": true, "Syrc": true, "Thaa": true,
|
||
"Yezi": true,
|
||
}
|
||
|
||
// Base returns the language of tag, without its script and region: "zh"
|
||
// for "zh-Hant-TW", "he" for "iw". It returns "" when tag names no language.
|
||
func Base(tag string) string {
|
||
t, ok := parse(tag)
|
||
if !ok {
|
||
return ""
|
||
}
|
||
b, _ := t.Base()
|
||
return b.String()
|
||
}
|