Documentation
¶
Index ¶
- func SetDefaultLanguage(lang Language)
- type Dict
- func (d Dict[M]) FormatError(w failure.ErrorWriter)
- func (d Dict[M]) Get(lang Language) (string, bool)
- func (d Dict[M]) LogValue() slog.Value
- func (d Dict[M]) MarshalJSON() ([]byte, error)
- func (d Dict[M]) MustGet(lang Language) string
- func (d Dict[M]) SetErrorField(field failure.FieldSetter)
- func (d Dict[M]) String() string
- type Language
- type Message
- type Template
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefaultLanguage ¶ added in v0.0.4
func SetDefaultLanguage(lang Language)
Types ¶
type Dict ¶ added in v0.0.2
Dict is the set of messages for each language. The type M is usually string. Template is used for dynamical messages that is evaluated at a timing language specified.
Example ¶
package main
import (
"fmt"
"golang.org/x/text/language"
"github.com/morikuni/go-mlang"
)
var InvalidUserName = mlang.Dict[string]{
language.English: "Invalid user name",
language.Japanese: "ユーザ名が不正です",
}
func InvalidPasswordLength(min, max int) mlang.Message {
return mlang.Dict[string]{
language.English: fmt.Sprintf("Password must be between %d and %d characters long", min, max),
language.Japanese: fmt.Sprintf("パスワードは%d文字以上%d文字以下である必要があります", min, max),
}
}
func PenCount(count int) mlang.Message {
s := mlang.Dict[string]{
language.Japanese: fmt.Sprintf("%d本のペン", count),
}
if count == 1 {
s[language.English] = "a pen"
} else {
s[language.English] = fmt.Sprintf("%d pens", count)
}
return s
}
func IHavePen(count int) mlang.Message {
return mlang.Dict[mlang.Template]{
language.English: mlang.NewTemplate("I have %s", PenCount(count)),
language.Japanese: mlang.NewTemplate("私は%sを持っています", PenCount(count)),
}
}
func main() {
fmt.Println(InvalidUserName.MustGet(language.English))
fmt.Println(InvalidUserName.MustGet(language.Japanese))
fmt.Println("-----")
fmt.Println(InvalidPasswordLength(1, 2).MustGet(language.English))
fmt.Println(InvalidPasswordLength(1, 2).MustGet(language.Japanese))
fmt.Println("-----")
fmt.Println(IHavePen(1).MustGet(language.English))
fmt.Println(IHavePen(1).MustGet(language.Japanese))
fmt.Println(IHavePen(2).MustGet(language.English))
fmt.Println(IHavePen(2).MustGet(language.Japanese))
}
Output: Invalid user name ユーザ名が不正です ----- Password must be between 1 and 2 characters long パスワードは1文字以上2文字以下である必要があります ----- I have a pen 私は1本のペンを持っています I have 2 pens 私は2本のペンを持っています
func (Dict[M]) FormatError ¶ added in v0.0.3
func (d Dict[M]) FormatError(w failure.ErrorWriter)
FormatError implements failure.ErrorFormatter.
func (Dict[M]) MarshalJSON ¶ added in v0.0.5
MarshalJSON implements json.Marshaler.
func (Dict[M]) SetErrorField ¶ added in v0.0.2
func (d Dict[M]) SetErrorField(field failure.FieldSetter)
type Language ¶ added in v0.0.2
type Language = any
Language can be any comparable types. Basically, golang.org/x/text/language.Tag is used, but you can use any your original types as well.
Click to show internal directories.
Click to hide internal directories.