message.go

 1package i18n
 2
 3// Message represents a translatable message with support for plural forms.
 4type Message struct {
 5	// ID is the unique identifier for this message (e.g., "composer.title")
 6	ID string `json:"id"`
 7
 8	// Description provides context for translators
 9	Description string `json:"description,omitempty"`
10
11	// Hash is an optional content hash for tracking changes
12	Hash string `json:"hash,omitempty"`
13
14	// Zero form is used when count is exactly 0 (optional)
15	Zero string `json:"zero,omitempty"`
16
17	// One form is used for singular (count == 1)
18	One string `json:"one,omitempty"`
19
20	// Two form is used for dual (count == 2) in some languages
21	Two string `json:"two,omitempty"`
22
23	// Few form is used for small counts in some languages (e.g., Polish)
24	Few string `json:"few,omitempty"`
25
26	// Many form is used for larger counts in some languages (e.g., Russian)
27	Many string `json:"many,omitempty"`
28
29	// Other is the default form used when no specific plural form matches
30	Other string `json:"other,omitempty"`
31}
32
33// MessageMap maps message IDs to Message structs.
34type MessageMap map[string]*Message
35
36// GetText returns the appropriate text for the given plural form.
37func (m *Message) GetText(form PluralForm) string {
38	switch form {
39	case Zero:
40		if m.Zero != "" {
41			return m.Zero
42		}
43	case One:
44		if m.One != "" {
45			return m.One
46		}
47	case Two:
48		if m.Two != "" {
49			return m.Two
50		}
51	case Few:
52		if m.Few != "" {
53			return m.Few
54		}
55	case Many:
56		if m.Many != "" {
57			return m.Many
58		}
59	case Other:
60		if m.Other != "" {
61			return m.Other
62		}
63	}
64	// Fallback to Other or One
65	if m.Other != "" {
66		return m.Other
67	}
68	return m.One
69}
70
71// GetDefault returns the most appropriate default text (tries Other, then One).
72func (m *Message) GetDefault() string {
73	if m.Other != "" {
74		return m.Other
75	}
76	if m.One != "" {
77		return m.One
78	}
79	if m.Zero != "" {
80		return m.Zero
81	}
82	if m.Few != "" {
83		return m.Few
84	}
85	if m.Many != "" {
86		return m.Many
87	}
88	if m.Two != "" {
89		return m.Two
90	}
91	return ""
92}