translations

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package translations provides for translations.

Example
package main

import (
	"fmt"
	"github.com/soranoba/valis"
	"github.com/soranoba/valis/code"
	"github.com/soranoba/valis/translations"
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/message/catalog"
)

func main() {
	c := translations.NewCatalog(catalog.Fallback(language.English))
	if true /* When set all predefined translation data. */ {
		for _, f := range translations.AllPredefinedCatalogRegistrationFunc {
			c.Set(f)
		}
	} else /* When select and set by yourself. */ {
		c.Set(translations.DefaultEnglish)
		c.Set(translations.DefaultJapanese)
	}

	err := valis.NewError(code.TooShortLength, "value", 10)
	en := message.NewPrinter(language.English, message.Catalog(c))
	fmt.Printf("%s %s.\n", ".Name", en.Sprintf(err.Code(), err.Params()...))

	ja := message.NewPrinter(language.Japanese, message.Catalog(c))
	fmt.Printf("%s%s。\n", "名前", ja.Sprintf(err.Code(), err.Params()...))

}
Output:

.Name is too short length (minimum is 10 characters).
名前は10文字以上必要です。

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// AllPredefinedCatalogRegistrationFunc is all catalog registration functions predefined by this library.
	AllPredefinedCatalogRegistrationFunc = [...]CatalogRegistrationFunc{
		DefaultEnglish,
		DefaultJapanese,
	}
)

Functions

func DefaultEnglish

func DefaultEnglish(c *catalog.Builder)

DefaultEnglish is a CatalogRegistrationFunc for English.

func DefaultJapanese

func DefaultJapanese(c *catalog.Builder)

DefaultJapanese is a CatalogRegistrationFunc for Japanese.

Types

type Catalog

type Catalog struct {
	catalog.Catalog
}

Catalog is a wrapper of catalog.Catalog.

func NewCatalog

func NewCatalog(opts ...catalog.Option) *Catalog

NewCatalog returns a new catalog instance.

func (*Catalog) Set

func (t *Catalog) Set(f CatalogRegistrationFunc)

Set calls CatalogRegistrationFunc, and registers translation data.

type CatalogRegistrationFunc

type CatalogRegistrationFunc func(c *catalog.Builder)

CatalogRegistrationFunc is a function that registers translation data to catalog.Builder.

Jump to

Keyboard shortcuts

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