-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
102 lines (83 loc) · 2.67 KB
/
main.go
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
package main
import (
"fmt"
"github.com/eduardolat/goeasyi18n"
)
/*
Gender Handling: Why is it useful?
Let's say your app has a feature that says, "John liked your post" or "Emily liked your post."
In some languages, the verb "liked" might change based on the gender of the person who
liked the post.
Example:
- English: "He liked your post" vs "She liked your post"
- Spanish: "A él le gustó tu publicación" vs "A ella le gustó tu publicación"
With gender-specific translations, you can easily adapt the sentence structure to fit the
gender, making your app more linguistically accurate and inclusive.
No need for messy if-else statements to handle gender variations. Just set it up once, and
the library takes care of the rest!
*/
func main() {
// 1. Create a new i18n instance
i18n := goeasyi18n.NewI18n()
// 2. Create your translations
// If something goes wrong, the default value is used
// The gender keys are Male, Female and NonBinary
enTranslations := goeasyi18n.TranslateStrings{
{
Key: "friend_emails",
Default: "Hello, your friend have emails",
Male: "Hello, he has emails",
Female: "Hello, she has emails",
NonBinary: "Hello, your friend have emails",
},
}
esTranslations := goeasyi18n.TranslateStrings{
{
Key: "friend_emails",
Default: "Hola, tu amigo tiene correos",
Male: "Hola, él tiene correos",
Female: "Hola, ella tiene correos",
NonBinary: "Hola, tu amigue tiene correos",
},
}
// 3. Add your languages with their translations
i18n.AddLanguage("en", enTranslations)
i18n.AddLanguage("es", esTranslations)
// 4. Create the Options
// The Gender field is a *string that contains the gender to use
// Here you can use male, female, nonbinary or non-binary
maleText := "male"
femaleText := "female"
nonbinaryText := "nonbinary"
maleOptions := goeasyi18n.Options{
Gender: &maleText,
}
femaleOptions := goeasyi18n.Options{
Gender: &femaleText,
}
nonbinaryOptions := goeasyi18n.Options{
Gender: &nonbinaryText,
}
// 5. You are done! 🎉 Just get that translations!
ten1 := i18n.T("en", "friend_emails", maleOptions)
ten2 := i18n.T("en", "friend_emails", femaleOptions)
ten3 := i18n.T("en", "friend_emails", nonbinaryOptions)
tes1 := i18n.T("es", "friend_emails", maleOptions)
tes2 := i18n.T("es", "friend_emails", femaleOptions)
tes3 := i18n.T("es", "friend_emails", nonbinaryOptions)
fmt.Println(ten1)
fmt.Println(ten2)
fmt.Println(ten3)
fmt.Println(tes1)
fmt.Println(tes2)
fmt.Println(tes3)
/*
Prints:
Hello, he has emails
Hello, she has emails
Hello, your friend have emails
Hola, él tiene correos
Hola, ella tiene correos
Hola, tu amigue tiene correos
*/
}