🤖
adam
  • Getting started
  • First Steps
    • Creating Your First Command
    • Grouping Commands in Modules
    • Middlewares
    • Argument Parsing
  • Errors
    • Error Handling 101
    • Error Types
    • Changing How Errors Are Handled
  • Localization
    • Getting Started
    • How to Localize
  • Restrictions
    • What Are Restrictions?
    • Creating Custom Restrictions
Powered by GitBook
On this page
  • Choosing a Localization Library
  • Enabling Localization

Was this helpful?

Export as PDF
  1. Localization

Getting Started

PreviousChanging How Errors Are HandledNextHow to Localize

Last updated 3 years ago

Was this helpful?

Adam was built with the option of localization in mind. This means everything the user sees in the end is somehow localizable. Before you can use localization, however, you need to set a few things up.

Choosing a Localization Library

Adam does not provide direct support for localization. Instead, it abstracts it using a i18n.Func.

Although abstracted, not all types of i18n libraries are supported. Your localization library must support key-based placeholders, and pluralization through the use of an interface{} that is either a number type or a string containing a number. If you are unsure about which library to use, we recommend Nick Snyder's .

Enabling Localization

To enable localization, you need to first use a SettingsProvider that returns a *i18n.Localizer. Localizers are responsible for generating translations. Below is an example using go-i18n.

There are better ways of structuring this outside of package main. Regard this as an .

cmd/mybot/settings_provider.go
package main

import (
    "github.com/diamondburned/ariakwa/v2/discord"
    "github.com/mavolin/adam/pkg/i18n"
    "github.com/mavolin/disstate/v3/pkg/state"
    i18nimpl "github.com/nicksnyder/go-i18n/v2/i18n"
)

type settingsRepository interface {
    GuildSettings(discord.GuildID) (prefixes []string, lang string, err error)
}

func newSettingsProvider(
    repo settingsRepository, bundle *i18nimpl.Bundle,
) bot.SettingsProvider {    
    return func (_ *state.Base, m *discord.Message) (
        []string, *i18n.Localizer, bool,
    ) {    
        prefixes, lang, err := repo.GuildSettings(m.GuildID)
        if err != nil {
            log.Println(err)
            
            // allow execution regardless, and just use fallbacks
            return nil, nil, true
        }
        
        return prefixes, i18n.NewLocalizer(lang, newI18nFunc(bundle, lang)), true
    }
}

func newI18nFunc(b *i18nimpl.Bundle, lang string) i18n.Func {
    l := i18nimpl.NewLocalizer(b, lang)
    
    return func(
        term i18n.Term, placeholders map[string]interface{}, plural interface{},
    ) (string, error) {
        return l.Localize(&i18nimpl.LocalizeConfig{
            MessageID: string(term),
            TemplateData: placeholders,
            PluralCount: plural,
        })
    }
}
cmd/mybot/main.go
package main

import (
    "github.com/mavolin/myBot/pkg/repository/memory"
    
    "github.com/mavolin/adam/pkg/bot"
    "github.com/nicksnyder/go-i18n/v2/i18n"
    "golang.org/x/text/language"
    
    "os"
)

func main() {
    repo := memory.New()
    
    bundle := i18n.NewBundle(language.English)
    bundle.LoadMessageFile("en-US.yaml")

    bot, err := bot.New(bot.Options{
        Token: os.Getenv("DISCORD_BOT_TOKEN"),
        SettingsProvider: newSettingsProvider(repo, bundle),
    })
}
go-i18n
MWE