zyt

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

Date translator for Go

Latest release CI workflow Go reference

The zyt[^name-explanation] package translates month and day names from Go's time package to and from non-English languages.

When parsing multiple name variants are understood. For example the month January in German is recognized as Januar, Jänner and multiple abbreviations.

Languages using partitive and genitive cases for months and days are supported (e.g. Finnish and many Slavic languages).

Locales can be customized by users, either by defining from scratch or by cloning and modifying.

[^name-explanation]: Zyt is Swiss German for time.

Usage

To print the current time in German:

fmt.Println(zyt.German.Format(time.RFC850, time.Now()))

To parse a timestamp in Finnish:

fmt.Println(zyt.Finnish.ParseInLocation("January 2006", "maaliskuu 2001", time.UTC))

To select the best-fitting locale:

l, _ := zyt.Best(language.BritishEnglish)
l.Format(time.RFC850, time.Now())

Supported locales

  • English
  • German
    • Austrian German
  • Finnish

Contributions to add more are very welcome.

Alternatives

Documentation

Index

Examples

Constants

View Source
const DefaultCacheSize int = 16

Variables

View Source
var (
	German         = New(german.German)
	AustrianGerman = New(german.AustrianGerman)
)
View Source
var Best = DefaultRegistry.Best
View Source
var DefaultRegistry = &Registry{}

The global default registry. Built-in languages are automatically registered.

View Source
var English = New(english.English)
View Source
var Finnish = New(finnish.Finnish)
View Source
var Register = DefaultRegistry.Register

Functions

func SetCacheSize

func SetCacheSize(size int)

SetCacheSize modifies the size of the internal cache used to store previously parsed time layouts. The cache is shared across all locales and has a default size of DefaultCacheSize.

Types

type Locale

type Locale struct {
	// contains filtered or unexported fields
}
Example (Cloned)
package main

import (
	"fmt"
	"time"

	"github.com/hansmi/zyt"
)

func main() {
	data := zyt.English.Data().Clone()
	data.Months[2].Name = "MyMarch"

	l := zyt.New(data)

	fmt.Println(l.Format("January 2006", time.Date(2000, time.March, 1, 0, 0, 0, 0, time.UTC)))

}
Output:

MyMarch 2000
Example (Custom)
package main

import (
	"fmt"
	"time"

	"github.com/hansmi/zyt"
	"github.com/hansmi/zyt/pkg/zytdata"
	"golang.org/x/text/language"
)

func main() {
	data := zytdata.LocaleData{
		Tag: language.MustParse("x-custom"),
		Months: [12]zytdata.MonthInfo{
			{
				Name:       "MyJanuary",
				Abbr:       "MyJan",
				ExtraNames: []string{"AlsoJanuary"},
			},
			// Other months omitted for brevity
		},
		Days: [7]zytdata.DayInfo{
			{
				Name: "MyMonday",
				Abbr: "MyMon",
			},
			// Other days omitted for brevity
		},
	}

	l := zyt.New(data)

	fmt.Println(l.Format("Monday, 1 January, 2006", time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC)))

	ts, err := l.ParseInLocation("January 2006", "AlsoJanuary 2001", time.UTC)
	if err != nil {
		panic(err)
	}
	fmt.Println(ts.Format(time.RFC3339))

}
Output:

MyMonday, 1 MyJanuary, 2001
2001-01-01T00:00:00Z

func New

func New(d zytdata.LocaleData) *Locale

New instantiates a new locale.

func (*Locale) Data

func (l *Locale) Data() zytdata.LocaleData

func (*Locale) Format

func (l *Locale) Format(layout string, t time.Time) string

Format returns a textual representation of the time value formatted according to the layout defined by the argument. Wraps time.Time.Format.

When the layout contains a numeric form of the day and the month name, the genitive form of the month name is used if the locale has one.

func (*Locale) ParseInLocation

func (l *Locale) ParseInLocation(layout, value string, loc *time.Location) (time.Time, error)

ParseInLocation parses a formatted string and returns the time value it represents. Wraps time.ParseInLocation.

func (*Locale) String

func (l *Locale) String() string

func (*Locale) Tag

func (l *Locale) Tag() language.Tag

type Registry

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

func (*Registry) Best

func (r *Registry) Best(tag language.Tag) (*Locale, language.Confidence)

Best selects the most appropriate locale for the requested language tag. If none is found the default is returned.

func (*Registry) Register

func (r *Registry) Register(priority int, l *Locale)

Register a locale. The locale with the lowest priority number becomes the default.

Directories

Path Synopsis
internal
pkg

Jump to

Keyboard shortcuts

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