- Complete Spanish (neutral Latin American, Paraguay friendly, no inverted marks) and Dutch catalogs, including the Mini App and owner texts - Command menus per language in three scopes (private, group members, group admins); per-chat menus follow a user's /language and a group's /grouplang, from chat and from the Mini App - Bot name, description and short description per language, sent only when the catalog text changes (TELEGRAM_SET_PROFILE) - Dates from the catalog, numbers, sizes and durations per locale - Pseudo-locale en-XA generated from English (I18N_PSEUDO), owners only - Catalog checks: completeness, placeholders, tags, commands, plurals, Telegram length limits, untranslated text, unused and missing IDs - CI workflow; make i18n-check; docs/TRANSLATING.md - Test fix: the queued-download helper could match another chat's status Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
2.8 KiB
Go
88 lines
2.8 KiB
Go
package i18n
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// The pseudo-locale is made from the English catalog when the bundle is
|
|
// loaded with WithPseudo. Every letter gets an accent and every message is
|
|
// wrapped in brackets and made about a third longer. Switch to it and any
|
|
// text that still looks like plain English did not come from the catalog,
|
|
// any text cut off at the bracket is too long for its place, and broken
|
|
// accents point at an encoding problem.
|
|
|
|
var accents = map[rune]rune{
|
|
'a': 'á', 'b': 'ƀ', 'c': 'ç', 'd': 'ð', 'e': 'é', 'f': 'ƒ', 'g': 'ĝ', 'h': 'ĥ', 'i': 'î', 'j': 'ĵ',
|
|
'k': 'ķ', 'l': 'ļ', 'm': 'ḿ', 'n': 'ñ', 'o': 'ö', 'p': 'þ', 'q': 'ǫ', 'r': 'ŕ', 's': 'š', 't': 'ţ',
|
|
'u': 'û', 'v': 'ṽ', 'w': 'ŵ', 'x': 'ẋ', 'y': 'ý', 'z': 'ž',
|
|
'A': 'Å', 'B': 'Ɓ', 'C': 'Ç', 'D': 'Ð', 'E': 'É', 'F': 'Ƒ', 'G': 'Ĝ', 'H': 'Ĥ', 'I': 'Î', 'J': 'Ĵ',
|
|
'K': 'Ķ', 'L': 'Ļ', 'M': 'Ṁ', 'N': 'Ñ', 'O': 'Ö', 'P': 'Þ', 'Q': 'Ǫ', 'R': 'Ŕ', 'S': 'Š', 'T': 'Ţ',
|
|
'U': 'Û', 'V': 'Ṽ', 'W': 'Ŵ', 'X': 'Ẋ', 'Y': 'Ý', 'Z': 'Ž',
|
|
}
|
|
|
|
// keep matches what must survive unchanged: placeholders, HTML tags and
|
|
// entities, bot commands, links, and the list separators of format.months.
|
|
var keep = regexp.MustCompile(`\{\{[^}]*\}\}|</?[a-zA-Z][^>]*>|&[a-z]+;|/[a-z_]+|https?://\S+|@\w+|,`)
|
|
|
|
// Pseudolocalize turns one English message into its pseudo-locale form.
|
|
func Pseudolocalize(s string) string {
|
|
var sb strings.Builder
|
|
letters := 0
|
|
last := 0
|
|
for _, loc := range keep.FindAllStringIndex(s, -1) {
|
|
letters += accentInto(&sb, s[last:loc[0]])
|
|
sb.WriteString(s[loc[0]:loc[1]])
|
|
last = loc[1]
|
|
}
|
|
letters += accentInto(&sb, s[last:])
|
|
body := sb.String()
|
|
// Leading and trailing line breaks stay outside the brackets.
|
|
trimmed := strings.Trim(body, "\n")
|
|
lead := body[:strings.Index(body, trimmed)]
|
|
trail := body[len(lead)+len(trimmed):]
|
|
pad := strings.Repeat("~", (letters+2)/3)
|
|
return lead + "[" + trimmed + " " + pad + "]" + trail
|
|
}
|
|
|
|
func accentInto(sb *strings.Builder, s string) int {
|
|
n := 0
|
|
for _, r := range s {
|
|
if a, ok := accents[r]; ok {
|
|
sb.WriteRune(a)
|
|
n++
|
|
} else {
|
|
sb.WriteRune(r)
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
// pseudoDoc applies Pseudolocalize to every text of a parsed catalog.
|
|
// format.months is a list, not a sentence: its names are accented without
|
|
// brackets so it still splits into twelve.
|
|
func pseudoDoc(in map[string]any, path string) map[string]any {
|
|
out := make(map[string]any, len(in))
|
|
for k, v := range in {
|
|
key := k
|
|
if path != "" {
|
|
key = path + "." + k
|
|
}
|
|
switch x := v.(type) {
|
|
case string:
|
|
if key == "format.months" {
|
|
var sb strings.Builder
|
|
accentInto(&sb, x)
|
|
out[k] = sb.String()
|
|
} else {
|
|
out[k] = Pseudolocalize(x)
|
|
}
|
|
case map[string]any:
|
|
out[k] = pseudoDoc(x, key)
|
|
default:
|
|
out[k] = v
|
|
}
|
|
}
|
|
return out
|
|
}
|