locale

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 9 Imported by: 0

README

Continuous Integration Coverage Go Reference

go-locale

A fluent API wrapper for go-i18n module.

Install

go get github.com/MajorLettuce/go-locale

Usage

Here is a complete example on how to use this package:

package main

import (
	"embed"
	"fmt"

	"github.com/MajorLettuce/go-locale"
)

// Embed FS variable must be defined outside of a function
// Make sure 'locale' directory exists in your project directory:
//
// <project root>
// |-- main.go
// |-- locale/
//   |-- en.yml  :  greeting: Hello, {{.name}}!
//   |-- es.yml  :  greeting: Hola, {{.name}}!

//go:embed locale/*.yml
var localeFS embed.FS

func main() {
	// Initialize reads *.yml files from 'locale' directory
	err := locale.Initialize("en", localeFS, "locale")
	if err != nil {
		panic(err)
	}

	// Now you can read locale messages globally
	fmt.Println(
		locale.Message("greeting"), // Hello, <no value>!
	)

	// You can specify message data and language fluently
	fmt.Println(
		locale.Message("greeting").In("es").With("name", "Alex"), // Hola, Alex!
	)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultLanguage

func GetDefaultLanguage() language.Tag

func GetLanguages

func GetLanguages() []language.Tag

func GetMessage

func GetMessage(id string, tag language.Tag, args []any, count interface{}, fallback string) string

func Initialize

func Initialize(
	defaultLanguage language.Tag,
	filesystem fs.ReadDirFS,
	directoryPath string,
) error

Load localization files from directory specified by the <directoryPath> and prepare them for global usage.

//go:embed locale/*.yml
var localeFS embed.FS
err := locale.Initialize(languages.English, localeFS, "locale")

func InitializeMock

func InitializeMock() error

Types

type FluentChain

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

func Count

func Count(count interface{}) *FluentChain

Constructs an instance of FluentChain and calls Count() on it

func Fallback

func Fallback(text string) *FluentChain

Constructs an instance of FluentChain and calls Count() on it

func In

func In(tag language.Tag) *FluentChain

Constructs an instance of FluentChain and calls In() on it

func Message

func Message(id string) *FluentChain

Constructs an instance of FluentChain and calls Message() on it

func Mock

func Mock(text string) *FluentChain

Constructs an instance of FluentChain and calls Mock() on it

func With

func With(args ...any) *FluentChain

Constructs an instance of FluentChain and calls With() on it

func (*FluentChain) Count

func (l *FluentChain) Count(count interface{}) *FluentChain

Change the plural count for the given fluent call chain

func (*FluentChain) Fallback

func (l *FluentChain) Fallback(text string) *FluentChain

Change the plural count for the given fluent call chain

func (*FluentChain) In

func (l *FluentChain) In(tag language.Tag) *FluentChain

Set the language for the given fluent call chain

func (*FluentChain) Message

func (l *FluentChain) Message(id string) *FluentChain

Set the message id as specified in the file

func (*FluentChain) Mock

func (l *FluentChain) Mock(text string) *FluentChain

Change the plural count for the given fluent call chain

func (*FluentChain) String

func (l *FluentChain) String() string

func (*FluentChain) With

func (l *FluentChain) With(args ...any) *FluentChain

Change the template arguments for the given fluent call chain

Jump to

Keyboard shortcuts

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