-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
70 lines (70 loc) · 2.31 KB
/
Copy pathdoc.go
File metadata and controls
70 lines (70 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package i18n provides internationalization support for the gogpu/ui toolkit.
//
// The i18n package enables string localization, locale management, plural rules,
// and text direction (LTR/RTL) detection. It integrates with the reactive state
// system via [state.Signal] for dynamic locale switching.
//
// # Core Types
//
// - [Locale] — represents a language/region combination (e.g., "en-US", "ru-RU")
// - [Bundle] — holds message translations for a single locale
// - [Translator] — resolves messages with locale fallback chains
// - [Direction] — text direction (LTR or RTL)
// - [PluralForms] — plural category forms (zero, one, two, few, many, other)
//
// # Basic Usage
//
// // Create translator with English fallback
// t := i18n.NewTranslator(i18n.NewLocale("en", "US"))
//
// // Add English bundle
// en := i18n.NewBundle(i18n.NewLocale("en", "US"))
// en.Set("greeting", "Hello!")
// en.SetPlural("items", i18n.PluralForms{
// One: "%d item",
// Other: "%d items",
// })
// t.AddBundle(en)
//
// // Add Russian bundle
// ru := i18n.NewBundle(i18n.NewLocale("ru", "RU"))
// ru.Set("greeting", "Привет!")
// t.AddBundle(ru)
//
// // Resolve messages
// t.T("greeting") // "Hello!"
// t.SetLocale(i18n.NewLocale("ru", "RU"))
// t.T("greeting") // "Привет!"
//
// # Plural Support
//
// Plural rules follow CLDR categories (zero, one, two, few, many, other).
// Built-in rules are provided for English, Russian, and Arabic:
//
// t.Tp("items", 1) // "1 item" (one)
// t.Tp("items", 5) // "5 items" (other)
// t.Tpf("items", 3, 3) // "3 items" (other, formatted)
//
// # Reactive Locale Switching
//
// The Translator exposes a [state.ReadonlySignal] for the current locale,
// enabling reactive UI updates when the locale changes:
//
// localeSig := t.LocaleSignal()
// state.Effect(func() {
// fmt.Println("Locale changed to:", localeSig.Get())
// })
//
// # Text Direction
//
// RTL detection is built in for Arabic, Hebrew, Persian, and Urdu:
//
// locale := i18n.NewLocale("ar", "SA")
// locale.Direction() // i18n.RTL
//
// # Thread Safety
//
// [Translator] is safe for concurrent access from multiple goroutines.
// All methods use a sync.RWMutex internally. [Bundle] is NOT thread-safe;
// it should be fully populated before being added to a Translator.
package i18n