• Stars
    star
    267
  • Rank 153,621 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🌎 a set of locales generated from the CLDR Project which can be used independently or within an i18n package; these were built for use with, but not exclusive to https://github.com/go-playground/universal-translator

locales

Project status Build Status GoDoc License

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, v36.0.1
  • 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"
)

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(l.FmtDateFull(datetime))
	fmt.Println(l.FmtDateLong(datetime))
	fmt.Println(l.FmtDateMedium(datetime))
	fmt.Println(l.FmtDateShort(datetime))

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

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

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

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

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

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

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

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

	var f64 float64

	f64 = -10356.4523

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

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

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

	f64 = 78.12

	// Percent
	fmt.Println(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.

More Repositories

1

validator

πŸ’―Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
Go
16,425
star
2

webhooks

🎣 Webhook receiver for GitHub, Bitbucket, GitLab, Gogs
Go
949
star
3

form

πŸš‚ Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.
Go
750
star
4

pool

🚀 a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation
Go
724
star
5

lars

🚨 Is a lightweight, fast and extensible zero allocation HTTP router for Go used to create customizable frameworks.
Go
386
star
6

universal-translator

πŸ’¬ i18n Translator for Go/Golang using CLDR data + pluralization rules
Go
375
star
7

log

πŸ“— Simple, configurable and scalable Structured Logging for Go.
Go
292
star
8

mold

βœ‚οΈ Is a general library to help modify or set data within data structures and other objects.
Go
225
star
9

stats

πŸ“ˆ Monitors Go MemStats + System stats such as Memory, Swap and CPU and sends via UDP anywhere you want for logging etc...
Go
170
star
10

pure

🚱 Is a lightweight HTTP router that sticks to the std "net/http" implementation
Go
149
star
11

overalls

πŸ‘–Multi-Package go project coverprofile for tools like goveralls
Go
114
star
12

statics

πŸ“ Embeds static resources into go files for single binary compilation + works with http.FileSystem + symlinks
Go
68
star
13

colors

🎨 Go color manipulation, conversion and printing library/utility
Go
67
star
14

assert

❗Basic Assertion Library used along side native go testing, with building blocks for custom assertions
Go
62
star
15

errors

πŸ’₯Error Context, Stack Trace, Types and Tags for full error handling and logging.
Go
61
star
16

kms

πŸ”ͺ Is a library that aids in graceful shutdown of a process/application
Go
47
star
17

pkg

⭐ pkg extends the core go packages with missing or additional functionality built in. All packages correspond to the std go package name with an additional suffix of `ext` to avoid naming conflicts.
Go
42
star
18

tz

Timezone Country and Zone data generated from timezonedb.com
Go
30
star
19

generate

πŸƒruns go generate recursively on a specified path or environment variable and can filter by regex
Go
30
star
20

justdoit

simple auto-compile daemon that just works
Go
19
star
21

spoon

library + program to help making zero downtime, self-upgrading programs and servers.
Go
16
star
22

retry

πŸ”„ Retry provides a set of standardized common components and abstracts away some code that normally is duplicated
Go
16
star
23

ansi

⬛ ansi contains a bunch of constants and possibly additional terminal related functionality in the future.
Go
14
star
24

sensitive

provides base types who's values should never be seen by the human eye, but still used for configuration.
Go
14
star
25

ksql

a JSON data expression lexer, parser, cli and library
Go
13
star
26

backoff

:bowtie: Backoff uses an exponential backoff algorithm to backoff between retries with optional auto-tuning functionality.
Go
12
star
27

cache

Contains multiple in-memory cache implementations including LRU & LFU
Go
11
star
28

assets

Asset Pipeline for Go HTML applications
Go
8
star
29

ws

πŸ™Œ ws creates a hub for WebSocket connections and abstracts away allot of the boilerplate code for managing connections using Gorilla WebSocket
Go
8
star
30

itertools

Go Iteration tools with a rusty flavour
Go
7
star
31

livereload

πŸ” is an asset live-reload library that allows easy registration of path & file change monitoring and notifications for https://github.com/livereload/livereload-js
JavaScript
6
star
32

relay-client-go

This package is a Go client for the Relay Job Runner https://github.com/rust-playground/relay-rs
Go
5
star
33

backoff-sys

Bare building blocks for backing off and can be used to build more complex backoff packages
Go
4
star
34

mongostore

πŸ”‘ Gorilla's session store implementation using MongoDB
Go
4
star
35

bundler

Generic Bundler to concatenate any type of files using a custom left and right delimiter, i.e. css or js files
Go
4
star
36

wave

〰️ Package wave is a thin helper layer on top of Go's net/rpc
Go
1
star