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	}
60	// Fallback to Other or One
61	if m.Other != "" {
62		return m.Other
63	}
64	return m.One
65}
66
67// GetDefault returns the most appropriate default text (tries Other, then One).
68func (m *Message) GetDefault() string {
69	if m.Other != "" {
70		return m.Other
71	}
72	if m.One != "" {
73		return m.One
74	}
75	if m.Zero != "" {
76		return m.Zero
77	}
78	if m.Few != "" {
79		return m.Few
80	}
81	if m.Many != "" {
82		return m.Many
83	}
84	if m.Two != "" {
85		return m.Two
86	}
87	return ""
88}