add description to translations

This commit is contained in:
Eduard Gert
2026-06-09 11:33:54 +02:00
parent aaa5dbb606
commit 32be58cb24
8 changed files with 4243 additions and 1370 deletions
+20 -2
View File
@@ -33,6 +33,12 @@ const (
// commonBundleFile is the per-language translation bundle. Single
// namespace for now ("common") — split later if the key set grows
// enough to warrant per-screen bundles.
//
// Shape is Chrome-extension JSON (each key maps to an object with a
// "message" and an optional "description") so Crowdin reads the
// description as translator context straight from the source file.
// Only the source bundle (en) needs descriptions; target bundles carry
// just "message". loadBundle flattens both back to key->message.
commonBundleFile = "common.json"
)
@@ -195,15 +201,27 @@ func loadLocaleIndex(localesFS fs.FS) (*localeIndex, error) {
return &idx, nil
}
// bundleEntry is the on-disk shape of one translation key: a Chrome-JSON
// object carrying the translatable "message" plus an optional translator
// "description" (consumed by Crowdin, ignored at runtime).
type bundleEntry struct {
Message string `json:"message"`
Description string `json:"description,omitempty"`
}
func loadBundle(localesFS fs.FS, code LanguageCode) (map[string]string, error) {
p := path.Join(string(code), commonBundleFile)
data, err := fs.ReadFile(localesFS, p)
if err != nil {
return nil, err
}
var bundle map[string]string
if err := json.Unmarshal(data, &bundle); err != nil {
var entries map[string]bundleEntry
if err := json.Unmarshal(data, &entries); err != nil {
return nil, fmt.Errorf("parse %s: %w", p, err)
}
bundle := make(map[string]string, len(entries))
for k, e := range entries {
bundle[k] = e.Message
}
return bundle, nil
}
+7 -7
View File
@@ -23,13 +23,13 @@ func fakeLocales() fstest.MapFS {
]
}`)},
"en/common.json": {Data: []byte(`{
"tray.menu.connect": "Connect",
"tray.menu.installVersion": "Install version {version}",
"notify.update.body": "NetBird {version} is available."
"tray.menu.connect": {"message": "Connect", "description": "Tray menu item"},
"tray.menu.installVersion": {"message": "Install version {version}"},
"notify.update.body": {"message": "NetBird {version} is available."}
}`)},
"hu/common.json": {Data: []byte(`{
"tray.menu.connect": "Csatlakozás",
"tray.menu.installVersion": "{version} telepítése"
"tray.menu.connect": {"message": "Csatlakozás"},
"tray.menu.installVersion": {"message": "{version} telepítése"}
}`)},
}
}
@@ -118,7 +118,7 @@ func TestBundle_MissingDefaultBundleFails(t *testing.T) {
// English locale.
fs := fstest.MapFS{
"_index.json": {Data: []byte(`{"languages":[{"code":"hu","displayName":"Magyar","englishName":"Hungarian"}]}`)},
"hu/common.json": {Data: []byte(`{"k":"v"}`)},
"hu/common.json": {Data: []byte(`{"k":{"message":"v"}}`)},
}
_, err := NewBundle(fs)
require.Error(t, err)
@@ -134,7 +134,7 @@ func TestBundle_MissingBundleSkipsLanguage(t *testing.T) {
{"code":"en","displayName":"English","englishName":"English"},
{"code":"de","displayName":"Deutsch","englishName":"German"}
]}`)},
"en/common.json": {Data: []byte(`{"k":"v"}`)},
"en/common.json": {Data: []byte(`{"k":{"message":"v"}}`)},
}
b, err := NewBundle(fs)
require.NoError(t, err)
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff