langs

package module
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 6 Imported by: 2

README

langs

Go Reference

Translation helpers.

Install

go get github.com/altipla-consulting/langs

Contributing

You can make pull requests or create issues in GitHub. Any code you send should be formatted using make gofmt.

License

MIT License

Documentation

Overview

Package langs contains translation helpers.

Index

Constants

This section is empty.

Variables

View Source
var (
	CA   = Lang{Code: "ca", Native: "Català", Group: "ca"}
	DE   = Lang{Code: "de", Native: "Deutsch", Group: "de"}
	DeDE = Lang{Code: "de-DE", Native: "Deutsch", Group: "de"}
	EN   = Lang{Code: "en", Native: "English", Group: "en"}
	EnGB = Lang{Code: "en-GB", Native: "English", Group: "en"}
	EnUS = Lang{Code: "en-US", Native: "English", Group: "en"}
	ES   = Lang{Code: "es", Native: "Español", Group: "es"}
	EsES = Lang{Code: "es-ES", Native: "Español", Group: "es"}
	EU   = Lang{Code: "eu", Native: "Euskera", Group: "eu"}
	FR   = Lang{Code: "fr", Native: "Français", Group: "fr"}
	FrFR = Lang{Code: "fr-FR", Native: "Français", Group: "fr"}
	IT   = Lang{Code: "it", Native: "Italiano", Group: "it"}
	JA   = Lang{Code: "ja", Native: "日本語", Group: "ja"}
	PT   = Lang{Code: "pt", Native: "Portugues", Group: "pt"}
	PtPT = Lang{Code: "pt-PT", Native: "Portugues", Group: "pt"}
	RU   = Lang{Code: "ru", Native: "Русский", Group: "ru"}

	Empty = Lang{}
)
View Source
var All = []Lang{
	CA,
	DE,
	DeDE,
	EN,
	EnGB,
	EnUS,
	ES,
	EsES,
	EU,
	FR,
	FrFR,
	IT,
	JA,
	PT,
	PtPT,
	RU,
}

All contains all the known languages of this library.

Functions

func IsValid

func IsValid(lang string) bool

IsValid checks if the lang code is a known one.

func NativeName deprecated added in v0.2.0

func NativeName(lang Lang) string

Deprecated: use Lang.Native instead.

Types

type Chain added in v0.3.0

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

Chain helps configuring the chain of fallbacks for a project.

func NewChain added in v0.3.0

func NewChain(fallbacks ...Lang) Chain

NewChain initializes a new chain.

type Content added in v0.3.0

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

Content represents a translatable string that can store a different value in each language.

It can be serialized to JSON to send it to a client application. It can also be used with libs.altipla.consulting/rdb.

func NewContent added in v0.3.0

func NewContent() Content

NewContent returns an empty content without any values.

func NewContentFromMap added in v0.3.0

func NewContentFromMap(values map[Lang]string) Content

NewContentFromMap builds a new content from a map containing one or multiple values.

func NewContentValue added in v0.3.0

func NewContentValue(lang Lang, value string) Content

NewContentValue builds a new content with a single translated value.

func ParseContent added in v0.3.0

func ParseContent(values map[string]string) (Content, error)

ParseContent reads a new content map parsing every language name to check if it's correct. It only returns an error for invalid languages.

func (*Content) Clear added in v0.3.0

func (content *Content) Clear(lang Lang)

Clear removes a specific language translated value.

func (*Content) ClearAll added in v0.3.0

func (content *Content) ClearAll()

ClearAll removes all translated values in all languages.

func (Content) Get added in v0.3.0

func (content Content) Get(lang Lang) string

Get returns the translated value for a language or empty if not present.

func (Content) GetChain added in v0.3.0

func (content Content) GetChain(chain Chain, lang Lang) string

GetChain does the following steps:

1. Return the content in the requested lang if available. 2. Use the fallback languages if one of them is available. Order is important here. 3. Return any lang available randomly to have something.

If the content is empty it returns an empty string.

func (Content) IsEmpty added in v0.3.0

func (content Content) IsEmpty() bool

IsEmpty returns if the content does not contains any translated value in any language.

func (Content) Map added in v0.4.0

func (content Content) Map() map[Lang]string

Map returns a map of every language of the content and its value.

func (Content) MarshalJSON added in v0.3.0

func (content Content) MarshalJSON() ([]byte, error)

MarshalJSON implements the JSON interface.

func (Content) PlainMap added in v0.4.0

func (content Content) PlainMap() map[string]string

PlainMap returns a map of every language of the content and its value in a format that can be serialized to protobufs.

func (*Content) Scan added in v0.5.0

func (content *Content) Scan(value interface{}) error

func (*Content) Set added in v0.3.0

func (content *Content) Set(lang Lang, value string)

Set changes the translated value of a language. If empty that language will be discarded from the content.

func (*Content) UnmarshalJSON added in v0.3.0

func (content *Content) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the JSON interface.

func (Content) Value added in v0.5.0

func (content Content) Value() (driver.Value, error)

type Lang added in v0.3.0

type Lang struct {
	Code   string
	Native string
	Group  string
}

Lang represents a language

func Parse added in v0.7.0

func Parse(lang string) (Lang, error)

Parse returns the Lang for a given language.

func (Lang) Empty added in v0.9.0

func (l Lang) Empty() bool

Empty returns if the lang is empty.

func (Lang) MarshalJSON added in v0.6.0

func (lang Lang) MarshalJSON() ([]byte, error)

func (Lang) MarshalText added in v0.6.0

func (lang Lang) MarshalText() ([]byte, error)

func (*Lang) Scan added in v0.8.0

func (lang *Lang) Scan(src any) error

Scan implements sql.Scanner.

func (Lang) String added in v0.3.0

func (lang Lang) String() string

String returns the code of the language as string.

func (*Lang) UnmarshalJSON added in v0.6.0

func (lang *Lang) UnmarshalJSON(b []byte) error

func (*Lang) UnmarshalText added in v0.6.0

func (lang *Lang) UnmarshalText(text []byte) error

func (Lang) Value added in v0.8.0

func (lang Lang) Value() (driver.Value, error)

Value implements driver.Valuer.

Jump to

Keyboard shortcuts

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