mlang

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2024 License: MIT Imports: 6 Imported by: 0

README

go-mlang

Go Reference

Go library for i18n (multi language message).

Usage

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))
	// Invalid user name
	fmt.Println(InvalidUserName.MustGet(language.Japanese))
	// ユーザ名が不正です

	fmt.Println(InvalidPasswordLength(1, 2).MustGet(language.English))
	// Password must be between 1 and 2 characters long
	fmt.Println(InvalidPasswordLength(1, 2).MustGet(language.Japanese))
	// パスワードは1文字以上2文字以下である必要があります

	fmt.Println(IHavePen(1).MustGet(language.English))
	// I have a pen
	fmt.Println(IHavePen(1).MustGet(language.Japanese))
	// 私は1本のペンを持っています
	fmt.Println(IHavePen(2).MustGet(language.English))
	// I have 2 pens
	fmt.Println(IHavePen(2).MustGet(language.Japanese))
	// 私は2本のペンを持っています
}

Documentation

Index

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

type Dict[M string | Template] map[Language]M

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]) Get added in v0.0.2

func (d Dict[M]) Get(lang Language) (string, bool)

func (Dict[M]) LogValue added in v0.0.4

func (d Dict[M]) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (Dict[M]) MarshalJSON added in v0.0.5

func (d Dict[M]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Dict[M]) MustGet added in v0.0.2

func (d Dict[M]) MustGet(lang Language) string

func (Dict[M]) SetErrorField added in v0.0.2

func (d Dict[M]) SetErrorField(field failure.FieldSetter)

func (Dict[M]) String added in v0.0.4

func (d Dict[M]) String() string

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.

type Message added in v0.0.2

type Message interface {
	failure.Field

	Get(lang Language) (string, bool)
	MustGet(lang Language) string
	// contains filtered or unexported methods
}

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template is a message template.

func NewTemplate

func NewTemplate(format string, args ...any) Template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL