-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.go
More file actions
210 lines (184 loc) · 5.66 KB
/
translate.go
File metadata and controls
210 lines (184 loc) · 5.66 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Package i18n provides internationalization and localization functionality.
//
// Translation API Functions:
// - T(key, args...) - Translate by key with placeholder substitution
// - F(format, args...) - Translate by format string (auto-generates key from format)
// - S(text) - Translate static text (auto-generates key from text)
// - P(key, count) - Pluralization support
// - R(locale, format) - Direct translation (no function wrapping)
//
// Example usage:
//
// greeting := i18n.T("hello_world")
// msg := i18n.F("Welcome %s!", "John")
// title := i18n.S("Dashboard")
package i18n
import (
"fmt"
"strings"
)
// TranslatedFunc returns a localized string when called with a locale.
// This allows you to prepare a translation function and call it later with different locales.
type TranslatedFunc func(locale string) string
// T translates by exact key with placeholder substitution.
// Use this when you have predefined translation keys in your dictionary files.
// Placeholders are numbered: {0}, {1}, {2}, etc.
//
// Example:
//
// fn := i18n.T("welcome_user", "John")
// fmt.Println(fn("en")) // "Welcome John!"
// fmt.Println(fn("fr")) // "Bienvenue John!"
//
// Dictionary should contain:
//
// "welcome_user": "Welcome {0}!"
func T(key string, args ...any) TranslatedFunc {
return func(locale string) string {
dict := GetDictionary(locale)
template := key
if dict != nil {
if tr := dict.Get(key); tr != "" && tr != key {
template = tr
}
} else if defaultDict := GetDictionary(DefaultLanguage()); defaultDict != nil {
if tr := defaultDict.Get(key); tr != "" && tr != key {
template = tr
}
}
// Replace placeholders {0}, {1}, {2}, etc.
for i, arg := range args {
placeholder := fmt.Sprintf("{%d}", i)
template = strings.ReplaceAll(template, placeholder, fmt.Sprint(arg))
}
return template
}
}
// F translates by format string with auto-generated key.
// This automatically generates a translation key from the format string and normalizes placeholders.
// Use this when you want to use the English text as the source and auto-generate keys.
//
// Example:
//
// fn := i18n.F("Hello %s, you have %d messages", "John", 5)
// fmt.Println(fn("en")) // "Hello John, you have 5 messages"
// fmt.Println(fn("fr")) // "Bonjour John, vous avez 5 messages"
//
// Auto-generated key: "hello-1-you-have-2-messages"
// Dictionary should contain:
//
// "hello-1-you-have-2-messages": "Bonjour {0}, vous avez {1} messages"
func F(format string, args ...any) TranslatedFunc {
key := slugify(format)
normalizedTemplate, _ := normalize(format)
return func(locale string) string {
dict := GetDictionary(locale)
template := normalizedTemplate
if dict != nil {
if tr := dict.Get(key); tr != "" && tr != key {
template = tr
}
} else if defaultDict := GetDictionary(DefaultLanguage()); defaultDict != nil {
if tr := defaultDict.Get(key); tr != "" && tr != key {
template = tr
}
}
// Replace placeholders {0}, {1}, {2}, etc.
for i, arg := range args {
placeholder := fmt.Sprintf("{%d}", i)
template = strings.ReplaceAll(template, placeholder, fmt.Sprint(arg))
}
return template
}
}
// S translates static text with auto-generated key.
// Use this for simple static strings without placeholders.
//
// Example:
//
// fn := i18n.S("Dashboard")
// fmt.Println(fn("en")) // "Dashboard"
// fmt.Println(fn("fr")) // "Tableau de bord"
//
// Auto-generated key: "dashboard"
// Dictionary should contain:
//
// "dashboard": "Tableau de bord"
func S(text string) TranslatedFunc {
key := slugify(text)
return func(locale string) string {
dict := GetDictionary(locale)
if dict != nil {
if tr := dict.Get(key); tr != "" && tr != key {
return tr
}
}
if defaultDict := GetDictionary(DefaultLanguage()); defaultDict != nil {
if tr := defaultDict.Get(key); tr != "" && tr != key {
return tr
}
}
return text
}
}
// P handles pluralization for a given key and count.
// Supports ICU-style plural forms: zero, one, two, few, many, other.
//
// Example:
//
// fn := i18n.P("item_count", 5)
// fmt.Println(fn("en")) // "5 items"
//
// Dictionary should contain:
//
// "item_count": "{count, plural, zero {no items} one {# item} other {# items}}"
func P(key string, count int) TranslatedFunc {
return func(locale string) string {
dict := GetDictionary(locale)
template := key
if dict != nil {
template = dict.Get(key)
} else if defaultDict := GetDictionary(DefaultLanguage()); defaultDict != nil {
template = defaultDict.Get(key)
}
// Handle ICU-style plural syntax
if strings.Contains(template, "{count, plural") {
// Determine the appropriate plural form for the locale
form := determinePluralForm(locale, count)
// Extract the appropriate plural form from template
if result := extractPluralForm(template, form, count); result != "" {
return result
}
// Fallback to "other" if specific form not found
if form != "other" {
if result := extractPluralForm(template, "other", count); result != "" {
return result
}
}
}
// Fallback: simple string substitution
return strings.ReplaceAll(template, "{count}", fmt.Sprint(count))
}
}
// R performs direct translation without function wrapping.
// Use this when you want immediate translation without creating a TranslatedFunc.
//
// Example:
//
// text := i18n.R("en", "Dashboard")
// fmt.Println(text) // "Dashboard"
func R(locale, text string) string {
key := slugify(text)
dict := GetDictionary(locale)
if dict != nil {
if tr := dict.Get(key); tr != "" && tr != key {
return tr
}
}
if defaultDict := GetDictionary(DefaultLanguage()); defaultDict != nil {
if tr := defaultDict.Get(key); tr != "" && tr != key {
return tr
}
}
return text
}