locales

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2016 License: MIT Imports: 3 Imported by: 21

README

locales

![Project status](https://img.shields.io/badge/version-0.9.1-green.svg) [![Build Status](https://semaphoreci.com/api/v1/joeybloggs/locales/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/locales) [![Coverage Status](https://coveralls.io/repos/github/go-playground/locales/badge.svg?branch=master)](https://coveralls.io/github/go-playground/locales?branch=master) [![GoDoc](https://godoc.org/github.com/go-playground/locales?status.svg)](https://godoc.org/github.com/go-playground/locales) ![License](https://img.shields.io/dub/l/vibe-d.svg) [![Gitter](https://badges.gitter.im/go-playground/locales.svg)](https://gitter.im/go-playground/locales?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

Locales is a set of locales generated from the Unicode CLDR Project which can be used independently or within an i18n package; these were built for use with, but not exclusive to, Universal Translator.

Features

  • Rules generated from the latest CLDR data, v29
  • Contains Cardinal, Ordinal and Range Plural Rules
  • Contains Month, Weekday and Timezone translations built in
  • Contains Date & Time formatting functions
  • Contains Number, Currency, Accounting and Percent formatting functions
  • Supports the "Gregorian" calendar only ( my time isn't unlimited, had to draw the line somewhere )

Full Tests

I could sure use your help adding tests for every locale, it is a huge undertaking and I just don't have the free time to do it all at the moment; any help would be greatly appreciated!!!! please see issue for details.

Installation

Use go get

go get github.com/go-playground/locales

NOTES

You'll notice most return types are []byte, this is because most of the time the results will be concatenated with a larger body of text and can avoid some allocations if already appending to a byte array, otherwise just cast as string.

Usage

package main

import (
	"fmt"
	"time"

	"github.com/go-playground/locales/currency"
	"github.com/go-playground/locales/en_CA"
)

const (
	dateTimeString = "Jan 2, 2006 at 3:04:05pm"
)

func main() {

	loc, _ := time.LoadLocation("America/Toronto")
	datetime := time.Date(2016, 02, 03, 9, 0, 1, 0, loc)

	l := en_CA.New()

	// Dates
	fmt.Println(string(l.FmtDateFull(datetime)))
	fmt.Println(string(l.FmtDateLong(datetime)))
	fmt.Println(string(l.FmtDateMedium(datetime)))
	fmt.Println(string(l.FmtDateShort(datetime)))

	// Times
	fmt.Println(string(l.FmtTimeFull(datetime)))
	fmt.Println(string(l.FmtTimeLong(datetime)))
	fmt.Println(string(l.FmtTimeMedium(datetime)))
	fmt.Println(string(l.FmtTimeShort(datetime)))

	// Months Wide
	fmt.Println(string(l.MonthWide(time.January)))
	fmt.Println(string(l.MonthWide(time.February)))
	fmt.Println(string(l.MonthWide(time.March)))
	// ...

	// Months Abbreviated
	fmt.Println(string(l.MonthAbbreviated(time.January)))
	fmt.Println(string(l.MonthAbbreviated(time.February)))
	fmt.Println(string(l.MonthAbbreviated(time.March)))
	// ...

	// Months Narrow
	fmt.Println(string(l.MonthNarrow(time.January)))
	fmt.Println(string(l.MonthNarrow(time.February)))
	fmt.Println(string(l.MonthNarrow(time.March)))
	// ...

	// Weekdays Wide
	fmt.Println(string(l.WeekdayWide(time.Sunday)))
	fmt.Println(string(l.WeekdayWide(time.Monday)))
	fmt.Println(string(l.WeekdayWide(time.Tuesday)))
	// ...

	// Weekdays Abbreviated
	fmt.Println(string(l.WeekdayAbbreviated(time.Sunday)))
	fmt.Println(string(l.WeekdayAbbreviated(time.Monday)))
	fmt.Println(string(l.WeekdayAbbreviated(time.Tuesday)))
	// ...

	// Weekdays Short
	fmt.Println(string(l.WeekdayShort(time.Sunday)))
	fmt.Println(string(l.WeekdayShort(time.Monday)))
	fmt.Println(string(l.WeekdayShort(time.Tuesday)))
	// ...

	// Weekdays Narrow
	fmt.Println(string(l.WeekdayNarrow(time.Sunday)))
	fmt.Println(string(l.WeekdayNarrow(time.Monday)))
	fmt.Println(string(l.WeekdayNarrow(time.Tuesday)))
	// ...

	var f64 float64

	f64 = -10356.4523

	// Number
	fmt.Println(string(l.FmtNumber(f64, 2)))

	// Currency
	fmt.Println(string(l.FmtCurrency(f64, 2, currency.CAD)))
	fmt.Println(string(l.FmtCurrency(f64, 2, currency.USD)))

	// Accounting
	fmt.Println(string(l.FmtAccounting(f64, 2, currency.CAD)))
	fmt.Println(string(l.FmtAccounting(f64, 2, currency.USD)))

	f64 = 78.12

	// Percent
	fmt.Println(string(l.FmtPercent(f64, 0)))

	// Plural Rules for locale, so you know what rules you must cover
	fmt.Println(l.PluralsCardinal())
	fmt.Println(l.PluralsOrdinal())

	// Cardinal Plural Rules
	fmt.Println(l.CardinalPluralRule(1, 0))
	fmt.Println(l.CardinalPluralRule(1.0, 0))
	fmt.Println(l.CardinalPluralRule(1.0, 1))
	fmt.Println(l.CardinalPluralRule(3, 0))

	// Ordinal Plural Rules
	fmt.Println(l.OrdinalPluralRule(21, 0)) // 21st
	fmt.Println(l.OrdinalPluralRule(22, 0)) // 22nd
	fmt.Println(l.OrdinalPluralRule(33, 0)) // 33rd
	fmt.Println(l.OrdinalPluralRule(34, 0)) // 34th

	// Range Plural Rules
	fmt.Println(l.RangePluralRule(1, 0, 1, 0)) // 1-1
	fmt.Println(l.RangePluralRule(1, 0, 2, 0)) // 1-2
	fmt.Println(l.RangePluralRule(5, 0, 8, 0)) // 5-8
}

NOTES:

These rules were generated from the Unicode CLDR Project, if you encounter any issues I strongly encourage contributing to the CLDR project to get the locale information corrected and the next time these locales are regenerated the fix will come with.

I do however realize that time constraints are often important and so there are two options:

  1. Create your own locale, copy, paste and modify, and ensure it complies with the Translator interface.
  2. Add an exception in the locale generation code directly and once regenerated, fix will be in place.

Please to not make fixes inside the locale files, they WILL get overwritten when the locales are regenerated.

License

Distributed under MIT License, please see license file in code for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func F

func F(n float64, v uint64) (f int64)

F returns the visible fractional digits in N, with trailing zeros.

func T

func T(n float64, v uint64) (t int64)

T returns the visible fractional digits in N, without trailing zeros.

func W

func W(n float64, v uint64) (w int64)

W returns the number of visible fraction digits in N, without trailing zeros.

Types

type PluralRule

type PluralRule int

PluralRule denotes the type of plural rules

const (
	PluralRuleUnknown PluralRule = iota
	PluralRuleZero               // zero
	PluralRuleOne                // one - singular
	PluralRuleTwo                // two - dual
	PluralRuleFew                // few - paucal
	PluralRuleMany               // many - also used for fractions if they have a separate class
	PluralRuleOther              // other - required—general plural form—also used if the language only has a single form
)

PluralRule's

func (PluralRule) String

func (p PluralRule) String() string

String returns the string value of PluralRule

type Translator

type Translator interface {

	// Locale returns the string value of the translator
	Locale() string

	// returns an array of cardinal plural rules associated
	// with this translator
	PluralsCardinal() []PluralRule

	// returns an array of ordinal plural rules associated
	// with this translator
	PluralsOrdinal() []PluralRule

	// returns an array of range plural rules associated
	// with this translator
	PluralsRange() []PluralRule

	// returns the cardinal PluralRule given 'num' and digits/precision of 'v' for locale
	CardinalPluralRule(num float64, v uint64) PluralRule

	// returns the ordinal PluralRule given 'num' and digits/precision of 'v' for locale
	OrdinalPluralRule(num float64, v uint64) PluralRule

	// returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for locale
	RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) PluralRule

	// returns the locales abbreviated month given the 'month' provided
	MonthAbbreviated(month time.Month) string

	// returns the locales abbreviated months
	MonthsAbbreviated() []string

	// returns the locales narrow month given the 'month' provided
	MonthNarrow(month time.Month) string

	// returns the locales narrow months
	MonthsNarrow() []string

	// returns the locales wide month given the 'month' provided
	MonthWide(month time.Month) string

	// returns the locales wide months
	MonthsWide() []string

	// returns the locales abbreviated weekday given the 'weekday' provided
	WeekdayAbbreviated(weekday time.Weekday) string

	// returns the locales abbreviated weekdays
	WeekdaysAbbreviated() []string

	// returns the locales narrow weekday given the 'weekday' provided
	WeekdayNarrow(weekday time.Weekday) string

	// WeekdaysNarrowreturns the locales narrow weekdays
	WeekdaysNarrow() []string

	// returns the locales short weekday given the 'weekday' provided
	WeekdayShort(weekday time.Weekday) string

	// returns the locales short weekdays
	WeekdaysShort() []string

	// returns the locales wide weekday given the 'weekday' provided
	WeekdayWide(weekday time.Weekday) string

	// returns the locales wide weekdays
	WeekdaysWide() []string

	// returns 'num' with digits/precision of 'v' for locale and handles both Whole and Real numbers based on 'v'
	FmtNumber(num float64, v uint64) string

	// returns 'num' with digits/precision of 'v' for locale and handles both Whole and Real numbers based on 'v'
	// NOTE: 'num' passed into FmtPercent is assumed to be in percent already
	FmtPercent(num float64, v uint64) string

	// returns the currency representation of 'num' with digits/precision of 'v' for locale
	FmtCurrency(num float64, v uint64, currency currency.Type) string

	// returns the currency representation of 'num' with digits/precision of 'v' for locale
	// in accounting notation.
	FmtAccounting(num float64, v uint64, currency currency.Type) string

	// returns the short date representation of 't' for '{{ .Locale }}'
	FmtDateShort(t time.Time) string

	// returns the medium date representation of 't' for '{{ .Locale }}'
	FmtDateMedium(t time.Time) string

	//  returns the long date representation of 't' for '{{ .Locale }}'
	FmtDateLong(t time.Time) string

	// returns the full date representation of 't' for '{{ .Locale }}'
	FmtDateFull(t time.Time) string

	// returns the short time representation of 't' for '{{ .Locale }}'
	FmtTimeShort(t time.Time) string

	// returns the medium time representation of 't' for '{{ .Locale }}'
	FmtTimeMedium(t time.Time) string

	// returns the long time representation of 't' for '{{ .Locale }}'
	FmtTimeLong(t time.Time) string

	// returns the full time representation of 't' for '{{ .Locale }}'
	FmtTimeFull(t time.Time) string
}

Translator encapsulates an instance of a locale NOTE: some values are returned as a []byte just in case the caller wishes to add more and can help avoid allocations; otherwise just cast as string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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