Skip to content

Commit 3c15077

Browse files
committed
chore: initial commit
0 parents  commit 3c15077

File tree

7 files changed

+202
-0
lines changed

7 files changed

+202
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### JetBrains
2+
.idea/
3+
*.iml

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module hypera.dev/axolotl-lang/v2
2+
3+
go 1.22.2
4+
5+
require (
6+
github.com/BurntSushi/toml v1.3.2
7+
github.com/bwmarrin/discordgo v0.28.1
8+
github.com/nicksnyder/go-i18n/v2 v2.4.0
9+
golang.org/x/text v0.14.0
10+
)
11+
12+
require (
13+
github.com/gorilla/websocket v1.4.2 // indirect
14+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
15+
golang.org/x/sys v0.5.0 // indirect
16+
)

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
2+
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
3+
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
4+
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
5+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
6+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7+
github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC+diLMM=
8+
github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4=
9+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
10+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
11+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
12+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
13+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
14+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
16+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
17+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
18+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
19+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
20+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
21+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

lang.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package lang
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
7+
"github.com/BurntSushi/toml"
8+
"github.com/bwmarrin/discordgo"
9+
"github.com/nicksnyder/go-i18n/v2/i18n"
10+
"golang.org/x/text/language"
11+
)
12+
13+
//go:embed locales/*.toml
14+
var localeFS embed.FS
15+
16+
type Data map[string]any
17+
18+
var (
19+
supportedLocales = []discordgo.Locale{
20+
discordgo.EnglishGB,
21+
discordgo.EnglishUS,
22+
discordgo.Japanese,
23+
}
24+
25+
bundle *i18n.Bundle
26+
locales = make(map[discordgo.Locale]*Localizer)
27+
)
28+
29+
func LoadLocales() error {
30+
bundle = i18n.NewBundle(language.MustParse(string(supportedLocales[0])))
31+
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
32+
33+
for _, locale := range supportedLocales {
34+
_, err := bundle.LoadMessageFileFS(localeFS, fmt.Sprintf("locales/%s.toml", string(locale)))
35+
if err != nil {
36+
return fmt.Errorf("load locale file for %q: %w", locale, err)
37+
}
38+
locales[locale] = newLocalizer(locale)
39+
}
40+
41+
return nil
42+
}
43+
44+
func GetLocalizer(locale discordgo.Locale) (*Localizer, bool) {
45+
if l, ok := locales[locale]; ok {
46+
return l, true
47+
}
48+
49+
// Use default locale.
50+
return locales[supportedLocales[0]], false
51+
}
52+
53+
func Localize(locale discordgo.Locale, id string) string {
54+
l, _ := GetLocalizer(locale)
55+
return l.Localize(id)
56+
}
57+
58+
func LocalizeWithData(locale discordgo.Locale, id string, data Data) string {
59+
l, _ := GetLocalizer(locale)
60+
return l.LocalizeWithData(id, data)
61+
}
62+
63+
func LocalizePlural(locale discordgo.Locale, id string, count any) string {
64+
l, _ := GetLocalizer(locale)
65+
return l.LocalizePlural(id, count)
66+
}
67+
68+
func LocalizePluralWithData(locale discordgo.Locale, id string, count any, data Data) string {
69+
l, _ := GetLocalizer(locale)
70+
return l.LocalizePluralWithData(id, count, data)
71+
}
72+
73+
type Localizer struct {
74+
*i18n.Localizer
75+
}
76+
77+
func newLocalizer(locale discordgo.Locale) *Localizer {
78+
return &Localizer{Localizer: i18n.NewLocalizer(bundle, string(locale))}
79+
}
80+
81+
func (l *Localizer) Localize(id string) string {
82+
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
83+
MessageID: id,
84+
})
85+
if s == "" && err != nil {
86+
return id
87+
}
88+
return s
89+
}
90+
91+
func (l *Localizer) LocalizeWithData(id string, data Data) string {
92+
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
93+
MessageID: id,
94+
TemplateData: data,
95+
})
96+
if s == "" && err != nil {
97+
return id
98+
}
99+
return s
100+
}
101+
102+
func (l *Localizer) LocalizePlural(id string, count any) string {
103+
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
104+
MessageID: id,
105+
PluralCount: count,
106+
})
107+
if s == "" && err != nil {
108+
return id
109+
}
110+
return s
111+
}
112+
113+
func (l *Localizer) LocalizePluralWithData(id string, count any, data Data) string {
114+
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
115+
MessageID: id,
116+
TemplateData: data,
117+
PluralCount: count,
118+
})
119+
if s == "" && err != nil {
120+
return id
121+
}
122+
return s
123+
}

locales/en-GB.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[generic]
2+
error = "Oops!"
3+
error-description = """
4+
An error occurred. Please try again later.
5+
If the issue persists, please report this to a staff member.
6+
"""
7+
8+
[ticket]
9+
creation-title = "Ticket creation"
10+
channel-created = "Please follow the steps in {{.channel}} to continue."
11+
product-select = "Please select the product you wish to receive support for."
12+
creation-welcome = """
13+
Thank you for creating a ticket. A support team member will be available to assist you shortly. \
14+
While you wait, please provide as much information as possible.\
15+
"""

locales/en-US.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Messages should be placed in en-GB.
2+
# If a message contains a word/spelling that is specific to en-GB, it should be duplicated here and changed.

locales/ja.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# This file was translated by someone who knows very basic Japanese, with the
3+
# help of a dictionary (jisho.org), a translation service and a lot of research.
4+
#
5+
# This will almost definately contain mistakes, please fix any mistakes you find!
6+
#
7+
8+
[generic]
9+
error = "おっと"
10+
error-description = """
11+
エラーが発生しました。後でもう一度お試しください。
12+
問題が解決しない場合は、スタッフまでお知らせください。
13+
"""
14+
15+
[ticket]
16+
creation-title = "チケット作成"
17+
channel-created = "{{.channel}}の手順でお進みください。"
18+
product-select = "サポートを受けたい製品を選択してください。"
19+
creation-welcome = """
20+
チケットを作成していただきありがとうございます。まもなくサポートチームメンバーが対応させていただきます。\
21+
お待ちいただく間、できるだけ多くの情報をご提供ください。\
22+
"""

0 commit comments

Comments
 (0)