Skip to content

Commit

Permalink
feat: add command.info translations
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing committed May 1, 2024
1 parent 1c328fa commit 97aa9ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,46 +73,47 @@ func Bundle() *i18n.Bundle {
return bundle
}

func GetLocalizer(locale discordgo.Locale) (*Localizer, bool) {
func GetLocalizer(locale discordgo.Locale) *Localizer {
if l, ok := locales[locale]; ok {
return l, true
return l
}

// Use default locale.
return locales[supportedLocales[0]], false
return locales[supportedLocales[0]]
}

func Localize(locale discordgo.Locale, id string) string {
l, _ := GetLocalizer(locale)
return l.Localize(id)
return GetLocalizer(locale).Localize(id)
}

func LocalizeWithData(locale discordgo.Locale, id string, data Data) string {
l, _ := GetLocalizer(locale)
return l.LocalizeWithData(id, data)
return GetLocalizer(locale).LocalizeWithData(id, data)
}

func LocalizePlural(locale discordgo.Locale, id string, count any) string {
l, _ := GetLocalizer(locale)
return l.LocalizePlural(id, count)
return GetLocalizer(locale).LocalizePlural(id, count)
}

func LocalizePluralWithData(locale discordgo.Locale, id string, count any, data Data) string {
l, _ := GetLocalizer(locale)
return l.LocalizePluralWithData(id, count, data)
return GetLocalizer(locale).LocalizePluralWithData(id, count, data)
}

type Localizer struct {
*i18n.Localizer
prefix string
}

func newLocalizer(locale discordgo.Locale) *Localizer {
return &Localizer{Localizer: i18n.NewLocalizer(bundle, string(locale))}
}

func (l *Localizer) Section(name string) *Localizer {
return &Localizer{Localizer: l.Localizer, prefix: name + "."}
}

func (l *Localizer) Localize(id string) string {
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
MessageID: id,
MessageID: l.prefix + id,
})
if s == "" && err != nil {
return id
Expand All @@ -122,7 +123,7 @@ func (l *Localizer) Localize(id string) string {

func (l *Localizer) LocalizeWithData(id string, data Data) string {
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
MessageID: id,
MessageID: l.prefix + id,
TemplateData: data,
})
if s == "" && err != nil {
Expand All @@ -133,7 +134,7 @@ func (l *Localizer) LocalizeWithData(id string, data Data) string {

func (l *Localizer) LocalizePlural(id string, count any) string {
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
MessageID: id,
MessageID: l.prefix + id,
PluralCount: count,
})
if s == "" && err != nil {
Expand All @@ -144,7 +145,7 @@ func (l *Localizer) LocalizePlural(id string, count any) string {

func (l *Localizer) LocalizePluralWithData(id string, count any, data Data) string {
s, err := l.Localizer.Localize(&i18n.LocalizeConfig{
MessageID: id,
MessageID: l.prefix + id,
TemplateData: data,
PluralCount: count,
})
Expand Down
15 changes: 15 additions & 0 deletions locales/en-GB.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ creation-welcome = """
Thank you for creating a ticket. A support team member will be available to assist you shortly. \
While you wait, please provide as much information as possible.\
"""

[command.info]
title = "Information"
version-title = "Version"
version = """
Axolotl v{{.axolotlVersion}} ({{.goVersion}})
Locales v{{.axolotlLangVersion}}
"""
status-title = "Status"
status = """
Uptime: `{{.uptime}}`
Last heartbeat: `{{.lastHeartbeat}}`
"""
supported-locales-title = "Supported Locales"
supported-locale-line = "{{.locale}} by {{.authors}}"
15 changes: 15 additions & 0 deletions locales/ja.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ creation-welcome = """
チケットを作成していただきありがとうございます。まもなくサポートチームメンバーが対応させていただきます。\
お待ちいただく間、できるだけ多くの情報をご提供ください。\
"""

[command.info]
title = "情報"
version-title = "バージョン"
version = """
Axolotl v{{.axolotlVersion}} ({{.goVersion}})
Locales v{{.axolotlLangVersion}}
"""
status-title = "現状"
status = """
アップタイム: `{{.uptime}}`
最後のハートビート: `{{.lastHeartbeat}}`
"""
supported-locales-title = "対応言語"
supported-locale-line = "{{.locale}}:{{.authors}}"

0 comments on commit 97aa9ae

Please sign in to comment.